blob: 87221fda132188fecd42fcfe1045d20ab89ca43a [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
Edward Cree8e17c1b2017-08-07 15:30:30 +0100179#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200180#define BPF_COMPLEXITY_LIMIT_STACK 1024
Alexei Starovoitovceefbc92018-12-03 22:46:06 -0800181#define BPF_COMPLEXITY_LIMIT_STATES 64
Daniel Borkmann07016152016-04-05 22:33:17 +0200182
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200183#define BPF_MAP_PTR_UNPRIV 1UL
184#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
185 POISON_POINTER_DELTA))
186#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
187
188static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
189{
190 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
191}
192
193static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
194{
195 return aux->map_state & BPF_MAP_PTR_UNPRIV;
196}
197
198static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
199 const struct bpf_map *map, bool unpriv)
200{
201 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
202 unpriv |= bpf_map_ptr_unpriv(aux);
203 aux->map_state = (unsigned long)map |
204 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
205}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700206
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200207struct bpf_call_arg_meta {
208 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200209 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200210 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200211 int regno;
212 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700213 s64 msize_smax_value;
214 u64 msize_umax_value;
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700215 int ref_obj_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800216 int func_id;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200217};
218
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700219static DEFINE_MUTEX(bpf_verifier_lock);
220
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800221static const struct bpf_line_info *
222find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
223{
224 const struct bpf_line_info *linfo;
225 const struct bpf_prog *prog;
226 u32 i, nr_linfo;
227
228 prog = env->prog;
229 nr_linfo = prog->aux->nr_linfo;
230
231 if (!nr_linfo || insn_off >= prog->len)
232 return NULL;
233
234 linfo = prog->aux->linfo;
235 for (i = 1; i < nr_linfo; i++)
236 if (insn_off < linfo[i].insn_off)
237 break;
238
239 return &linfo[i - 1];
240}
241
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700242void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
243 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700244{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700245 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700246
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700247 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700248
249 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
250 "verifier log line truncated - local buffer too short\n");
251
252 n = min(log->len_total - log->len_used - 1, n);
253 log->kbuf[n] = '\0';
254
255 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
256 log->len_used += n;
257 else
258 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700259}
Jiri Olsaabe08842018-03-23 11:41:28 +0100260
261/* log_level controls verbosity level of eBPF verifier.
262 * bpf_verifier_log_write() is used to dump the verification trace to the log,
263 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000264 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100265__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
266 const char *fmt, ...)
267{
268 va_list args;
269
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700270 if (!bpf_verifier_log_needed(&env->log))
271 return;
272
Jiri Olsaabe08842018-03-23 11:41:28 +0100273 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700274 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100275 va_end(args);
276}
277EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
278
279__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
280{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700281 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100282 va_list args;
283
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700284 if (!bpf_verifier_log_needed(&env->log))
285 return;
286
Jiri Olsaabe08842018-03-23 11:41:28 +0100287 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700288 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100289 va_end(args);
290}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700291
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800292static const char *ltrim(const char *s)
293{
294 while (isspace(*s))
295 s++;
296
297 return s;
298}
299
300__printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
301 u32 insn_off,
302 const char *prefix_fmt, ...)
303{
304 const struct bpf_line_info *linfo;
305
306 if (!bpf_verifier_log_needed(&env->log))
307 return;
308
309 linfo = find_linfo(env, insn_off);
310 if (!linfo || linfo == env->prev_linfo)
311 return;
312
313 if (prefix_fmt) {
314 va_list args;
315
316 va_start(args, prefix_fmt);
317 bpf_verifier_vlog(&env->log, prefix_fmt, args);
318 va_end(args);
319 }
320
321 verbose(env, "%s\n",
322 ltrim(btf_name_by_offset(env->prog->aux->btf,
323 linfo->line_off)));
324
325 env->prev_linfo = linfo;
326}
327
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200328static bool type_is_pkt_pointer(enum bpf_reg_type type)
329{
330 return type == PTR_TO_PACKET ||
331 type == PTR_TO_PACKET_META;
332}
333
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800334static bool type_is_sk_pointer(enum bpf_reg_type type)
335{
336 return type == PTR_TO_SOCKET ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800337 type == PTR_TO_SOCK_COMMON ||
338 type == PTR_TO_TCP_SOCK;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800339}
340
Joe Stringer840b9612018-10-02 13:35:32 -0700341static bool reg_type_may_be_null(enum bpf_reg_type type)
342{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700343 return type == PTR_TO_MAP_VALUE_OR_NULL ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800344 type == PTR_TO_SOCKET_OR_NULL ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800345 type == PTR_TO_SOCK_COMMON_OR_NULL ||
346 type == PTR_TO_TCP_SOCK_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700347}
348
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800349static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
350{
351 return reg->type == PTR_TO_MAP_VALUE &&
352 map_value_has_spin_lock(reg->map_ptr);
353}
354
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700355static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
356{
357 return type == PTR_TO_SOCKET ||
358 type == PTR_TO_SOCKET_OR_NULL ||
359 type == PTR_TO_TCP_SOCK ||
360 type == PTR_TO_TCP_SOCK_OR_NULL;
361}
362
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700363static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700364{
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700365 return type == ARG_PTR_TO_SOCK_COMMON;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700366}
367
368/* Determine whether the function releases some resources allocated by another
369 * function call. The first reference type argument will be assumed to be
370 * released by release_reference().
371 */
372static bool is_release_function(enum bpf_func_id func_id)
373{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700374 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700375}
376
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800377static bool is_acquire_function(enum bpf_func_id func_id)
378{
379 return func_id == BPF_FUNC_sk_lookup_tcp ||
Lorenz Baueredbf8c02019-03-22 09:54:01 +0800380 func_id == BPF_FUNC_sk_lookup_udp ||
381 func_id == BPF_FUNC_skc_lookup_tcp;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800382}
383
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700384static bool is_ptr_cast_function(enum bpf_func_id func_id)
385{
386 return func_id == BPF_FUNC_tcp_sock ||
387 func_id == BPF_FUNC_sk_fullsock;
388}
389
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700390/* string representation of 'enum bpf_reg_type' */
391static const char * const reg_type_str[] = {
392 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100393 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700394 [PTR_TO_CTX] = "ctx",
395 [CONST_PTR_TO_MAP] = "map_ptr",
396 [PTR_TO_MAP_VALUE] = "map_value",
397 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700398 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700399 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200400 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700401 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700402 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700403 [PTR_TO_SOCKET] = "sock",
404 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800405 [PTR_TO_SOCK_COMMON] = "sock_common",
406 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800407 [PTR_TO_TCP_SOCK] = "tcp_sock",
408 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700409};
410
Edward Cree8efea212018-08-22 20:02:44 +0100411static char slot_type_char[] = {
412 [STACK_INVALID] = '?',
413 [STACK_SPILL] = 'r',
414 [STACK_MISC] = 'm',
415 [STACK_ZERO] = '0',
416};
417
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800418static void print_liveness(struct bpf_verifier_env *env,
419 enum bpf_reg_liveness live)
420{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800421 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800422 verbose(env, "_");
423 if (live & REG_LIVE_READ)
424 verbose(env, "r");
425 if (live & REG_LIVE_WRITTEN)
426 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800427 if (live & REG_LIVE_DONE)
428 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800429}
430
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800431static struct bpf_func_state *func(struct bpf_verifier_env *env,
432 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700433{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800434 struct bpf_verifier_state *cur = env->cur_state;
435
436 return cur->frame[reg->frameno];
437}
438
439static void print_verifier_state(struct bpf_verifier_env *env,
440 const struct bpf_func_state *state)
441{
442 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700443 enum bpf_reg_type t;
444 int i;
445
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800446 if (state->frameno)
447 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700448 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700449 reg = &state->regs[i];
450 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700451 if (t == NOT_INIT)
452 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800453 verbose(env, " R%d", i);
454 print_liveness(env, reg->live);
455 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100456 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
457 tnum_is_const(reg->var_off)) {
458 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700459 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800460 if (t == PTR_TO_STACK)
461 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100462 } else {
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700463 verbose(env, "(id=%d", reg->id);
464 if (reg_type_may_be_refcounted_or_null(t))
465 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
Edward Creef1174f72017-08-07 15:26:19 +0100466 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700467 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200468 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700469 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100470 else if (t == CONST_PTR_TO_MAP ||
471 t == PTR_TO_MAP_VALUE ||
472 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700473 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100474 reg->map_ptr->key_size,
475 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100476 if (tnum_is_const(reg->var_off)) {
477 /* Typically an immediate SCALAR_VALUE, but
478 * could be a pointer whose offset is too big
479 * for reg->off
480 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700481 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100482 } else {
483 if (reg->smin_value != reg->umin_value &&
484 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700485 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100486 (long long)reg->smin_value);
487 if (reg->smax_value != reg->umax_value &&
488 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700489 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100490 (long long)reg->smax_value);
491 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700492 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100493 (unsigned long long)reg->umin_value);
494 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700495 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100496 (unsigned long long)reg->umax_value);
497 if (!tnum_is_unknown(reg->var_off)) {
498 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100499
Edward Cree7d1238f2017-08-07 15:26:56 +0100500 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700501 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100502 }
Edward Creef1174f72017-08-07 15:26:19 +0100503 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700504 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100505 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700506 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700507 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100508 char types_buf[BPF_REG_SIZE + 1];
509 bool valid = false;
510 int j;
511
512 for (j = 0; j < BPF_REG_SIZE; j++) {
513 if (state->stack[i].slot_type[j] != STACK_INVALID)
514 valid = true;
515 types_buf[j] = slot_type_char[
516 state->stack[i].slot_type[j]];
517 }
518 types_buf[BPF_REG_SIZE] = 0;
519 if (!valid)
520 continue;
521 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
522 print_liveness(env, state->stack[i].spilled_ptr.live);
523 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800524 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700525 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100526 else
527 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700528 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700529 if (state->acquired_refs && state->refs[0].id) {
530 verbose(env, " refs=%d", state->refs[0].id);
531 for (i = 1; i < state->acquired_refs; i++)
532 if (state->refs[i].id)
533 verbose(env, ",%d", state->refs[i].id);
534 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700535 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700536}
537
Joe Stringer84dbf352018-10-02 13:35:34 -0700538#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
539static int copy_##NAME##_state(struct bpf_func_state *dst, \
540 const struct bpf_func_state *src) \
541{ \
542 if (!src->FIELD) \
543 return 0; \
544 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
545 /* internal bug, make state invalid to reject the program */ \
546 memset(dst, 0, sizeof(*dst)); \
547 return -EFAULT; \
548 } \
549 memcpy(dst->FIELD, src->FIELD, \
550 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
551 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700552}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700553/* copy_reference_state() */
554COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700555/* copy_stack_state() */
556COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
557#undef COPY_STATE_FN
558
559#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
560static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
561 bool copy_old) \
562{ \
563 u32 old_size = state->COUNT; \
564 struct bpf_##NAME##_state *new_##FIELD; \
565 int slot = size / SIZE; \
566 \
567 if (size <= old_size || !size) { \
568 if (copy_old) \
569 return 0; \
570 state->COUNT = slot * SIZE; \
571 if (!size && old_size) { \
572 kfree(state->FIELD); \
573 state->FIELD = NULL; \
574 } \
575 return 0; \
576 } \
577 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
578 GFP_KERNEL); \
579 if (!new_##FIELD) \
580 return -ENOMEM; \
581 if (copy_old) { \
582 if (state->FIELD) \
583 memcpy(new_##FIELD, state->FIELD, \
584 sizeof(*new_##FIELD) * (old_size / SIZE)); \
585 memset(new_##FIELD + old_size / SIZE, 0, \
586 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
587 } \
588 state->COUNT = slot * SIZE; \
589 kfree(state->FIELD); \
590 state->FIELD = new_##FIELD; \
591 return 0; \
592}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700593/* realloc_reference_state() */
594REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700595/* realloc_stack_state() */
596REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
597#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700598
599/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
600 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800601 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700602 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
603 * which realloc_stack_state() copies over. It points to previous
604 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700605 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700606static int realloc_func_state(struct bpf_func_state *state, int stack_size,
607 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700608{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700609 int err = realloc_reference_state(state, refs_size, copy_old);
610 if (err)
611 return err;
612 return realloc_stack_state(state, stack_size, copy_old);
613}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700614
Joe Stringerfd978bf72018-10-02 13:35:35 -0700615/* Acquire a pointer id from the env and update the state->refs to include
616 * this new pointer reference.
617 * On success, returns a valid pointer id to associate with the register
618 * On failure, returns a negative errno.
619 */
620static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
621{
622 struct bpf_func_state *state = cur_func(env);
623 int new_ofs = state->acquired_refs;
624 int id, err;
625
626 err = realloc_reference_state(state, state->acquired_refs + 1, true);
627 if (err)
628 return err;
629 id = ++env->id_gen;
630 state->refs[new_ofs].id = id;
631 state->refs[new_ofs].insn_idx = insn_idx;
632
633 return id;
634}
635
636/* release function corresponding to acquire_reference_state(). Idempotent. */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800637static int release_reference_state(struct bpf_func_state *state, int ptr_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700638{
639 int i, last_idx;
640
Joe Stringerfd978bf72018-10-02 13:35:35 -0700641 last_idx = state->acquired_refs - 1;
642 for (i = 0; i < state->acquired_refs; i++) {
643 if (state->refs[i].id == ptr_id) {
644 if (last_idx && i != last_idx)
645 memcpy(&state->refs[i], &state->refs[last_idx],
646 sizeof(*state->refs));
647 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
648 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700649 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700650 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700651 }
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800652 return -EINVAL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700653}
654
655static int transfer_reference_state(struct bpf_func_state *dst,
656 struct bpf_func_state *src)
657{
658 int err = realloc_reference_state(dst, src->acquired_refs, false);
659 if (err)
660 return err;
661 err = copy_reference_state(dst, src);
662 if (err)
663 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700664 return 0;
665}
666
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800667static void free_func_state(struct bpf_func_state *state)
668{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800669 if (!state)
670 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700671 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800672 kfree(state->stack);
673 kfree(state);
674}
675
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700676static void free_verifier_state(struct bpf_verifier_state *state,
677 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700678{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800679 int i;
680
681 for (i = 0; i <= state->curframe; i++) {
682 free_func_state(state->frame[i]);
683 state->frame[i] = NULL;
684 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700685 if (free_self)
686 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700687}
688
689/* copy verifier state from src to dst growing dst stack space
690 * when necessary to accommodate larger src stack
691 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800692static int copy_func_state(struct bpf_func_state *dst,
693 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700694{
695 int err;
696
Joe Stringerfd978bf72018-10-02 13:35:35 -0700697 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
698 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700699 if (err)
700 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700701 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
702 err = copy_reference_state(dst, src);
703 if (err)
704 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700705 return copy_stack_state(dst, src);
706}
707
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800708static int copy_verifier_state(struct bpf_verifier_state *dst_state,
709 const struct bpf_verifier_state *src)
710{
711 struct bpf_func_state *dst;
712 int i, err;
713
714 /* if dst has more stack frames then src frame, free them */
715 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
716 free_func_state(dst_state->frame[i]);
717 dst_state->frame[i] = NULL;
718 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100719 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800720 dst_state->curframe = src->curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800721 dst_state->active_spin_lock = src->active_spin_lock;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800722 for (i = 0; i <= src->curframe; i++) {
723 dst = dst_state->frame[i];
724 if (!dst) {
725 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
726 if (!dst)
727 return -ENOMEM;
728 dst_state->frame[i] = dst;
729 }
730 err = copy_func_state(dst, src->frame[i]);
731 if (err)
732 return err;
733 }
734 return 0;
735}
736
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700737static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
738 int *insn_idx)
739{
740 struct bpf_verifier_state *cur = env->cur_state;
741 struct bpf_verifier_stack_elem *elem, *head = env->head;
742 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700743
744 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700745 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700746
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700747 if (cur) {
748 err = copy_verifier_state(cur, &head->st);
749 if (err)
750 return err;
751 }
752 if (insn_idx)
753 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700754 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700755 *prev_insn_idx = head->prev_insn_idx;
756 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700757 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700758 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700759 env->head = elem;
760 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700761 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700762}
763
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100764static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100765 int insn_idx, int prev_insn_idx,
766 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700767{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700768 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100769 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700770 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700771
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700772 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700773 if (!elem)
774 goto err;
775
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700776 elem->insn_idx = insn_idx;
777 elem->prev_insn_idx = prev_insn_idx;
778 elem->next = env->head;
779 env->head = elem;
780 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700781 err = copy_verifier_state(&elem->st, cur);
782 if (err)
783 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100784 elem->st.speculative |= speculative;
Daniel Borkmann07016152016-04-05 22:33:17 +0200785 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700786 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700787 goto err;
788 }
789 return &elem->st;
790err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800791 free_verifier_state(env->cur_state, true);
792 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700793 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700794 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700795 return NULL;
796}
797
798#define CALLER_SAVED_REGS 6
799static const int caller_saved[CALLER_SAVED_REGS] = {
800 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
801};
802
Edward Creef1174f72017-08-07 15:26:19 +0100803static void __mark_reg_not_init(struct bpf_reg_state *reg);
804
Edward Creeb03c9f92017-08-07 15:26:36 +0100805/* Mark the unknown part of a register (variable offset or scalar value) as
806 * known to have the value @imm.
807 */
808static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
809{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700810 /* Clear id, off, and union(map_ptr, range) */
811 memset(((u8 *)reg) + sizeof(reg->type), 0,
812 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100813 reg->var_off = tnum_const(imm);
814 reg->smin_value = (s64)imm;
815 reg->smax_value = (s64)imm;
816 reg->umin_value = imm;
817 reg->umax_value = imm;
818}
819
Edward Creef1174f72017-08-07 15:26:19 +0100820/* Mark the 'variable offset' part of a register as zero. This should be
821 * used only on registers holding a pointer type.
822 */
823static void __mark_reg_known_zero(struct bpf_reg_state *reg)
824{
Edward Creeb03c9f92017-08-07 15:26:36 +0100825 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100826}
827
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800828static void __mark_reg_const_zero(struct bpf_reg_state *reg)
829{
830 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800831 reg->type = SCALAR_VALUE;
832}
833
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700834static void mark_reg_known_zero(struct bpf_verifier_env *env,
835 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100836{
837 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700838 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100839 /* Something bad happened, let's kill all regs */
840 for (regno = 0; regno < MAX_BPF_REG; regno++)
841 __mark_reg_not_init(regs + regno);
842 return;
843 }
844 __mark_reg_known_zero(regs + regno);
845}
846
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200847static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
848{
849 return type_is_pkt_pointer(reg->type);
850}
851
852static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
853{
854 return reg_is_pkt_pointer(reg) ||
855 reg->type == PTR_TO_PACKET_END;
856}
857
858/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
859static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
860 enum bpf_reg_type which)
861{
862 /* The register can already have a range from prior markings.
863 * This is fine as long as it hasn't been advanced from its
864 * origin.
865 */
866 return reg->type == which &&
867 reg->id == 0 &&
868 reg->off == 0 &&
869 tnum_equals_const(reg->var_off, 0);
870}
871
Edward Creeb03c9f92017-08-07 15:26:36 +0100872/* Attempts to improve min/max values based on var_off information */
873static void __update_reg_bounds(struct bpf_reg_state *reg)
874{
875 /* min signed is max(sign bit) | min(other bits) */
876 reg->smin_value = max_t(s64, reg->smin_value,
877 reg->var_off.value | (reg->var_off.mask & S64_MIN));
878 /* max signed is min(sign bit) | max(other bits) */
879 reg->smax_value = min_t(s64, reg->smax_value,
880 reg->var_off.value | (reg->var_off.mask & S64_MAX));
881 reg->umin_value = max(reg->umin_value, reg->var_off.value);
882 reg->umax_value = min(reg->umax_value,
883 reg->var_off.value | reg->var_off.mask);
884}
885
886/* Uses signed min/max values to inform unsigned, and vice-versa */
887static void __reg_deduce_bounds(struct bpf_reg_state *reg)
888{
889 /* Learn sign from signed bounds.
890 * If we cannot cross the sign boundary, then signed and unsigned bounds
891 * are the same, so combine. This works even in the negative case, e.g.
892 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
893 */
894 if (reg->smin_value >= 0 || reg->smax_value < 0) {
895 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
896 reg->umin_value);
897 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
898 reg->umax_value);
899 return;
900 }
901 /* Learn sign from unsigned bounds. Signed bounds cross the sign
902 * boundary, so we must be careful.
903 */
904 if ((s64)reg->umax_value >= 0) {
905 /* Positive. We can't learn anything from the smin, but smax
906 * is positive, hence safe.
907 */
908 reg->smin_value = reg->umin_value;
909 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
910 reg->umax_value);
911 } else if ((s64)reg->umin_value < 0) {
912 /* Negative. We can't learn anything from the smax, but smin
913 * is negative, hence safe.
914 */
915 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
916 reg->umin_value);
917 reg->smax_value = reg->umax_value;
918 }
919}
920
921/* Attempts to improve var_off based on unsigned min/max information */
922static void __reg_bound_offset(struct bpf_reg_state *reg)
923{
924 reg->var_off = tnum_intersect(reg->var_off,
925 tnum_range(reg->umin_value,
926 reg->umax_value));
927}
928
929/* Reset the min/max bounds of a register */
930static void __mark_reg_unbounded(struct bpf_reg_state *reg)
931{
932 reg->smin_value = S64_MIN;
933 reg->smax_value = S64_MAX;
934 reg->umin_value = 0;
935 reg->umax_value = U64_MAX;
936}
937
Edward Creef1174f72017-08-07 15:26:19 +0100938/* Mark a register as having a completely unknown (scalar) value. */
939static void __mark_reg_unknown(struct bpf_reg_state *reg)
940{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700941 /*
942 * Clear type, id, off, and union(map_ptr, range) and
943 * padding between 'type' and union
944 */
945 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100946 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100947 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800948 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100949 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100950}
951
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700952static void mark_reg_unknown(struct bpf_verifier_env *env,
953 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100954{
955 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700956 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800957 /* Something bad happened, let's kill all regs except FP */
958 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100959 __mark_reg_not_init(regs + regno);
960 return;
961 }
962 __mark_reg_unknown(regs + regno);
963}
964
965static void __mark_reg_not_init(struct bpf_reg_state *reg)
966{
967 __mark_reg_unknown(reg);
968 reg->type = NOT_INIT;
969}
970
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700971static void mark_reg_not_init(struct bpf_verifier_env *env,
972 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200973{
Edward Creef1174f72017-08-07 15:26:19 +0100974 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700975 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800976 /* Something bad happened, let's kill all regs except FP */
977 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100978 __mark_reg_not_init(regs + regno);
979 return;
980 }
981 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200982}
983
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700984static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800985 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700986{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800987 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988 int i;
989
Edward Creedc503a82017-08-15 20:34:35 +0100990 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700991 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100992 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +0100993 regs[i].parent = NULL;
Edward Creedc503a82017-08-15 20:34:35 +0100994 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700995
996 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100997 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700998 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800999 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001000
1001 /* 1st arg to a function */
1002 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001003 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +01001004}
1005
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001006#define BPF_MAIN_FUNC (-1)
1007static void init_func_state(struct bpf_verifier_env *env,
1008 struct bpf_func_state *state,
1009 int callsite, int frameno, int subprogno)
1010{
1011 state->callsite = callsite;
1012 state->frameno = frameno;
1013 state->subprogno = subprogno;
1014 init_reg_state(env, state);
1015}
1016
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001017enum reg_arg_type {
1018 SRC_OP, /* register is used as source operand */
1019 DST_OP, /* register is used as destination operand */
1020 DST_OP_NO_MARK /* same as above, check only, don't mark */
1021};
1022
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001023static int cmp_subprogs(const void *a, const void *b)
1024{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001025 return ((struct bpf_subprog_info *)a)->start -
1026 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001027}
1028
1029static int find_subprog(struct bpf_verifier_env *env, int off)
1030{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001031 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001032
Jiong Wang9c8105b2018-05-02 16:17:18 -04001033 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1034 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001035 if (!p)
1036 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001037 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001038
1039}
1040
1041static int add_subprog(struct bpf_verifier_env *env, int off)
1042{
1043 int insn_cnt = env->prog->len;
1044 int ret;
1045
1046 if (off >= insn_cnt || off < 0) {
1047 verbose(env, "call to invalid destination\n");
1048 return -EINVAL;
1049 }
1050 ret = find_subprog(env, off);
1051 if (ret >= 0)
1052 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001053 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001054 verbose(env, "too many subprograms\n");
1055 return -E2BIG;
1056 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001057 env->subprog_info[env->subprog_cnt++].start = off;
1058 sort(env->subprog_info, env->subprog_cnt,
1059 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001060 return 0;
1061}
1062
1063static int check_subprogs(struct bpf_verifier_env *env)
1064{
1065 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001066 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001067 struct bpf_insn *insn = env->prog->insnsi;
1068 int insn_cnt = env->prog->len;
1069
Jiong Wangf910cef2018-05-02 16:17:17 -04001070 /* Add entry function. */
1071 ret = add_subprog(env, 0);
1072 if (ret < 0)
1073 return ret;
1074
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001075 /* determine subprog starts. The end is one before the next starts */
1076 for (i = 0; i < insn_cnt; i++) {
1077 if (insn[i].code != (BPF_JMP | BPF_CALL))
1078 continue;
1079 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1080 continue;
1081 if (!env->allow_ptr_leaks) {
1082 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1083 return -EPERM;
1084 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001085 ret = add_subprog(env, i + insn[i].imm + 1);
1086 if (ret < 0)
1087 return ret;
1088 }
1089
Jiong Wang4cb3d992018-05-02 16:17:19 -04001090 /* Add a fake 'exit' subprog which could simplify subprog iteration
1091 * logic. 'subprog_cnt' should not be increased.
1092 */
1093 subprog[env->subprog_cnt].start = insn_cnt;
1094
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001095 if (env->log.level > 1)
1096 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001097 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001098
1099 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001100 subprog_start = subprog[cur_subprog].start;
1101 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001102 for (i = 0; i < insn_cnt; i++) {
1103 u8 code = insn[i].code;
1104
Jiong Wang092ed092019-01-26 12:26:01 -05001105 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001106 goto next;
1107 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1108 goto next;
1109 off = i + insn[i].off + 1;
1110 if (off < subprog_start || off >= subprog_end) {
1111 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1112 return -EINVAL;
1113 }
1114next:
1115 if (i == subprog_end - 1) {
1116 /* to avoid fall-through from one subprog into another
1117 * the last insn of the subprog should be either exit
1118 * or unconditional jump back
1119 */
1120 if (code != (BPF_JMP | BPF_EXIT) &&
1121 code != (BPF_JMP | BPF_JA)) {
1122 verbose(env, "last insn is not an exit or jmp\n");
1123 return -EINVAL;
1124 }
1125 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001126 cur_subprog++;
1127 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001128 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001129 }
1130 }
1131 return 0;
1132}
1133
Edward Cree679c7822018-08-22 20:02:19 +01001134/* Parentage chain of this register (or stack slot) should take care of all
1135 * issues like callee-saved registers, stack slot allocation time, etc.
1136 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001137static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001138 const struct bpf_reg_state *state,
1139 struct bpf_reg_state *parent)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001140{
1141 bool writes = parent == state->parent; /* Observe write marks */
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 }
Edward Creedc503a82017-08-15 20:34:35 +01001153 /* ... then we depend on parent's value */
Edward Cree679c7822018-08-22 20:02:19 +01001154 parent->live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001155 state = parent;
1156 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001157 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001158 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001159 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001160}
1161
1162static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001163 enum reg_arg_type t)
1164{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001165 struct bpf_verifier_state *vstate = env->cur_state;
1166 struct bpf_func_state *state = vstate->frame[vstate->curframe];
1167 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +01001168
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001169 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001170 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001171 return -EINVAL;
1172 }
1173
1174 if (t == SRC_OP) {
1175 /* check whether register used as source operand can be read */
1176 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001177 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001178 return -EACCES;
1179 }
Edward Cree679c7822018-08-22 20:02:19 +01001180 /* We don't need to worry about FP liveness because it's read-only */
1181 if (regno != BPF_REG_FP)
1182 return mark_reg_read(env, &regs[regno],
1183 regs[regno].parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001184 } else {
1185 /* check whether register used as dest operand can be written to */
1186 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001187 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001188 return -EACCES;
1189 }
Edward Creedc503a82017-08-15 20:34:35 +01001190 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001191 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001192 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001193 }
1194 return 0;
1195}
1196
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001197static bool is_spillable_regtype(enum bpf_reg_type type)
1198{
1199 switch (type) {
1200 case PTR_TO_MAP_VALUE:
1201 case PTR_TO_MAP_VALUE_OR_NULL:
1202 case PTR_TO_STACK:
1203 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001204 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001205 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001206 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001207 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001208 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001209 case PTR_TO_SOCKET:
1210 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001211 case PTR_TO_SOCK_COMMON:
1212 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001213 case PTR_TO_TCP_SOCK:
1214 case PTR_TO_TCP_SOCK_OR_NULL:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001215 return true;
1216 default:
1217 return false;
1218 }
1219}
1220
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001221/* Does this register contain a constant zero? */
1222static bool register_is_null(struct bpf_reg_state *reg)
1223{
1224 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1225}
1226
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001227/* check_stack_read/write functions track spill/fill of registers,
1228 * stack boundary and alignment are checked in check_mem_access()
1229 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001230static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001231 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001232 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001233{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001234 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001235 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001236 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001237
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001238 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001239 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001240 if (err)
1241 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001242 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1243 * so it's aligned access and [off, off + size) are within stack limits
1244 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001245 if (!env->allow_ptr_leaks &&
1246 state->stack[spi].slot_type[0] == STACK_SPILL &&
1247 size != BPF_REG_SIZE) {
1248 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1249 return -EACCES;
1250 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001251
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001252 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001253 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001254 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001255
1256 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001257 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001258 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001259 return -EACCES;
1260 }
1261
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001262 if (state != cur && type == PTR_TO_STACK) {
1263 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1264 return -EINVAL;
1265 }
1266
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001267 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001268 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001269 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001270
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001271 for (i = 0; i < BPF_REG_SIZE; i++) {
1272 if (state->stack[spi].slot_type[i] == STACK_MISC &&
1273 !env->allow_ptr_leaks) {
1274 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1275 int soff = (-spi - 1) * BPF_REG_SIZE;
1276
1277 /* detected reuse of integer stack slot with a pointer
1278 * which means either llvm is reusing stack slot or
1279 * an attacker is trying to exploit CVE-2018-3639
1280 * (speculative store bypass)
1281 * Have to sanitize that slot with preemptive
1282 * store of zero.
1283 */
1284 if (*poff && *poff != soff) {
1285 /* disallow programs where single insn stores
1286 * into two different stack slots, since verifier
1287 * cannot sanitize them
1288 */
1289 verbose(env,
1290 "insn %d cannot access two stack slots fp%d and fp%d",
1291 insn_idx, *poff, soff);
1292 return -EINVAL;
1293 }
1294 *poff = soff;
1295 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001296 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001297 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001298 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001299 u8 type = STACK_MISC;
1300
Edward Cree679c7822018-08-22 20:02:19 +01001301 /* regular write of data into stack destroys any spilled ptr */
1302 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001303 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1304 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1305 for (i = 0; i < BPF_REG_SIZE; i++)
1306 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001307
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001308 /* only mark the slot as written if all 8 bytes were written
1309 * otherwise read propagation may incorrectly stop too soon
1310 * when stack slots are partially written.
1311 * This heuristic means that read propagation will be
1312 * conservative, since it will add reg_live_read marks
1313 * to stack slots all the way to first state when programs
1314 * writes+reads less than 8 bytes
1315 */
1316 if (size == BPF_REG_SIZE)
1317 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1318
1319 /* when we zero initialize stack slots mark them as such */
1320 if (value_regno >= 0 &&
1321 register_is_null(&cur->regs[value_regno]))
1322 type = STACK_ZERO;
1323
Jiong Wang0bae2d42018-12-15 03:34:40 -05001324 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001325 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001326 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001327 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001328 }
1329 return 0;
1330}
1331
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001332static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001333 struct bpf_func_state *reg_state /* func where register points to */,
1334 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001335{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001336 struct bpf_verifier_state *vstate = env->cur_state;
1337 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001338 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1339 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001340
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001341 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001342 verbose(env, "invalid read from stack off %d+0 size %d\n",
1343 off, size);
1344 return -EACCES;
1345 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001346 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001347
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001348 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001349 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001350 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001351 return -EACCES;
1352 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001353 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001354 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001355 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001356 return -EACCES;
1357 }
1358 }
1359
Edward Creedc503a82017-08-15 20:34:35 +01001360 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001361 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001362 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001363 /* mark reg as written since spilled pointer state likely
1364 * has its liveness marks cleared by is_state_visited()
1365 * which resets stack/reg liveness for state transitions
1366 */
1367 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001368 }
Edward Cree679c7822018-08-22 20:02:19 +01001369 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1370 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001371 return 0;
1372 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001373 int zeros = 0;
1374
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001375 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001376 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1377 continue;
1378 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1379 zeros++;
1380 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001381 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001382 verbose(env, "invalid read from stack off %d+%d size %d\n",
1383 off, i, size);
1384 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001385 }
Edward Cree679c7822018-08-22 20:02:19 +01001386 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1387 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001388 if (value_regno >= 0) {
1389 if (zeros == size) {
1390 /* any size read into register is zero extended,
1391 * so the whole register == const_zero
1392 */
1393 __mark_reg_const_zero(&state->regs[value_regno]);
1394 } else {
1395 /* have read misc data from the stack */
1396 mark_reg_unknown(env, state->regs, value_regno);
1397 }
1398 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1399 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001400 return 0;
1401 }
1402}
1403
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001404static int check_stack_access(struct bpf_verifier_env *env,
1405 const struct bpf_reg_state *reg,
1406 int off, int size)
1407{
1408 /* Stack accesses must be at a fixed offset, so that we
1409 * can determine what type of data were returned. See
1410 * check_stack_read().
1411 */
1412 if (!tnum_is_const(reg->var_off)) {
1413 char tn_buf[48];
1414
1415 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1416 verbose(env, "variable stack access var_off=%s off=%d size=%d",
1417 tn_buf, off, size);
1418 return -EACCES;
1419 }
1420
1421 if (off >= 0 || off < -MAX_BPF_STACK) {
1422 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1423 return -EACCES;
1424 }
1425
1426 return 0;
1427}
1428
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001429/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001430static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001431 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001432{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001433 struct bpf_reg_state *regs = cur_regs(env);
1434 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001435
Yonghong Song9fd29c02017-11-12 14:49:09 -08001436 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1437 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001438 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001439 map->value_size, off, size);
1440 return -EACCES;
1441 }
1442 return 0;
1443}
1444
Edward Creef1174f72017-08-07 15:26:19 +01001445/* check read/write into a map element with possible variable offset */
1446static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001447 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001448{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001449 struct bpf_verifier_state *vstate = env->cur_state;
1450 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001451 struct bpf_reg_state *reg = &state->regs[regno];
1452 int err;
1453
Edward Creef1174f72017-08-07 15:26:19 +01001454 /* We may have adjusted the register to this map value, so we
1455 * need to try adding each of min_value and max_value to off
1456 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001457 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001458 if (env->log.level)
1459 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001460
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001461 /* The minimum value is only important with signed
1462 * comparisons where we can't assume the floor of a
1463 * value is 0. If we are using signed variables for our
1464 * index'es we need to make sure that whatever we use
1465 * will have a set floor within our range.
1466 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001467 if (reg->smin_value < 0 &&
1468 (reg->smin_value == S64_MIN ||
1469 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1470 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001471 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 -08001472 regno);
1473 return -EACCES;
1474 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001475 err = __check_map_access(env, regno, reg->smin_value + off, size,
1476 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001477 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001478 verbose(env, "R%d min value is outside of the array range\n",
1479 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001480 return err;
1481 }
1482
Edward Creeb03c9f92017-08-07 15:26:36 +01001483 /* If we haven't set a max value then we need to bail since we can't be
1484 * sure we won't do bad things.
1485 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001486 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001487 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001488 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 -08001489 regno);
1490 return -EACCES;
1491 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001492 err = __check_map_access(env, regno, reg->umax_value + off, size,
1493 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001494 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001495 verbose(env, "R%d max value is outside of the array range\n",
1496 regno);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001497
1498 if (map_value_has_spin_lock(reg->map_ptr)) {
1499 u32 lock = reg->map_ptr->spin_lock_off;
1500
1501 /* if any part of struct bpf_spin_lock can be touched by
1502 * load/store reject this program.
1503 * To check that [x1, x2) overlaps with [y1, y2)
1504 * it is sufficient to check x1 < y2 && y1 < x2.
1505 */
1506 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
1507 lock < reg->umax_value + off + size) {
1508 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
1509 return -EACCES;
1510 }
1511 }
Edward Creef1174f72017-08-07 15:26:19 +01001512 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001513}
1514
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001515#define MAX_PACKET_OFF 0xffff
1516
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001517static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001518 const struct bpf_call_arg_meta *meta,
1519 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001520{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001521 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001522 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001523 case BPF_PROG_TYPE_LWT_IN:
1524 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001525 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001526 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001527 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001528 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001529 if (t == BPF_WRITE)
1530 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001531 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001532
1533 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001534 case BPF_PROG_TYPE_SCHED_CLS:
1535 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001536 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001537 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001538 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001539 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001540 if (meta)
1541 return meta->pkt_access;
1542
1543 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001544 return true;
1545 default:
1546 return false;
1547 }
1548}
1549
Edward Creef1174f72017-08-07 15:26:19 +01001550static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001551 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001552{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001553 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001554 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001555
Yonghong Song9fd29c02017-11-12 14:49:09 -08001556 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1557 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001558 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 -07001559 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001560 return -EACCES;
1561 }
1562 return 0;
1563}
1564
Edward Creef1174f72017-08-07 15:26:19 +01001565static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001566 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001567{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001568 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001569 struct bpf_reg_state *reg = &regs[regno];
1570 int err;
1571
1572 /* We may have added a variable offset to the packet pointer; but any
1573 * reg->range we have comes after that. We are only checking the fixed
1574 * offset.
1575 */
1576
1577 /* We don't allow negative numbers, because we aren't tracking enough
1578 * detail to prove they're safe.
1579 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001580 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001581 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 +01001582 regno);
1583 return -EACCES;
1584 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001585 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001586 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001587 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001588 return err;
1589 }
Jiong Wange6478152018-11-08 04:08:42 -05001590
1591 /* __check_packet_access has made sure "off + size - 1" is within u16.
1592 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1593 * otherwise find_good_pkt_pointers would have refused to set range info
1594 * that __check_packet_access would have rejected this pkt access.
1595 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1596 */
1597 env->prog->aux->max_pkt_offset =
1598 max_t(u32, env->prog->aux->max_pkt_offset,
1599 off + reg->umax_value + size - 1);
1600
Edward Creef1174f72017-08-07 15:26:19 +01001601 return err;
1602}
1603
1604/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001605static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001606 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001607{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001608 struct bpf_insn_access_aux info = {
1609 .reg_type = *reg_type,
1610 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001611
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001612 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001613 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001614 /* A non zero info.ctx_field_size indicates that this field is a
1615 * candidate for later verifier transformation to load the whole
1616 * field and then apply a mask when accessed with a narrower
1617 * access than actual ctx access size. A zero info.ctx_field_size
1618 * will only allow for whole field access and rejects any other
1619 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001620 */
Yonghong Song23994632017-06-22 15:07:39 -07001621 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001622
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001623 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001624 /* remember the offset of last byte accessed in ctx */
1625 if (env->prog->aux->max_ctx_offset < off + size)
1626 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001627 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001628 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001629
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001630 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001631 return -EACCES;
1632}
1633
Petar Penkovd58e4682018-09-14 07:46:18 -07001634static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1635 int size)
1636{
1637 if (size < 0 || off < 0 ||
1638 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1639 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1640 off, size);
1641 return -EACCES;
1642 }
1643 return 0;
1644}
1645
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001646static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
1647 u32 regno, int off, int size,
1648 enum bpf_access_type t)
Joe Stringerc64b7982018-10-02 13:35:33 -07001649{
1650 struct bpf_reg_state *regs = cur_regs(env);
1651 struct bpf_reg_state *reg = &regs[regno];
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001652 struct bpf_insn_access_aux info = {};
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001653 bool valid;
Joe Stringerc64b7982018-10-02 13:35:33 -07001654
1655 if (reg->smin_value < 0) {
1656 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1657 regno);
1658 return -EACCES;
1659 }
1660
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001661 switch (reg->type) {
1662 case PTR_TO_SOCK_COMMON:
1663 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
1664 break;
1665 case PTR_TO_SOCKET:
1666 valid = bpf_sock_is_valid_access(off, size, t, &info);
1667 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001668 case PTR_TO_TCP_SOCK:
1669 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
1670 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001671 default:
1672 valid = false;
Joe Stringerc64b7982018-10-02 13:35:33 -07001673 }
1674
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001675
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001676 if (valid) {
1677 env->insn_aux_data[insn_idx].ctx_field_size =
1678 info.ctx_field_size;
1679 return 0;
1680 }
1681
1682 verbose(env, "R%d invalid %s access off=%d size=%d\n",
1683 regno, reg_type_str[reg->type], off, size);
1684
1685 return -EACCES;
Joe Stringerc64b7982018-10-02 13:35:33 -07001686}
1687
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001688static bool __is_pointer_value(bool allow_ptr_leaks,
1689 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001690{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001691 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001692 return false;
1693
Edward Creef1174f72017-08-07 15:26:19 +01001694 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001695}
1696
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001697static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1698{
1699 return cur_regs(env) + regno;
1700}
1701
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001702static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1703{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001704 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001705}
1706
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001707static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1708{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001709 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001710
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001711 return reg->type == PTR_TO_CTX;
1712}
1713
1714static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
1715{
1716 const struct bpf_reg_state *reg = reg_state(env, regno);
1717
1718 return type_is_sk_pointer(reg->type);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001719}
1720
Daniel Borkmannca369602018-02-23 22:29:05 +01001721static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1722{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001723 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001724
1725 return type_is_pkt_pointer(reg->type);
1726}
1727
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001728static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1729{
1730 const struct bpf_reg_state *reg = reg_state(env, regno);
1731
1732 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1733 return reg->type == PTR_TO_FLOW_KEYS;
1734}
1735
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001736static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1737 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001738 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001739{
Edward Creef1174f72017-08-07 15:26:19 +01001740 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001741 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001742
1743 /* Byte size accesses are always allowed. */
1744 if (!strict || size == 1)
1745 return 0;
1746
David S. Millere4eda882017-05-22 12:27:07 -04001747 /* For platforms that do not have a Kconfig enabling
1748 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1749 * NET_IP_ALIGN is universally set to '2'. And on platforms
1750 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1751 * to this code only in strict mode where we want to emulate
1752 * the NET_IP_ALIGN==2 checking. Therefore use an
1753 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001754 */
David S. Millere4eda882017-05-22 12:27:07 -04001755 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001756
1757 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1758 if (!tnum_is_aligned(reg_off, size)) {
1759 char tn_buf[48];
1760
1761 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001762 verbose(env,
1763 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001764 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001765 return -EACCES;
1766 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001767
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001768 return 0;
1769}
1770
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001771static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1772 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001773 const char *pointer_desc,
1774 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001775{
Edward Creef1174f72017-08-07 15:26:19 +01001776 struct tnum reg_off;
1777
1778 /* Byte size accesses are always allowed. */
1779 if (!strict || size == 1)
1780 return 0;
1781
1782 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1783 if (!tnum_is_aligned(reg_off, size)) {
1784 char tn_buf[48];
1785
1786 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001787 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001788 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001789 return -EACCES;
1790 }
1791
1792 return 0;
1793}
1794
David S. Millere07b98d2017-05-10 11:38:07 -07001795static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001796 const struct bpf_reg_state *reg, int off,
1797 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001798{
Daniel Borkmannca369602018-02-23 22:29:05 +01001799 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001800 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001801
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001802 switch (reg->type) {
1803 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001804 case PTR_TO_PACKET_META:
1805 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1806 * right in front, treat it the very same way.
1807 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001808 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07001809 case PTR_TO_FLOW_KEYS:
1810 pointer_desc = "flow keys ";
1811 break;
Edward Creef1174f72017-08-07 15:26:19 +01001812 case PTR_TO_MAP_VALUE:
1813 pointer_desc = "value ";
1814 break;
1815 case PTR_TO_CTX:
1816 pointer_desc = "context ";
1817 break;
1818 case PTR_TO_STACK:
1819 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001820 /* The stack spill tracking logic in check_stack_write()
1821 * and check_stack_read() relies on stack accesses being
1822 * aligned.
1823 */
1824 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001825 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07001826 case PTR_TO_SOCKET:
1827 pointer_desc = "sock ";
1828 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001829 case PTR_TO_SOCK_COMMON:
1830 pointer_desc = "sock_common ";
1831 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001832 case PTR_TO_TCP_SOCK:
1833 pointer_desc = "tcp_sock ";
1834 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001835 default:
Edward Creef1174f72017-08-07 15:26:19 +01001836 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001837 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001838 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1839 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001840}
1841
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001842static int update_stack_depth(struct bpf_verifier_env *env,
1843 const struct bpf_func_state *func,
1844 int off)
1845{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001846 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001847
1848 if (stack >= -off)
1849 return 0;
1850
1851 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001852 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001853 return 0;
1854}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001855
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001856/* starting from main bpf function walk all instructions of the function
1857 * and recursively walk all callees that given function can call.
1858 * Ignore jump and exit insns.
1859 * Since recursion is prevented by check_cfg() this algorithm
1860 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1861 */
1862static int check_max_stack_depth(struct bpf_verifier_env *env)
1863{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001864 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1865 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001866 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001867 int ret_insn[MAX_CALL_FRAMES];
1868 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001869
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001870process_func:
1871 /* round up to 32-bytes, since this is granularity
1872 * of interpreter stack size
1873 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001874 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001875 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001876 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001877 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001878 return -EACCES;
1879 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001880continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001881 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001882 for (; i < subprog_end; i++) {
1883 if (insn[i].code != (BPF_JMP | BPF_CALL))
1884 continue;
1885 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1886 continue;
1887 /* remember insn and function to return to */
1888 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001889 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001890
1891 /* find the callee */
1892 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001893 idx = find_subprog(env, i);
1894 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001895 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1896 i);
1897 return -EFAULT;
1898 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001899 frame++;
1900 if (frame >= MAX_CALL_FRAMES) {
1901 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1902 return -EFAULT;
1903 }
1904 goto process_func;
1905 }
1906 /* end of for() loop means the last insn of the 'subprog'
1907 * was reached. Doesn't matter whether it was JA or EXIT
1908 */
1909 if (frame == 0)
1910 return 0;
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 frame--;
1913 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001914 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001915 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001916}
1917
David S. Miller19d28fb2018-01-11 21:27:54 -05001918#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001919static int get_callee_stack_depth(struct bpf_verifier_env *env,
1920 const struct bpf_insn *insn, int idx)
1921{
1922 int start = idx + insn->imm + 1, subprog;
1923
1924 subprog = find_subprog(env, start);
1925 if (subprog < 0) {
1926 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1927 start);
1928 return -EFAULT;
1929 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001930 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001931}
David S. Miller19d28fb2018-01-11 21:27:54 -05001932#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001933
Daniel Borkmann58990d12018-06-07 17:40:03 +02001934static int check_ctx_reg(struct bpf_verifier_env *env,
1935 const struct bpf_reg_state *reg, int regno)
1936{
1937 /* Access to ctx or passing it to a helper is only allowed in
1938 * its original, unmodified form.
1939 */
1940
1941 if (reg->off) {
1942 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1943 regno, reg->off);
1944 return -EACCES;
1945 }
1946
1947 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1948 char tn_buf[48];
1949
1950 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1951 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1952 return -EACCES;
1953 }
1954
1955 return 0;
1956}
1957
Jann Horn0c17d1d2017-12-18 20:11:55 -08001958/* truncate register to smaller size (in bytes)
1959 * must be called with size < BPF_REG_SIZE
1960 */
1961static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
1962{
1963 u64 mask;
1964
1965 /* clear high bits in bit representation */
1966 reg->var_off = tnum_cast(reg->var_off, size);
1967
1968 /* fix arithmetic bounds */
1969 mask = ((u64)1 << (size * 8)) - 1;
1970 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
1971 reg->umin_value &= mask;
1972 reg->umax_value &= mask;
1973 } else {
1974 reg->umin_value = 0;
1975 reg->umax_value = mask;
1976 }
1977 reg->smin_value = reg->umin_value;
1978 reg->smax_value = reg->umax_value;
1979}
1980
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001981/* check whether memory at (regno + off) is accessible for t = (read | write)
1982 * if t==write, value_regno is a register which value is stored into memory
1983 * if t==read, value_regno is a register which will receive the value from memory
1984 * if t==write && value_regno==-1, some unknown value is stored into memory
1985 * if t==read && value_regno==-1, don't care what we read from memory
1986 */
Daniel Borkmannca369602018-02-23 22:29:05 +01001987static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
1988 int off, int bpf_size, enum bpf_access_type t,
1989 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001990{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001991 struct bpf_reg_state *regs = cur_regs(env);
1992 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001993 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001994 int size, err = 0;
1995
1996 size = bpf_size_to_bytes(bpf_size);
1997 if (size < 0)
1998 return size;
1999
Edward Creef1174f72017-08-07 15:26:19 +01002000 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01002001 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002002 if (err)
2003 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002004
Edward Creef1174f72017-08-07 15:26:19 +01002005 /* for access checks, reg->off is just part of off */
2006 off += reg->off;
2007
2008 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002009 if (t == BPF_WRITE && value_regno >= 0 &&
2010 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002011 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002012 return -EACCES;
2013 }
Josef Bacik48461132016-09-28 10:54:32 -04002014
Yonghong Song9fd29c02017-11-12 14:49:09 -08002015 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002016 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002017 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002018
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002019 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01002020 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002021
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002022 if (t == BPF_WRITE && value_regno >= 0 &&
2023 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002024 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002025 return -EACCES;
2026 }
Edward Creef1174f72017-08-07 15:26:19 +01002027
Daniel Borkmann58990d12018-06-07 17:40:03 +02002028 err = check_ctx_reg(env, reg, regno);
2029 if (err < 0)
2030 return err;
2031
Yonghong Song31fd8582017-06-13 15:52:13 -07002032 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002033 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002034 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002035 * PTR_TO_PACKET[_META,_END]. In the latter
2036 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01002037 */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002038 if (reg_type == SCALAR_VALUE) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002039 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002040 } else {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002041 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002042 value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002043 if (reg_type_may_be_null(reg_type))
2044 regs[value_regno].id = ++env->id_gen;
2045 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002046 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002047 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002048
Edward Creef1174f72017-08-07 15:26:19 +01002049 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002050 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002051 err = check_stack_access(env, reg, off, size);
2052 if (err)
2053 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002054
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002055 state = func(env, reg);
2056 err = update_stack_depth(env, state, off);
2057 if (err)
2058 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002059
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002060 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002061 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002062 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002063 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002064 err = check_stack_read(env, state, off, size,
2065 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002066 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002067 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002068 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002069 return -EACCES;
2070 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002071 if (t == BPF_WRITE && value_regno >= 0 &&
2072 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002073 verbose(env, "R%d leaks addr into packet\n",
2074 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002075 return -EACCES;
2076 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002077 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002078 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002079 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002080 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2081 if (t == BPF_WRITE && value_regno >= 0 &&
2082 is_pointer_value(env, value_regno)) {
2083 verbose(env, "R%d leaks addr into flow keys\n",
2084 value_regno);
2085 return -EACCES;
2086 }
2087
2088 err = check_flow_keys_access(env, off, size);
2089 if (!err && t == BPF_READ && value_regno >= 0)
2090 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002091 } else if (type_is_sk_pointer(reg->type)) {
Joe Stringerc64b7982018-10-02 13:35:33 -07002092 if (t == BPF_WRITE) {
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002093 verbose(env, "R%d cannot write into %s\n",
2094 regno, reg_type_str[reg->type]);
Joe Stringerc64b7982018-10-02 13:35:33 -07002095 return -EACCES;
2096 }
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002097 err = check_sock_access(env, insn_idx, regno, off, size, t);
Joe Stringerc64b7982018-10-02 13:35:33 -07002098 if (!err && value_regno >= 0)
2099 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002100 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002101 verbose(env, "R%d invalid mem access '%s'\n", regno,
2102 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002103 return -EACCES;
2104 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002105
Edward Creef1174f72017-08-07 15:26:19 +01002106 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002107 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002108 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002109 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002110 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002111 return err;
2112}
2113
Yonghong Song31fd8582017-06-13 15:52:13 -07002114static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002115{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002116 int err;
2117
2118 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2119 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002120 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002121 return -EINVAL;
2122 }
2123
2124 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002125 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002126 if (err)
2127 return err;
2128
2129 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002130 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002131 if (err)
2132 return err;
2133
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002134 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002135 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002136 return -EACCES;
2137 }
2138
Daniel Borkmannca369602018-02-23 22:29:05 +01002139 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002140 is_pkt_reg(env, insn->dst_reg) ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002141 is_flow_key_reg(env, insn->dst_reg) ||
2142 is_sk_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002143 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002144 insn->dst_reg,
2145 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002146 return -EACCES;
2147 }
2148
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002149 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002150 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002151 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002152 if (err)
2153 return err;
2154
2155 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002156 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002157 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002158}
2159
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002160static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2161 int off, int access_size,
2162 bool zero_size_allowed)
2163{
2164 struct bpf_reg_state *reg = reg_state(env, regno);
2165
2166 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2167 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2168 if (tnum_is_const(reg->var_off)) {
2169 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2170 regno, off, access_size);
2171 } else {
2172 char tn_buf[48];
2173
2174 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2175 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2176 regno, tn_buf, access_size);
2177 }
2178 return -EACCES;
2179 }
2180 return 0;
2181}
2182
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002183/* when register 'regno' is passed into function that will read 'access_size'
2184 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002185 * and all elements of stack are initialized.
2186 * Unlike most pointer bounds-checking functions, this one doesn't take an
2187 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002188 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002189static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002190 int access_size, bool zero_size_allowed,
2191 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002192{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002193 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002194 struct bpf_func_state *state = func(env, reg);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002195 int err, min_off, max_off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002196
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002197 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002198 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002199 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002200 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002201 return 0;
2202
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002203 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002204 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002205 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002206 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002207 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002208
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002209 if (tnum_is_const(reg->var_off)) {
2210 min_off = max_off = reg->var_off.value + reg->off;
2211 err = __check_stack_boundary(env, regno, min_off, access_size,
2212 zero_size_allowed);
2213 if (err)
2214 return err;
2215 } else {
2216 min_off = reg->smin_value + reg->off;
2217 max_off = reg->umax_value + reg->off;
2218 err = __check_stack_boundary(env, regno, min_off, access_size,
2219 zero_size_allowed);
2220 if (err)
2221 return err;
2222 err = __check_stack_boundary(env, regno, max_off, access_size,
2223 zero_size_allowed);
2224 if (err)
2225 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002226 }
2227
Daniel Borkmann435faee12016-04-13 00:10:51 +02002228 if (meta && meta->raw_mode) {
2229 meta->access_size = access_size;
2230 meta->regno = regno;
2231 return 0;
2232 }
2233
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002234 for (i = min_off; i < max_off + access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002235 u8 *stype;
2236
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002237 slot = -i - 1;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002238 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002239 if (state->allocated_stack <= slot)
2240 goto err;
2241 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2242 if (*stype == STACK_MISC)
2243 goto mark;
2244 if (*stype == STACK_ZERO) {
2245 /* helper can write anything into the stack */
2246 *stype = STACK_MISC;
2247 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002248 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002249err:
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002250 if (tnum_is_const(reg->var_off)) {
2251 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2252 min_off, i - min_off, access_size);
2253 } else {
2254 char tn_buf[48];
2255
2256 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2257 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
2258 tn_buf, i - min_off, access_size);
2259 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002260 return -EACCES;
2261mark:
2262 /* reading any byte out of 8-byte 'spill_slot' will cause
2263 * the whole slot to be marked as 'read'
2264 */
Edward Cree679c7822018-08-22 20:02:19 +01002265 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2266 state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002267 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002268 return update_stack_depth(env, state, min_off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002269}
2270
Gianluca Borello06c1c042017-01-09 10:19:49 -08002271static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2272 int access_size, bool zero_size_allowed,
2273 struct bpf_call_arg_meta *meta)
2274{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002275 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002276
Edward Creef1174f72017-08-07 15:26:19 +01002277 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002278 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002279 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002280 return check_packet_access(env, regno, reg->off, access_size,
2281 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002282 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002283 return check_map_access(env, regno, reg->off, access_size,
2284 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002285 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002286 return check_stack_boundary(env, regno, access_size,
2287 zero_size_allowed, meta);
2288 }
2289}
2290
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002291/* Implementation details:
2292 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
2293 * Two bpf_map_lookups (even with the same key) will have different reg->id.
2294 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
2295 * value_or_null->value transition, since the verifier only cares about
2296 * the range of access to valid map value pointer and doesn't care about actual
2297 * address of the map element.
2298 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
2299 * reg->id > 0 after value_or_null->value transition. By doing so
2300 * two bpf_map_lookups will be considered two different pointers that
2301 * point to different bpf_spin_locks.
2302 * The verifier allows taking only one bpf_spin_lock at a time to avoid
2303 * dead-locks.
2304 * Since only one bpf_spin_lock is allowed the checks are simpler than
2305 * reg_is_refcounted() logic. The verifier needs to remember only
2306 * one spin_lock instead of array of acquired_refs.
2307 * cur_state->active_spin_lock remembers which map value element got locked
2308 * and clears it after bpf_spin_unlock.
2309 */
2310static int process_spin_lock(struct bpf_verifier_env *env, int regno,
2311 bool is_lock)
2312{
2313 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2314 struct bpf_verifier_state *cur = env->cur_state;
2315 bool is_const = tnum_is_const(reg->var_off);
2316 struct bpf_map *map = reg->map_ptr;
2317 u64 val = reg->var_off.value;
2318
2319 if (reg->type != PTR_TO_MAP_VALUE) {
2320 verbose(env, "R%d is not a pointer to map_value\n", regno);
2321 return -EINVAL;
2322 }
2323 if (!is_const) {
2324 verbose(env,
2325 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
2326 regno);
2327 return -EINVAL;
2328 }
2329 if (!map->btf) {
2330 verbose(env,
2331 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
2332 map->name);
2333 return -EINVAL;
2334 }
2335 if (!map_value_has_spin_lock(map)) {
2336 if (map->spin_lock_off == -E2BIG)
2337 verbose(env,
2338 "map '%s' has more than one 'struct bpf_spin_lock'\n",
2339 map->name);
2340 else if (map->spin_lock_off == -ENOENT)
2341 verbose(env,
2342 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
2343 map->name);
2344 else
2345 verbose(env,
2346 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
2347 map->name);
2348 return -EINVAL;
2349 }
2350 if (map->spin_lock_off != val + reg->off) {
2351 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
2352 val + reg->off);
2353 return -EINVAL;
2354 }
2355 if (is_lock) {
2356 if (cur->active_spin_lock) {
2357 verbose(env,
2358 "Locking two bpf_spin_locks are not allowed\n");
2359 return -EINVAL;
2360 }
2361 cur->active_spin_lock = reg->id;
2362 } else {
2363 if (!cur->active_spin_lock) {
2364 verbose(env, "bpf_spin_unlock without taking a lock\n");
2365 return -EINVAL;
2366 }
2367 if (cur->active_spin_lock != reg->id) {
2368 verbose(env, "bpf_spin_unlock of different lock\n");
2369 return -EINVAL;
2370 }
2371 cur->active_spin_lock = 0;
2372 }
2373 return 0;
2374}
2375
Daniel Borkmann90133412018-01-20 01:24:29 +01002376static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2377{
2378 return type == ARG_PTR_TO_MEM ||
2379 type == ARG_PTR_TO_MEM_OR_NULL ||
2380 type == ARG_PTR_TO_UNINIT_MEM;
2381}
2382
2383static bool arg_type_is_mem_size(enum bpf_arg_type type)
2384{
2385 return type == ARG_CONST_SIZE ||
2386 type == ARG_CONST_SIZE_OR_ZERO;
2387}
2388
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002389static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002390 enum bpf_arg_type arg_type,
2391 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002392{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002393 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002394 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002395 int err = 0;
2396
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002397 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002398 return 0;
2399
Edward Creedc503a82017-08-15 20:34:35 +01002400 err = check_reg_arg(env, regno, SRC_OP);
2401 if (err)
2402 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002403
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002404 if (arg_type == ARG_ANYTHING) {
2405 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002406 verbose(env, "R%d leaks addr into helper function\n",
2407 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002408 return -EACCES;
2409 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002410 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002411 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002412
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002413 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002414 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002415 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002416 return -EACCES;
2417 }
2418
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002419 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002420 arg_type == ARG_PTR_TO_MAP_VALUE ||
2421 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002422 expected_type = PTR_TO_STACK;
Paul Chaignond71962f2018-04-24 15:07:54 +02002423 if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002424 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002425 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002426 } else if (arg_type == ARG_CONST_SIZE ||
2427 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002428 expected_type = SCALAR_VALUE;
2429 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002430 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002431 } else if (arg_type == ARG_CONST_MAP_PTR) {
2432 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002433 if (type != expected_type)
2434 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002435 } else if (arg_type == ARG_PTR_TO_CTX) {
2436 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002437 if (type != expected_type)
2438 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002439 err = check_ctx_reg(env, reg, regno);
2440 if (err < 0)
2441 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002442 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
2443 expected_type = PTR_TO_SOCK_COMMON;
2444 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
2445 if (!type_is_sk_pointer(type))
2446 goto err_type;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002447 if (reg->ref_obj_id) {
2448 if (meta->ref_obj_id) {
2449 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
2450 regno, reg->ref_obj_id,
2451 meta->ref_obj_id);
2452 return -EFAULT;
2453 }
2454 meta->ref_obj_id = reg->ref_obj_id;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002455 }
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002456 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
2457 if (meta->func_id == BPF_FUNC_spin_lock) {
2458 if (process_spin_lock(env, regno, true))
2459 return -EACCES;
2460 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
2461 if (process_spin_lock(env, regno, false))
2462 return -EACCES;
2463 } else {
2464 verbose(env, "verifier internal error\n");
2465 return -EFAULT;
2466 }
Daniel Borkmann90133412018-01-20 01:24:29 +01002467 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002468 expected_type = PTR_TO_STACK;
2469 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002470 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002471 * happens during stack boundary checking.
2472 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002473 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002474 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002475 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002476 else if (!type_is_pkt_pointer(type) &&
2477 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002478 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002479 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002480 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002481 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002482 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002483 return -EFAULT;
2484 }
2485
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002486 if (arg_type == ARG_CONST_MAP_PTR) {
2487 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002488 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002489 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2490 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2491 * check that [key, key + map->key_size) are within
2492 * stack limits and initialized
2493 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002494 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002495 /* in function declaration map_ptr must come before
2496 * map_key, so that it's verified and known before
2497 * we have to check map_key here. Otherwise it means
2498 * that kernel subsystem misconfigured verifier
2499 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002500 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002501 return -EACCES;
2502 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002503 err = check_helper_mem_access(env, regno,
2504 meta->map_ptr->key_size, false,
2505 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002506 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
2507 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002508 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2509 * check [value, value + map->value_size) validity
2510 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002511 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002512 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002513 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002514 return -EACCES;
2515 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002516 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002517 err = check_helper_mem_access(env, regno,
2518 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002519 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002520 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002521 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002522
Yonghong Song849fa502018-04-28 22:28:09 -07002523 /* remember the mem_size which may be used later
2524 * to refine return values.
2525 */
2526 meta->msize_smax_value = reg->smax_value;
2527 meta->msize_umax_value = reg->umax_value;
2528
Edward Creef1174f72017-08-07 15:26:19 +01002529 /* The register is SCALAR_VALUE; the access check
2530 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002531 */
Edward Creef1174f72017-08-07 15:26:19 +01002532 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002533 /* For unprivileged variable accesses, disable raw
2534 * mode so that the program is required to
2535 * initialize all the memory that the helper could
2536 * just partially fill up.
2537 */
2538 meta = NULL;
2539
Edward Creeb03c9f92017-08-07 15:26:36 +01002540 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002541 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002542 regno);
2543 return -EACCES;
2544 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002545
Edward Creeb03c9f92017-08-07 15:26:36 +01002546 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002547 err = check_helper_mem_access(env, regno - 1, 0,
2548 zero_size_allowed,
2549 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002550 if (err)
2551 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002552 }
Edward Creef1174f72017-08-07 15:26:19 +01002553
Edward Creeb03c9f92017-08-07 15:26:36 +01002554 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002555 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002556 regno);
2557 return -EACCES;
2558 }
2559 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002560 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002561 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002562 }
2563
2564 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002565err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002566 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002567 reg_type_str[type], reg_type_str[expected_type]);
2568 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002569}
2570
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002571static int check_map_func_compatibility(struct bpf_verifier_env *env,
2572 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002573{
Kaixu Xia35578d72015-08-06 07:02:35 +00002574 if (!map)
2575 return 0;
2576
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002577 /* We need a two way check, first is from map perspective ... */
2578 switch (map->map_type) {
2579 case BPF_MAP_TYPE_PROG_ARRAY:
2580 if (func_id != BPF_FUNC_tail_call)
2581 goto error;
2582 break;
2583 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2584 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002585 func_id != BPF_FUNC_perf_event_output &&
2586 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002587 goto error;
2588 break;
2589 case BPF_MAP_TYPE_STACK_TRACE:
2590 if (func_id != BPF_FUNC_get_stackid)
2591 goto error;
2592 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002593 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002594 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002595 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002596 goto error;
2597 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002598 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002599 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002600 if (func_id != BPF_FUNC_get_local_storage)
2601 goto error;
2602 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002603 /* devmap returns a pointer to a live net_device ifindex that we cannot
2604 * allow to be modified from bpf side. So do not allow lookup elements
2605 * for now.
2606 */
2607 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002608 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002609 goto error;
2610 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002611 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2612 * appear.
2613 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002614 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002615 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002616 if (func_id != BPF_FUNC_redirect_map)
2617 goto error;
2618 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002619 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002620 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002621 if (func_id != BPF_FUNC_map_lookup_elem)
2622 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002623 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002624 case BPF_MAP_TYPE_SOCKMAP:
2625 if (func_id != BPF_FUNC_sk_redirect_map &&
2626 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002627 func_id != BPF_FUNC_map_delete_elem &&
2628 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002629 goto error;
2630 break;
John Fastabend81110382018-05-14 10:00:17 -07002631 case BPF_MAP_TYPE_SOCKHASH:
2632 if (func_id != BPF_FUNC_sk_redirect_hash &&
2633 func_id != BPF_FUNC_sock_hash_update &&
2634 func_id != BPF_FUNC_map_delete_elem &&
2635 func_id != BPF_FUNC_msg_redirect_hash)
2636 goto error;
2637 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002638 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2639 if (func_id != BPF_FUNC_sk_select_reuseport)
2640 goto error;
2641 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002642 case BPF_MAP_TYPE_QUEUE:
2643 case BPF_MAP_TYPE_STACK:
2644 if (func_id != BPF_FUNC_map_peek_elem &&
2645 func_id != BPF_FUNC_map_pop_elem &&
2646 func_id != BPF_FUNC_map_push_elem)
2647 goto error;
2648 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002649 default:
2650 break;
2651 }
2652
2653 /* ... and second from the function itself. */
2654 switch (func_id) {
2655 case BPF_FUNC_tail_call:
2656 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2657 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002658 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002659 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2660 return -EINVAL;
2661 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002662 break;
2663 case BPF_FUNC_perf_event_read:
2664 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002665 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002666 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2667 goto error;
2668 break;
2669 case BPF_FUNC_get_stackid:
2670 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2671 goto error;
2672 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002673 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002674 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002675 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2676 goto error;
2677 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002678 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002679 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002680 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2681 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002682 goto error;
2683 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002684 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002685 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07002686 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07002687 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2688 goto error;
2689 break;
John Fastabend81110382018-05-14 10:00:17 -07002690 case BPF_FUNC_sk_redirect_hash:
2691 case BPF_FUNC_msg_redirect_hash:
2692 case BPF_FUNC_sock_hash_update:
2693 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07002694 goto error;
2695 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002696 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00002697 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2698 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07002699 goto error;
2700 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002701 case BPF_FUNC_sk_select_reuseport:
2702 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2703 goto error;
2704 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002705 case BPF_FUNC_map_peek_elem:
2706 case BPF_FUNC_map_pop_elem:
2707 case BPF_FUNC_map_push_elem:
2708 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2709 map->map_type != BPF_MAP_TYPE_STACK)
2710 goto error;
2711 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002712 default:
2713 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002714 }
2715
2716 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002717error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002718 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002719 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002720 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002721}
2722
Daniel Borkmann90133412018-01-20 01:24:29 +01002723static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002724{
2725 int count = 0;
2726
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002727 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002728 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002729 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002730 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002731 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002732 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002733 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002734 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002735 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002736 count++;
2737
Daniel Borkmann90133412018-01-20 01:24:29 +01002738 /* We only support one arg being in raw mode at the moment,
2739 * which is sufficient for the helper functions we have
2740 * right now.
2741 */
2742 return count <= 1;
2743}
2744
2745static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2746 enum bpf_arg_type arg_next)
2747{
2748 return (arg_type_is_mem_ptr(arg_curr) &&
2749 !arg_type_is_mem_size(arg_next)) ||
2750 (!arg_type_is_mem_ptr(arg_curr) &&
2751 arg_type_is_mem_size(arg_next));
2752}
2753
2754static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2755{
2756 /* bpf_xxx(..., buf, len) call will access 'len'
2757 * bytes from memory 'buf'. Both arg types need
2758 * to be paired, so make sure there's no buggy
2759 * helper function specification.
2760 */
2761 if (arg_type_is_mem_size(fn->arg1_type) ||
2762 arg_type_is_mem_ptr(fn->arg5_type) ||
2763 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2764 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2765 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2766 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2767 return false;
2768
2769 return true;
2770}
2771
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002772static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002773{
2774 int count = 0;
2775
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002776 if (arg_type_may_be_refcounted(fn->arg1_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002777 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002778 if (arg_type_may_be_refcounted(fn->arg2_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002779 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002780 if (arg_type_may_be_refcounted(fn->arg3_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002781 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002782 if (arg_type_may_be_refcounted(fn->arg4_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002783 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002784 if (arg_type_may_be_refcounted(fn->arg5_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07002785 count++;
2786
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002787 /* A reference acquiring function cannot acquire
2788 * another refcounted ptr.
2789 */
2790 if (is_acquire_function(func_id) && count)
2791 return false;
2792
Joe Stringerfd978bf72018-10-02 13:35:35 -07002793 /* We only support one arg being unreferenced at the moment,
2794 * which is sufficient for the helper functions we have right now.
2795 */
2796 return count <= 1;
2797}
2798
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002799static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
Daniel Borkmann90133412018-01-20 01:24:29 +01002800{
2801 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07002802 check_arg_pair_ok(fn) &&
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002803 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002804}
2805
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002806/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2807 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002808 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002809static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2810 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002811{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002812 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002813 int i;
2814
2815 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002816 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002817 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002818
Joe Stringerf3709f62018-10-02 13:35:29 -07002819 bpf_for_each_spilled_reg(i, state, reg) {
2820 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002821 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002822 if (reg_is_pkt_pointer_any(reg))
2823 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002824 }
2825}
2826
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002827static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2828{
2829 struct bpf_verifier_state *vstate = env->cur_state;
2830 int i;
2831
2832 for (i = 0; i <= vstate->curframe; i++)
2833 __clear_all_pkt_pointers(env, vstate->frame[i]);
2834}
2835
Joe Stringerfd978bf72018-10-02 13:35:35 -07002836static void release_reg_references(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002837 struct bpf_func_state *state,
2838 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002839{
2840 struct bpf_reg_state *regs = state->regs, *reg;
2841 int i;
2842
2843 for (i = 0; i < MAX_BPF_REG; i++)
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002844 if (regs[i].ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002845 mark_reg_unknown(env, regs, i);
2846
2847 bpf_for_each_spilled_reg(i, state, reg) {
2848 if (!reg)
2849 continue;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002850 if (reg->ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002851 __mark_reg_unknown(reg);
2852 }
2853}
2854
2855/* The pointer with the specified id has released its reference to kernel
2856 * resources. Identify all copies of the same pointer and clear the reference.
2857 */
2858static int release_reference(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002859 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07002860{
2861 struct bpf_verifier_state *vstate = env->cur_state;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002862 int err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002863 int i;
2864
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002865 err = release_reference_state(cur_func(env), ref_obj_id);
2866 if (err)
2867 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002868
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002869 for (i = 0; i <= vstate->curframe; i++)
2870 release_reg_references(env, vstate->frame[i], ref_obj_id);
2871
2872 return 0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002873}
2874
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002875static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2876 int *insn_idx)
2877{
2878 struct bpf_verifier_state *state = env->cur_state;
2879 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002880 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002881
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002882 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002883 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002884 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002885 return -E2BIG;
2886 }
2887
2888 target_insn = *insn_idx + insn->imm;
2889 subprog = find_subprog(env, target_insn + 1);
2890 if (subprog < 0) {
2891 verbose(env, "verifier bug. No program starts at insn %d\n",
2892 target_insn + 1);
2893 return -EFAULT;
2894 }
2895
2896 caller = state->frame[state->curframe];
2897 if (state->frame[state->curframe + 1]) {
2898 verbose(env, "verifier bug. Frame %d already allocated\n",
2899 state->curframe + 1);
2900 return -EFAULT;
2901 }
2902
2903 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2904 if (!callee)
2905 return -ENOMEM;
2906 state->frame[state->curframe + 1] = callee;
2907
2908 /* callee cannot access r0, r6 - r9 for reading and has to write
2909 * into its own stack before reading from it.
2910 * callee can read/write into caller's stack
2911 */
2912 init_func_state(env, callee,
2913 /* remember the callsite, it will be used by bpf_exit */
2914 *insn_idx /* callsite */,
2915 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04002916 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002917
Joe Stringerfd978bf72018-10-02 13:35:35 -07002918 /* Transfer references to the callee */
2919 err = transfer_reference_state(callee, caller);
2920 if (err)
2921 return err;
2922
Edward Cree679c7822018-08-22 20:02:19 +01002923 /* copy r1 - r5 args that callee can access. The copy includes parent
2924 * pointers, which connects us up to the liveness chain
2925 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002926 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2927 callee->regs[i] = caller->regs[i];
2928
Edward Cree679c7822018-08-22 20:02:19 +01002929 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002930 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2931 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2932 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2933 }
2934
2935 /* only increment it after check_reg_arg() finished */
2936 state->curframe++;
2937
2938 /* and go analyze first insn of the callee */
2939 *insn_idx = target_insn;
2940
2941 if (env->log.level) {
2942 verbose(env, "caller:\n");
2943 print_verifier_state(env, caller);
2944 verbose(env, "callee:\n");
2945 print_verifier_state(env, callee);
2946 }
2947 return 0;
2948}
2949
2950static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2951{
2952 struct bpf_verifier_state *state = env->cur_state;
2953 struct bpf_func_state *caller, *callee;
2954 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002955 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002956
2957 callee = state->frame[state->curframe];
2958 r0 = &callee->regs[BPF_REG_0];
2959 if (r0->type == PTR_TO_STACK) {
2960 /* technically it's ok to return caller's stack pointer
2961 * (or caller's caller's pointer) back to the caller,
2962 * since these pointers are valid. Only current stack
2963 * pointer will be invalid as soon as function exits,
2964 * but let's be conservative
2965 */
2966 verbose(env, "cannot return stack pointer to the caller\n");
2967 return -EINVAL;
2968 }
2969
2970 state->curframe--;
2971 caller = state->frame[state->curframe];
2972 /* return to the caller whatever r0 had in the callee */
2973 caller->regs[BPF_REG_0] = *r0;
2974
Joe Stringerfd978bf72018-10-02 13:35:35 -07002975 /* Transfer references to the caller */
2976 err = transfer_reference_state(caller, callee);
2977 if (err)
2978 return err;
2979
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002980 *insn_idx = callee->callsite + 1;
2981 if (env->log.level) {
2982 verbose(env, "returning from callee:\n");
2983 print_verifier_state(env, callee);
2984 verbose(env, "to caller at %d:\n", *insn_idx);
2985 print_verifier_state(env, caller);
2986 }
2987 /* clear everything in the callee */
2988 free_func_state(callee);
2989 state->frame[state->curframe + 1] = NULL;
2990 return 0;
2991}
2992
Yonghong Song849fa502018-04-28 22:28:09 -07002993static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
2994 int func_id,
2995 struct bpf_call_arg_meta *meta)
2996{
2997 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
2998
2999 if (ret_type != RET_INTEGER ||
3000 (func_id != BPF_FUNC_get_stack &&
3001 func_id != BPF_FUNC_probe_read_str))
3002 return;
3003
3004 ret_reg->smax_value = meta->msize_smax_value;
3005 ret_reg->umax_value = meta->msize_umax_value;
3006 __reg_deduce_bounds(ret_reg);
3007 __reg_bound_offset(ret_reg);
3008}
3009
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003010static int
3011record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3012 int func_id, int insn_idx)
3013{
3014 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
3015
3016 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02003017 func_id != BPF_FUNC_map_lookup_elem &&
3018 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003019 func_id != BPF_FUNC_map_delete_elem &&
3020 func_id != BPF_FUNC_map_push_elem &&
3021 func_id != BPF_FUNC_map_pop_elem &&
3022 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003023 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02003024
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003025 if (meta->map_ptr == NULL) {
3026 verbose(env, "kernel subsystem misconfigured verifier\n");
3027 return -EINVAL;
3028 }
3029
3030 if (!BPF_MAP_PTR(aux->map_state))
3031 bpf_map_ptr_store(aux, meta->map_ptr,
3032 meta->map_ptr->unpriv_array);
3033 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3034 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3035 meta->map_ptr->unpriv_array);
3036 return 0;
3037}
3038
Joe Stringerfd978bf72018-10-02 13:35:35 -07003039static int check_reference_leak(struct bpf_verifier_env *env)
3040{
3041 struct bpf_func_state *state = cur_func(env);
3042 int i;
3043
3044 for (i = 0; i < state->acquired_refs; i++) {
3045 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3046 state->refs[i].id, state->refs[i].insn_idx);
3047 }
3048 return state->acquired_refs ? -EINVAL : 0;
3049}
3050
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003051static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003052{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003053 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003054 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003055 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003056 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003057 int i, err;
3058
3059 /* find function prototype */
3060 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003061 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3062 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003063 return -EINVAL;
3064 }
3065
Jakub Kicinski00176a32017-10-16 16:40:54 -07003066 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003067 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003068 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003069 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3070 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003071 return -EINVAL;
3072 }
3073
3074 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003075 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02003076 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003077 return -EINVAL;
3078 }
3079
Daniel Borkmann04514d12017-12-14 21:07:25 +01003080 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08003081 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01003082 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3083 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3084 func_id_name(func_id), func_id);
3085 return -EINVAL;
3086 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003087
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003088 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003089 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003090
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003091 err = check_func_proto(fn, func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003092 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003093 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003094 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003095 return err;
3096 }
3097
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003098 meta.func_id = func_id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003099 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003100 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003101 if (err)
3102 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003103 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003104 if (err)
3105 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003106 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003107 if (err)
3108 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003109 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003110 if (err)
3111 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003112 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003113 if (err)
3114 return err;
3115
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003116 err = record_func_map(env, &meta, func_id, insn_idx);
3117 if (err)
3118 return err;
3119
Daniel Borkmann435faee12016-04-13 00:10:51 +02003120 /* Mark slots with STACK_MISC in case of raw mode, stack offset
3121 * is inferred from register state.
3122 */
3123 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01003124 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
3125 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003126 if (err)
3127 return err;
3128 }
3129
Joe Stringerfd978bf72018-10-02 13:35:35 -07003130 if (func_id == BPF_FUNC_tail_call) {
3131 err = check_reference_leak(env);
3132 if (err) {
3133 verbose(env, "tail_call would lead to reference leak\n");
3134 return err;
3135 }
3136 } else if (is_release_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003137 err = release_reference(env, meta.ref_obj_id);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003138 if (err) {
3139 verbose(env, "func %s#%d reference has not been acquired before\n",
3140 func_id_name(func_id), func_id);
Joe Stringerfd978bf72018-10-02 13:35:35 -07003141 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003142 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07003143 }
3144
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003145 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07003146
3147 /* check that flags argument in get_local_storage(map, flags) is 0,
3148 * this is required because get_local_storage() can't return an error.
3149 */
3150 if (func_id == BPF_FUNC_get_local_storage &&
3151 !register_is_null(&regs[BPF_REG_2])) {
3152 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
3153 return -EINVAL;
3154 }
3155
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003156 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01003157 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003158 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003159 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3160 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003161
Edward Creedc503a82017-08-15 20:34:35 +01003162 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003163 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01003164 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003165 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003166 } else if (fn->ret_type == RET_VOID) {
3167 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07003168 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
3169 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01003170 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003171 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003172 /* remember map_ptr, so that check_map_access()
3173 * can check 'value_size' boundary of memory access
3174 * to map element returned from bpf_map_lookup_elem()
3175 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003176 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003177 verbose(env,
3178 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003179 return -EINVAL;
3180 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003181 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003182 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3183 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08003184 if (map_value_has_spin_lock(meta.map_ptr))
3185 regs[BPF_REG_0].id = ++env->id_gen;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003186 } else {
3187 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
3188 regs[BPF_REG_0].id = ++env->id_gen;
3189 }
Joe Stringerc64b7982018-10-02 13:35:33 -07003190 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
3191 mark_reg_known_zero(env, regs, BPF_REG_0);
3192 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003193 regs[BPF_REG_0].id = ++env->id_gen;
Lorenz Bauer85a51f82019-03-22 09:54:00 +08003194 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
3195 mark_reg_known_zero(env, regs, BPF_REG_0);
3196 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
3197 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003198 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
3199 mark_reg_known_zero(env, regs, BPF_REG_0);
3200 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
3201 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003202 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003203 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003204 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003205 return -EINVAL;
3206 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003207
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003208 if (is_ptr_cast_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003209 /* For release_reference() */
3210 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003211 } else if (is_acquire_function(func_id)) {
3212 int id = acquire_reference_state(env, insn_idx);
3213
3214 if (id < 0)
3215 return id;
3216 /* For mark_ptr_or_null_reg() */
3217 regs[BPF_REG_0].id = id;
3218 /* For release_reference() */
3219 regs[BPF_REG_0].ref_obj_id = id;
3220 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003221
Yonghong Song849fa502018-04-28 22:28:09 -07003222 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
3223
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003224 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00003225 if (err)
3226 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003227
Yonghong Songc195651e2018-04-28 22:28:08 -07003228 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
3229 const char *err_str;
3230
3231#ifdef CONFIG_PERF_EVENTS
3232 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3233 err_str = "cannot get callchain buffer for func %s#%d\n";
3234#else
3235 err = -ENOTSUPP;
3236 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3237#endif
3238 if (err) {
3239 verbose(env, err_str, func_id_name(func_id), func_id);
3240 return err;
3241 }
3242
3243 env->prog->has_callchain_buf = true;
3244 }
3245
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003246 if (changes_data)
3247 clear_all_pkt_pointers(env);
3248 return 0;
3249}
3250
Edward Creeb03c9f92017-08-07 15:26:36 +01003251static bool signed_add_overflows(s64 a, s64 b)
3252{
3253 /* Do the add in u64, where overflow is well-defined */
3254 s64 res = (s64)((u64)a + (u64)b);
3255
3256 if (b < 0)
3257 return res > a;
3258 return res < a;
3259}
3260
3261static bool signed_sub_overflows(s64 a, s64 b)
3262{
3263 /* Do the sub in u64, where overflow is well-defined */
3264 s64 res = (s64)((u64)a - (u64)b);
3265
3266 if (b < 0)
3267 return res < a;
3268 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07003269}
3270
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003271static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3272 const struct bpf_reg_state *reg,
3273 enum bpf_reg_type type)
3274{
3275 bool known = tnum_is_const(reg->var_off);
3276 s64 val = reg->var_off.value;
3277 s64 smin = reg->smin_value;
3278
3279 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3280 verbose(env, "math between %s pointer and %lld is not allowed\n",
3281 reg_type_str[type], val);
3282 return false;
3283 }
3284
3285 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3286 verbose(env, "%s pointer offset %d is not allowed\n",
3287 reg_type_str[type], reg->off);
3288 return false;
3289 }
3290
3291 if (smin == S64_MIN) {
3292 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3293 reg_type_str[type]);
3294 return false;
3295 }
3296
3297 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3298 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3299 smin, reg_type_str[type]);
3300 return false;
3301 }
3302
3303 return true;
3304}
3305
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003306static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3307{
3308 return &env->insn_aux_data[env->insn_idx];
3309}
3310
3311static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3312 u32 *ptr_limit, u8 opcode, bool off_is_neg)
3313{
3314 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
3315 (opcode == BPF_SUB && !off_is_neg);
3316 u32 off;
3317
3318 switch (ptr_reg->type) {
3319 case PTR_TO_STACK:
3320 off = ptr_reg->off + ptr_reg->var_off.value;
3321 if (mask_to_left)
3322 *ptr_limit = MAX_BPF_STACK + off;
3323 else
3324 *ptr_limit = -off;
3325 return 0;
3326 case PTR_TO_MAP_VALUE:
3327 if (mask_to_left) {
3328 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3329 } else {
3330 off = ptr_reg->smin_value + ptr_reg->off;
3331 *ptr_limit = ptr_reg->map_ptr->value_size - off;
3332 }
3333 return 0;
3334 default:
3335 return -EINVAL;
3336 }
3337}
3338
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003339static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3340 const struct bpf_insn *insn)
3341{
3342 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3343}
3344
3345static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3346 u32 alu_state, u32 alu_limit)
3347{
3348 /* If we arrived here from different branches with different
3349 * state or limits to sanitize, then this won't work.
3350 */
3351 if (aux->alu_state &&
3352 (aux->alu_state != alu_state ||
3353 aux->alu_limit != alu_limit))
3354 return -EACCES;
3355
3356 /* Corresponding fixup done in fixup_bpf_calls(). */
3357 aux->alu_state = alu_state;
3358 aux->alu_limit = alu_limit;
3359 return 0;
3360}
3361
3362static int sanitize_val_alu(struct bpf_verifier_env *env,
3363 struct bpf_insn *insn)
3364{
3365 struct bpf_insn_aux_data *aux = cur_aux(env);
3366
3367 if (can_skip_alu_sanitation(env, insn))
3368 return 0;
3369
3370 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3371}
3372
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003373static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3374 struct bpf_insn *insn,
3375 const struct bpf_reg_state *ptr_reg,
3376 struct bpf_reg_state *dst_reg,
3377 bool off_is_neg)
3378{
3379 struct bpf_verifier_state *vstate = env->cur_state;
3380 struct bpf_insn_aux_data *aux = cur_aux(env);
3381 bool ptr_is_dst_reg = ptr_reg == dst_reg;
3382 u8 opcode = BPF_OP(insn->code);
3383 u32 alu_state, alu_limit;
3384 struct bpf_reg_state tmp;
3385 bool ret;
3386
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003387 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003388 return 0;
3389
3390 /* We already marked aux for masking from non-speculative
3391 * paths, thus we got here in the first place. We only care
3392 * to explore bad access from here.
3393 */
3394 if (vstate->speculative)
3395 goto do_sim;
3396
3397 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3398 alu_state |= ptr_is_dst_reg ?
3399 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3400
3401 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3402 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003403 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003404 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003405do_sim:
3406 /* Simulate and find potential out-of-bounds access under
3407 * speculative execution from truncation as a result of
3408 * masking when off was not within expected range. If off
3409 * sits in dst, then we temporarily need to move ptr there
3410 * to simulate dst (== 0) +/-= ptr. Needed, for example,
3411 * for cases where we use K-based arithmetic in one direction
3412 * and truncated reg-based in the other in order to explore
3413 * bad access.
3414 */
3415 if (!ptr_is_dst_reg) {
3416 tmp = *dst_reg;
3417 *dst_reg = *ptr_reg;
3418 }
3419 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
Xu Yu08032782019-03-21 18:00:35 +08003420 if (!ptr_is_dst_reg && ret)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003421 *dst_reg = tmp;
3422 return !ret ? -EFAULT : 0;
3423}
3424
Edward Creef1174f72017-08-07 15:26:19 +01003425/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01003426 * Caller should also handle BPF_MOV case separately.
3427 * If we return -EACCES, caller may want to try again treating pointer as a
3428 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
3429 */
3430static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3431 struct bpf_insn *insn,
3432 const struct bpf_reg_state *ptr_reg,
3433 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04003434{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003435 struct bpf_verifier_state *vstate = env->cur_state;
3436 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3437 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003438 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003439 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3440 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3441 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3442 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003443 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003444 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003445 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04003446
Edward Creef1174f72017-08-07 15:26:19 +01003447 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003448
Daniel Borkmann6f161012018-01-18 01:15:21 +01003449 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3450 smin_val > smax_val || umin_val > umax_val) {
3451 /* Taint dst register if offset had invalid bounds derived from
3452 * e.g. dead branches.
3453 */
3454 __mark_reg_unknown(dst_reg);
3455 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003456 }
3457
Edward Creef1174f72017-08-07 15:26:19 +01003458 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3459 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003460 verbose(env,
3461 "R%d 32-bit pointer arithmetic prohibited\n",
3462 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003463 return -EACCES;
3464 }
David S. Millerd1174412017-05-10 11:22:52 -07003465
Joe Stringeraad2eea2018-10-02 13:35:30 -07003466 switch (ptr_reg->type) {
3467 case PTR_TO_MAP_VALUE_OR_NULL:
3468 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3469 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003470 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003471 case CONST_PTR_TO_MAP:
3472 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003473 case PTR_TO_SOCKET:
3474 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003475 case PTR_TO_SOCK_COMMON:
3476 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003477 case PTR_TO_TCP_SOCK:
3478 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003479 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3480 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003481 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003482 case PTR_TO_MAP_VALUE:
3483 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3484 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3485 off_reg == dst_reg ? dst : src);
3486 return -EACCES;
3487 }
3488 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07003489 default:
3490 break;
Edward Creef1174f72017-08-07 15:26:19 +01003491 }
3492
3493 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3494 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003495 */
Edward Creef1174f72017-08-07 15:26:19 +01003496 dst_reg->type = ptr_reg->type;
3497 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003498
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003499 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3500 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3501 return -EINVAL;
3502
Josef Bacik48461132016-09-28 10:54:32 -04003503 switch (opcode) {
3504 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003505 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3506 if (ret < 0) {
3507 verbose(env, "R%d tried to add from different maps or paths\n", dst);
3508 return ret;
3509 }
Edward Creef1174f72017-08-07 15:26:19 +01003510 /* We can take a fixed offset as long as it doesn't overflow
3511 * the s32 'off' field
3512 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003513 if (known && (ptr_reg->off + smin_val ==
3514 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003515 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003516 dst_reg->smin_value = smin_ptr;
3517 dst_reg->smax_value = smax_ptr;
3518 dst_reg->umin_value = umin_ptr;
3519 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003520 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003521 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003522 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003523 break;
3524 }
Edward Creef1174f72017-08-07 15:26:19 +01003525 /* A new variable offset is created. Note that off_reg->off
3526 * == 0, since it's a scalar.
3527 * dst_reg gets the pointer type and since some positive
3528 * integer value was added to the pointer, give it a new 'id'
3529 * if it's a PTR_TO_PACKET.
3530 * this creates a new 'base' pointer, off_reg (variable) gets
3531 * added into the variable offset, and we copy the fixed offset
3532 * from ptr_reg.
3533 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003534 if (signed_add_overflows(smin_ptr, smin_val) ||
3535 signed_add_overflows(smax_ptr, smax_val)) {
3536 dst_reg->smin_value = S64_MIN;
3537 dst_reg->smax_value = S64_MAX;
3538 } else {
3539 dst_reg->smin_value = smin_ptr + smin_val;
3540 dst_reg->smax_value = smax_ptr + smax_val;
3541 }
3542 if (umin_ptr + umin_val < umin_ptr ||
3543 umax_ptr + umax_val < umax_ptr) {
3544 dst_reg->umin_value = 0;
3545 dst_reg->umax_value = U64_MAX;
3546 } else {
3547 dst_reg->umin_value = umin_ptr + umin_val;
3548 dst_reg->umax_value = umax_ptr + umax_val;
3549 }
Edward Creef1174f72017-08-07 15:26:19 +01003550 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3551 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003552 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003553 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003554 dst_reg->id = ++env->id_gen;
3555 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003556 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003557 }
Josef Bacik48461132016-09-28 10:54:32 -04003558 break;
3559 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003560 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3561 if (ret < 0) {
3562 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3563 return ret;
3564 }
Edward Creef1174f72017-08-07 15:26:19 +01003565 if (dst_reg == off_reg) {
3566 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003567 verbose(env, "R%d tried to subtract pointer from scalar\n",
3568 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003569 return -EACCES;
3570 }
3571 /* We don't allow subtraction from FP, because (according to
3572 * test_verifier.c test "invalid fp arithmetic", JITs might not
3573 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003574 */
Edward Creef1174f72017-08-07 15:26:19 +01003575 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003576 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3577 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003578 return -EACCES;
3579 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003580 if (known && (ptr_reg->off - smin_val ==
3581 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003582 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003583 dst_reg->smin_value = smin_ptr;
3584 dst_reg->smax_value = smax_ptr;
3585 dst_reg->umin_value = umin_ptr;
3586 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003587 dst_reg->var_off = ptr_reg->var_off;
3588 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003589 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003590 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003591 break;
3592 }
Edward Creef1174f72017-08-07 15:26:19 +01003593 /* A new variable offset is created. If the subtrahend is known
3594 * nonnegative, then any reg->range we had before is still good.
3595 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003596 if (signed_sub_overflows(smin_ptr, smax_val) ||
3597 signed_sub_overflows(smax_ptr, smin_val)) {
3598 /* Overflow possible, we know nothing */
3599 dst_reg->smin_value = S64_MIN;
3600 dst_reg->smax_value = S64_MAX;
3601 } else {
3602 dst_reg->smin_value = smin_ptr - smax_val;
3603 dst_reg->smax_value = smax_ptr - smin_val;
3604 }
3605 if (umin_ptr < umax_val) {
3606 /* Overflow possible, we know nothing */
3607 dst_reg->umin_value = 0;
3608 dst_reg->umax_value = U64_MAX;
3609 } else {
3610 /* Cannot overflow (as long as bounds are consistent) */
3611 dst_reg->umin_value = umin_ptr - umax_val;
3612 dst_reg->umax_value = umax_ptr - umin_val;
3613 }
Edward Creef1174f72017-08-07 15:26:19 +01003614 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3615 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003616 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003617 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003618 dst_reg->id = ++env->id_gen;
3619 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01003620 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01003621 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003622 }
3623 break;
3624 case BPF_AND:
3625 case BPF_OR:
3626 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003627 /* bitwise ops on pointers are troublesome, prohibit. */
3628 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3629 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003630 return -EACCES;
3631 default:
3632 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003633 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3634 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003635 return -EACCES;
3636 }
3637
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003638 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3639 return -EINVAL;
3640
Edward Creeb03c9f92017-08-07 15:26:36 +01003641 __update_reg_bounds(dst_reg);
3642 __reg_deduce_bounds(dst_reg);
3643 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003644
3645 /* For unprivileged we require that resulting offset must be in bounds
3646 * in order to be able to sanitize access later on.
3647 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01003648 if (!env->allow_ptr_leaks) {
3649 if (dst_reg->type == PTR_TO_MAP_VALUE &&
3650 check_map_access(env, dst, dst_reg->off, 1, false)) {
3651 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
3652 "prohibited for !root\n", dst);
3653 return -EACCES;
3654 } else if (dst_reg->type == PTR_TO_STACK &&
3655 check_stack_access(env, dst_reg, dst_reg->off +
3656 dst_reg->var_off.value, 1)) {
3657 verbose(env, "R%d stack pointer arithmetic goes out of range, "
3658 "prohibited for !root\n", dst);
3659 return -EACCES;
3660 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003661 }
3662
Edward Creef1174f72017-08-07 15:26:19 +01003663 return 0;
3664}
3665
Jann Horn468f6ea2017-12-18 20:11:56 -08003666/* WARNING: This function does calculations on 64-bit values, but the actual
3667 * execution may occur on 32-bit values. Therefore, things like bitshifts
3668 * need extra checks in the 32-bit case.
3669 */
Edward Creef1174f72017-08-07 15:26:19 +01003670static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3671 struct bpf_insn *insn,
3672 struct bpf_reg_state *dst_reg,
3673 struct bpf_reg_state src_reg)
3674{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003675 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01003676 u8 opcode = BPF_OP(insn->code);
3677 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01003678 s64 smin_val, smax_val;
3679 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08003680 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003681 u32 dst = insn->dst_reg;
3682 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01003683
Jann Hornb7992072018-10-05 18:17:59 +02003684 if (insn_bitness == 32) {
3685 /* Relevant for 32-bit RSH: Information can propagate towards
3686 * LSB, so it isn't sufficient to only truncate the output to
3687 * 32 bits.
3688 */
3689 coerce_reg_to_size(dst_reg, 4);
3690 coerce_reg_to_size(&src_reg, 4);
3691 }
3692
Edward Creeb03c9f92017-08-07 15:26:36 +01003693 smin_val = src_reg.smin_value;
3694 smax_val = src_reg.smax_value;
3695 umin_val = src_reg.umin_value;
3696 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003697 src_known = tnum_is_const(src_reg.var_off);
3698 dst_known = tnum_is_const(dst_reg->var_off);
3699
Daniel Borkmann6f161012018-01-18 01:15:21 +01003700 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3701 smin_val > smax_val || umin_val > umax_val) {
3702 /* Taint dst register if offset had invalid bounds derived from
3703 * e.g. dead branches.
3704 */
3705 __mark_reg_unknown(dst_reg);
3706 return 0;
3707 }
3708
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003709 if (!src_known &&
3710 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3711 __mark_reg_unknown(dst_reg);
3712 return 0;
3713 }
3714
Edward Creef1174f72017-08-07 15:26:19 +01003715 switch (opcode) {
3716 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003717 ret = sanitize_val_alu(env, insn);
3718 if (ret < 0) {
3719 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3720 return ret;
3721 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003722 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3723 signed_add_overflows(dst_reg->smax_value, smax_val)) {
3724 dst_reg->smin_value = S64_MIN;
3725 dst_reg->smax_value = S64_MAX;
3726 } else {
3727 dst_reg->smin_value += smin_val;
3728 dst_reg->smax_value += smax_val;
3729 }
3730 if (dst_reg->umin_value + umin_val < umin_val ||
3731 dst_reg->umax_value + umax_val < umax_val) {
3732 dst_reg->umin_value = 0;
3733 dst_reg->umax_value = U64_MAX;
3734 } else {
3735 dst_reg->umin_value += umin_val;
3736 dst_reg->umax_value += umax_val;
3737 }
Edward Creef1174f72017-08-07 15:26:19 +01003738 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3739 break;
3740 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003741 ret = sanitize_val_alu(env, insn);
3742 if (ret < 0) {
3743 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3744 return ret;
3745 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003746 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3747 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3748 /* Overflow possible, we know nothing */
3749 dst_reg->smin_value = S64_MIN;
3750 dst_reg->smax_value = S64_MAX;
3751 } else {
3752 dst_reg->smin_value -= smax_val;
3753 dst_reg->smax_value -= smin_val;
3754 }
3755 if (dst_reg->umin_value < umax_val) {
3756 /* Overflow possible, we know nothing */
3757 dst_reg->umin_value = 0;
3758 dst_reg->umax_value = U64_MAX;
3759 } else {
3760 /* Cannot overflow (as long as bounds are consistent) */
3761 dst_reg->umin_value -= umax_val;
3762 dst_reg->umax_value -= umin_val;
3763 }
Edward Creef1174f72017-08-07 15:26:19 +01003764 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04003765 break;
3766 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01003767 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3768 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003769 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01003770 __mark_reg_unbounded(dst_reg);
3771 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003772 break;
3773 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003774 /* Both values are positive, so we can work with unsigned and
3775 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01003776 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003777 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3778 /* Potential overflow, we know nothing */
3779 __mark_reg_unbounded(dst_reg);
3780 /* (except what we can learn from the var_off) */
3781 __update_reg_bounds(dst_reg);
3782 break;
3783 }
3784 dst_reg->umin_value *= umin_val;
3785 dst_reg->umax_value *= umax_val;
3786 if (dst_reg->umax_value > S64_MAX) {
3787 /* Overflow possible, we know nothing */
3788 dst_reg->smin_value = S64_MIN;
3789 dst_reg->smax_value = S64_MAX;
3790 } else {
3791 dst_reg->smin_value = dst_reg->umin_value;
3792 dst_reg->smax_value = dst_reg->umax_value;
3793 }
Josef Bacik48461132016-09-28 10:54:32 -04003794 break;
3795 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01003796 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003797 __mark_reg_known(dst_reg, dst_reg->var_off.value &
3798 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003799 break;
3800 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003801 /* We get our minimum from the var_off, since that's inherently
3802 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05003803 */
Edward Creef1174f72017-08-07 15:26:19 +01003804 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003805 dst_reg->umin_value = dst_reg->var_off.value;
3806 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3807 if (dst_reg->smin_value < 0 || smin_val < 0) {
3808 /* Lose signed bounds when ANDing negative numbers,
3809 * ain't nobody got time for that.
3810 */
3811 dst_reg->smin_value = S64_MIN;
3812 dst_reg->smax_value = S64_MAX;
3813 } else {
3814 /* ANDing two positives gives a positive, so safe to
3815 * cast result into s64.
3816 */
3817 dst_reg->smin_value = dst_reg->umin_value;
3818 dst_reg->smax_value = dst_reg->umax_value;
3819 }
3820 /* We may learn something more from the var_off */
3821 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003822 break;
3823 case BPF_OR:
3824 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003825 __mark_reg_known(dst_reg, dst_reg->var_off.value |
3826 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003827 break;
3828 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003829 /* We get our maximum from the var_off, and our minimum is the
3830 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01003831 */
3832 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003833 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
3834 dst_reg->umax_value = dst_reg->var_off.value |
3835 dst_reg->var_off.mask;
3836 if (dst_reg->smin_value < 0 || smin_val < 0) {
3837 /* Lose signed bounds when ORing negative numbers,
3838 * ain't nobody got time for that.
3839 */
3840 dst_reg->smin_value = S64_MIN;
3841 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01003842 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003843 /* ORing two positives gives a positive, so safe to
3844 * cast result into s64.
3845 */
3846 dst_reg->smin_value = dst_reg->umin_value;
3847 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003848 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003849 /* We may learn something more from the var_off */
3850 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003851 break;
3852 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003853 if (umax_val >= insn_bitness) {
3854 /* Shifts greater than 31 or 63 are undefined.
3855 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003856 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003857 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003858 break;
3859 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003860 /* We lose all sign bit information (except what we can pick
3861 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04003862 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003863 dst_reg->smin_value = S64_MIN;
3864 dst_reg->smax_value = S64_MAX;
3865 /* If we might shift our top bit out, then we know nothing */
3866 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
3867 dst_reg->umin_value = 0;
3868 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07003869 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003870 dst_reg->umin_value <<= umin_val;
3871 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07003872 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07003873 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003874 /* We may learn something more from the var_off */
3875 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003876 break;
3877 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003878 if (umax_val >= insn_bitness) {
3879 /* Shifts greater than 31 or 63 are undefined.
3880 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003881 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003882 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003883 break;
3884 }
Edward Cree4374f252017-12-18 20:11:53 -08003885 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
3886 * be negative, then either:
3887 * 1) src_reg might be zero, so the sign bit of the result is
3888 * unknown, so we lose our signed bounds
3889 * 2) it's known negative, thus the unsigned bounds capture the
3890 * signed bounds
3891 * 3) the signed bounds cross zero, so they tell us nothing
3892 * about the result
3893 * If the value in dst_reg is known nonnegative, then again the
3894 * unsigned bounts capture the signed bounds.
3895 * Thus, in all cases it suffices to blow away our signed bounds
3896 * and rely on inferring new ones from the unsigned bounds and
3897 * var_off of the result.
3898 */
3899 dst_reg->smin_value = S64_MIN;
3900 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07003901 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003902 dst_reg->umin_value >>= umax_val;
3903 dst_reg->umax_value >>= umin_val;
3904 /* We may learn something more from the var_off */
3905 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003906 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07003907 case BPF_ARSH:
3908 if (umax_val >= insn_bitness) {
3909 /* Shifts greater than 31 or 63 are undefined.
3910 * This includes shifts by a negative number.
3911 */
3912 mark_reg_unknown(env, regs, insn->dst_reg);
3913 break;
3914 }
3915
3916 /* Upon reaching here, src_known is true and
3917 * umax_val is equal to umin_val.
3918 */
3919 dst_reg->smin_value >>= umin_val;
3920 dst_reg->smax_value >>= umin_val;
3921 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
3922
3923 /* blow away the dst_reg umin_value/umax_value and rely on
3924 * dst_reg var_off to refine the result.
3925 */
3926 dst_reg->umin_value = 0;
3927 dst_reg->umax_value = U64_MAX;
3928 __update_reg_bounds(dst_reg);
3929 break;
Josef Bacik48461132016-09-28 10:54:32 -04003930 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003931 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003932 break;
3933 }
3934
Jann Horn468f6ea2017-12-18 20:11:56 -08003935 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3936 /* 32-bit ALU ops are (32,32)->32 */
3937 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08003938 }
3939
Edward Creeb03c9f92017-08-07 15:26:36 +01003940 __reg_deduce_bounds(dst_reg);
3941 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003942 return 0;
3943}
3944
3945/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
3946 * and var_off.
3947 */
3948static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
3949 struct bpf_insn *insn)
3950{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003951 struct bpf_verifier_state *vstate = env->cur_state;
3952 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3953 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003954 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
3955 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01003956
3957 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003958 src_reg = NULL;
3959 if (dst_reg->type != SCALAR_VALUE)
3960 ptr_reg = dst_reg;
3961 if (BPF_SRC(insn->code) == BPF_X) {
3962 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003963 if (src_reg->type != SCALAR_VALUE) {
3964 if (dst_reg->type != SCALAR_VALUE) {
3965 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003966 * an arbitrary scalar. Disallow all math except
3967 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01003968 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07003969 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003970 mark_reg_unknown(env, regs, insn->dst_reg);
3971 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01003972 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003973 verbose(env, "R%d pointer %s pointer prohibited\n",
3974 insn->dst_reg,
3975 bpf_alu_string[opcode >> 4]);
3976 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01003977 } else {
3978 /* scalar += pointer
3979 * This is legal, but we have to reverse our
3980 * src/dest handling in computing the range
3981 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003982 return adjust_ptr_min_max_vals(env, insn,
3983 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003984 }
3985 } else if (ptr_reg) {
3986 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003987 return adjust_ptr_min_max_vals(env, insn,
3988 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003989 }
3990 } else {
3991 /* Pretend the src is a reg with a known value, since we only
3992 * need to be able to read from this state.
3993 */
3994 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003995 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01003996 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003997 if (ptr_reg) /* pointer += K */
3998 return adjust_ptr_min_max_vals(env, insn,
3999 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004000 }
4001
4002 /* Got here implies adding two SCALAR_VALUEs */
4003 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004004 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004005 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004006 return -EINVAL;
4007 }
4008 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004009 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004010 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004011 return -EINVAL;
4012 }
4013 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004014}
4015
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004016/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004017static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004018{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004019 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004020 u8 opcode = BPF_OP(insn->code);
4021 int err;
4022
4023 if (opcode == BPF_END || opcode == BPF_NEG) {
4024 if (opcode == BPF_NEG) {
4025 if (BPF_SRC(insn->code) != 0 ||
4026 insn->src_reg != BPF_REG_0 ||
4027 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004028 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004029 return -EINVAL;
4030 }
4031 } else {
4032 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01004033 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4034 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004035 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004036 return -EINVAL;
4037 }
4038 }
4039
4040 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004041 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004042 if (err)
4043 return err;
4044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004045 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004046 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004047 insn->dst_reg);
4048 return -EACCES;
4049 }
4050
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004051 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004052 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004053 if (err)
4054 return err;
4055
4056 } else if (opcode == BPF_MOV) {
4057
4058 if (BPF_SRC(insn->code) == BPF_X) {
4059 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004060 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004061 return -EINVAL;
4062 }
4063
4064 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004065 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004066 if (err)
4067 return err;
4068 } else {
4069 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004070 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004071 return -EINVAL;
4072 }
4073 }
4074
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004075 /* check dest operand, mark as required later */
4076 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004077 if (err)
4078 return err;
4079
4080 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05004081 struct bpf_reg_state *src_reg = regs + insn->src_reg;
4082 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4083
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004084 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4085 /* case: R1 = R2
4086 * copy register state to dest reg
4087 */
Jiong Wange434b8c2018-12-07 12:16:18 -05004088 *dst_reg = *src_reg;
4089 dst_reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004090 } else {
Edward Creef1174f72017-08-07 15:26:19 +01004091 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004092 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004093 verbose(env,
4094 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004095 insn->src_reg);
4096 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05004097 } else if (src_reg->type == SCALAR_VALUE) {
4098 *dst_reg = *src_reg;
4099 dst_reg->live |= REG_LIVE_WRITTEN;
4100 } else {
4101 mark_reg_unknown(env, regs,
4102 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004103 }
Jiong Wange434b8c2018-12-07 12:16:18 -05004104 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004105 }
4106 } else {
4107 /* case: R = imm
4108 * remember the value we stored into this reg
4109 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004110 /* clear any state __mark_reg_known doesn't set */
4111 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004112 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08004113 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4114 __mark_reg_known(regs + insn->dst_reg,
4115 insn->imm);
4116 } else {
4117 __mark_reg_known(regs + insn->dst_reg,
4118 (u32)insn->imm);
4119 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004120 }
4121
4122 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004123 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004124 return -EINVAL;
4125
4126 } else { /* all other ALU ops: and, sub, xor, add, ... */
4127
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004128 if (BPF_SRC(insn->code) == BPF_X) {
4129 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004130 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004131 return -EINVAL;
4132 }
4133 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004134 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004135 if (err)
4136 return err;
4137 } else {
4138 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004139 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004140 return -EINVAL;
4141 }
4142 }
4143
4144 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004145 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004146 if (err)
4147 return err;
4148
4149 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
4150 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004151 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004152 return -EINVAL;
4153 }
4154
Rabin Vincent229394e82016-01-12 20:17:08 +01004155 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
4156 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
4157 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
4158
4159 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004160 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01004161 return -EINVAL;
4162 }
4163 }
4164
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004165 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004166 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004167 if (err)
4168 return err;
4169
Edward Creef1174f72017-08-07 15:26:19 +01004170 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004171 }
4172
4173 return 0;
4174}
4175
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004176static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004177 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01004178 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004179 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004180{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004181 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004182 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004183 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004184 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004185
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004186 if (dst_reg->off < 0 ||
4187 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01004188 /* This doesn't give us any range */
4189 return;
4190
Edward Creeb03c9f92017-08-07 15:26:36 +01004191 if (dst_reg->umax_value > MAX_PACKET_OFF ||
4192 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01004193 /* Risk of overflow. For instance, ptr + (1<<63) may be less
4194 * than pkt_end, but that's because it's also less than pkt.
4195 */
4196 return;
4197
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004198 new_range = dst_reg->off;
4199 if (range_right_open)
4200 new_range--;
4201
4202 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004203 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004204 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004205 *
4206 * r2 = r3;
4207 * r2 += 8;
4208 * if (r2 > pkt_end) goto <handle exception>
4209 * <access okay>
4210 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004211 * r2 = r3;
4212 * r2 += 8;
4213 * if (r2 < pkt_end) goto <access okay>
4214 * <handle exception>
4215 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004216 * Where:
4217 * r2 == dst_reg, pkt_end == src_reg
4218 * r2=pkt(id=n,off=8,r=0)
4219 * r3=pkt(id=n,off=0,r=0)
4220 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004221 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004222 *
4223 * r2 = r3;
4224 * r2 += 8;
4225 * if (pkt_end >= r2) goto <access okay>
4226 * <handle exception>
4227 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004228 * r2 = r3;
4229 * r2 += 8;
4230 * if (pkt_end <= r2) goto <handle exception>
4231 * <access okay>
4232 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004233 * Where:
4234 * pkt_end == dst_reg, r2 == src_reg
4235 * r2=pkt(id=n,off=8,r=0)
4236 * r3=pkt(id=n,off=0,r=0)
4237 *
4238 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004239 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4240 * and [r3, r3 + 8-1) respectively is safe to access depending on
4241 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004242 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004243
Edward Creef1174f72017-08-07 15:26:19 +01004244 /* If our ids match, then we must have the same max_value. And we
4245 * don't care about the other reg's fixed offset, since if it's too big
4246 * the range won't allow anything.
4247 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4248 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004249 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004250 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07004251 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004252 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004253
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004254 for (j = 0; j <= vstate->curframe; j++) {
4255 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004256 bpf_for_each_spilled_reg(i, state, reg) {
4257 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004258 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004259 if (reg->type == type && reg->id == dst_reg->id)
4260 reg->range = max(reg->range, new_range);
4261 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004262 }
4263}
4264
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004265/* compute branch direction of the expression "if (reg opcode val) goto target;"
4266 * and return:
4267 * 1 - branch will be taken and "goto target" will be executed
4268 * 0 - branch will not be taken and fall-through to next insn
4269 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4270 */
Jiong Wang092ed092019-01-26 12:26:01 -05004271static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4272 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004273{
Jiong Wang092ed092019-01-26 12:26:01 -05004274 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004275 s64 sval;
4276
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004277 if (__is_pointer_value(false, reg))
4278 return -1;
4279
Jiong Wang092ed092019-01-26 12:26:01 -05004280 if (is_jmp32) {
4281 reg_lo = *reg;
4282 reg = &reg_lo;
4283 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4284 * could truncate high bits and update umin/umax according to
4285 * information of low bits.
4286 */
4287 coerce_reg_to_size(reg, 4);
4288 /* smin/smax need special handling. For example, after coerce,
4289 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4290 * used as operand to JMP32. It is a negative number from s32's
4291 * point of view, while it is a positive number when seen as
4292 * s64. The smin/smax are kept as s64, therefore, when used with
4293 * JMP32, they need to be transformed into s32, then sign
4294 * extended back to s64.
4295 *
4296 * Also, smin/smax were copied from umin/umax. If umin/umax has
4297 * different sign bit, then min/max relationship doesn't
4298 * maintain after casting into s32, for this case, set smin/smax
4299 * to safest range.
4300 */
4301 if ((reg->umax_value ^ reg->umin_value) &
4302 (1ULL << 31)) {
4303 reg->smin_value = S32_MIN;
4304 reg->smax_value = S32_MAX;
4305 }
4306 reg->smin_value = (s64)(s32)reg->smin_value;
4307 reg->smax_value = (s64)(s32)reg->smax_value;
4308
4309 val = (u32)val;
4310 sval = (s64)(s32)val;
4311 } else {
4312 sval = (s64)val;
4313 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004314
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004315 switch (opcode) {
4316 case BPF_JEQ:
4317 if (tnum_is_const(reg->var_off))
4318 return !!tnum_equals_const(reg->var_off, val);
4319 break;
4320 case BPF_JNE:
4321 if (tnum_is_const(reg->var_off))
4322 return !tnum_equals_const(reg->var_off, val);
4323 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08004324 case BPF_JSET:
4325 if ((~reg->var_off.mask & reg->var_off.value) & val)
4326 return 1;
4327 if (!((reg->var_off.mask | reg->var_off.value) & val))
4328 return 0;
4329 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004330 case BPF_JGT:
4331 if (reg->umin_value > val)
4332 return 1;
4333 else if (reg->umax_value <= val)
4334 return 0;
4335 break;
4336 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004337 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004338 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004339 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004340 return 0;
4341 break;
4342 case BPF_JLT:
4343 if (reg->umax_value < val)
4344 return 1;
4345 else if (reg->umin_value >= val)
4346 return 0;
4347 break;
4348 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004349 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004350 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004351 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004352 return 0;
4353 break;
4354 case BPF_JGE:
4355 if (reg->umin_value >= val)
4356 return 1;
4357 else if (reg->umax_value < val)
4358 return 0;
4359 break;
4360 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004361 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004362 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004363 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004364 return 0;
4365 break;
4366 case BPF_JLE:
4367 if (reg->umax_value <= val)
4368 return 1;
4369 else if (reg->umin_value > val)
4370 return 0;
4371 break;
4372 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004373 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004374 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004375 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004376 return 0;
4377 break;
4378 }
4379
4380 return -1;
4381}
4382
Jiong Wang092ed092019-01-26 12:26:01 -05004383/* Generate min value of the high 32-bit from TNUM info. */
4384static u64 gen_hi_min(struct tnum var)
4385{
4386 return var.value & ~0xffffffffULL;
4387}
4388
4389/* Generate max value of the high 32-bit from TNUM info. */
4390static u64 gen_hi_max(struct tnum var)
4391{
4392 return (var.value | var.mask) & ~0xffffffffULL;
4393}
4394
4395/* Return true if VAL is compared with a s64 sign extended from s32, and they
4396 * are with the same signedness.
4397 */
4398static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4399{
4400 return ((s32)sval >= 0 &&
4401 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4402 ((s32)sval < 0 &&
4403 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4404}
4405
Josef Bacik48461132016-09-28 10:54:32 -04004406/* Adjusts the register min/max values in the case that the dst_reg is the
4407 * variable register that we are working on, and src_reg is a constant or we're
4408 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01004409 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04004410 */
4411static void reg_set_min_max(struct bpf_reg_state *true_reg,
4412 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004413 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004414{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004415 s64 sval;
4416
Edward Creef1174f72017-08-07 15:26:19 +01004417 /* If the dst_reg is a pointer, we can't learn anything about its
4418 * variable offset from the compare (unless src_reg were a pointer into
4419 * the same object, but we don't bother with that.
4420 * Since false_reg and true_reg have the same type by construction, we
4421 * only need to check one of them for pointerness.
4422 */
4423 if (__is_pointer_value(false, false_reg))
4424 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004425
Jiong Wang092ed092019-01-26 12:26:01 -05004426 val = is_jmp32 ? (u32)val : val;
4427 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004428
Josef Bacik48461132016-09-28 10:54:32 -04004429 switch (opcode) {
4430 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004431 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004432 {
4433 struct bpf_reg_state *reg =
4434 opcode == BPF_JEQ ? true_reg : false_reg;
4435
4436 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4437 * if it is true we know the value for sure. Likewise for
4438 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04004439 */
Jiong Wang092ed092019-01-26 12:26:01 -05004440 if (is_jmp32) {
4441 u64 old_v = reg->var_off.value;
4442 u64 hi_mask = ~0xffffffffULL;
4443
4444 reg->var_off.value = (old_v & hi_mask) | val;
4445 reg->var_off.mask &= hi_mask;
4446 } else {
4447 __mark_reg_known(reg, val);
4448 }
Josef Bacik48461132016-09-28 10:54:32 -04004449 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004450 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004451 case BPF_JSET:
4452 false_reg->var_off = tnum_and(false_reg->var_off,
4453 tnum_const(~val));
4454 if (is_power_of_2(val))
4455 true_reg->var_off = tnum_or(true_reg->var_off,
4456 tnum_const(val));
4457 break;
Josef Bacik48461132016-09-28 10:54:32 -04004458 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004459 case BPF_JGT:
4460 {
4461 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
4462 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4463
Jiong Wang092ed092019-01-26 12:26:01 -05004464 if (is_jmp32) {
4465 false_umax += gen_hi_max(false_reg->var_off);
4466 true_umin += gen_hi_min(true_reg->var_off);
4467 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004468 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4469 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01004470 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004471 }
Josef Bacik48461132016-09-28 10:54:32 -04004472 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004473 case BPF_JSGT:
4474 {
4475 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
4476 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4477
Jiong Wang092ed092019-01-26 12:26:01 -05004478 /* If the full s64 was not sign-extended from s32 then don't
4479 * deduct further info.
4480 */
4481 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4482 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004483 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4484 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04004485 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004486 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004487 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004488 case BPF_JLT:
4489 {
4490 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
4491 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4492
Jiong Wang092ed092019-01-26 12:26:01 -05004493 if (is_jmp32) {
4494 false_umin += gen_hi_min(false_reg->var_off);
4495 true_umax += gen_hi_max(true_reg->var_off);
4496 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004497 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4498 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004499 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004500 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004501 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004502 case BPF_JSLT:
4503 {
4504 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
4505 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4506
Jiong Wang092ed092019-01-26 12:26:01 -05004507 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4508 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004509 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4510 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004511 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004512 }
Josef Bacik48461132016-09-28 10:54:32 -04004513 default:
4514 break;
4515 }
4516
Edward Creeb03c9f92017-08-07 15:26:36 +01004517 __reg_deduce_bounds(false_reg);
4518 __reg_deduce_bounds(true_reg);
4519 /* We might have learned some bits from the bounds. */
4520 __reg_bound_offset(false_reg);
4521 __reg_bound_offset(true_reg);
4522 /* Intersecting with the old var_off might have improved our bounds
4523 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4524 * then new var_off is (0; 0x7f...fc) which improves our umax.
4525 */
4526 __update_reg_bounds(false_reg);
4527 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004528}
4529
Edward Creef1174f72017-08-07 15:26:19 +01004530/* Same as above, but for the case that dst_reg holds a constant and src_reg is
4531 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04004532 */
4533static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4534 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004535 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004536{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004537 s64 sval;
4538
Edward Creef1174f72017-08-07 15:26:19 +01004539 if (__is_pointer_value(false, false_reg))
4540 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004541
Jiong Wang092ed092019-01-26 12:26:01 -05004542 val = is_jmp32 ? (u32)val : val;
4543 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004544
Josef Bacik48461132016-09-28 10:54:32 -04004545 switch (opcode) {
4546 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004547 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004548 {
4549 struct bpf_reg_state *reg =
4550 opcode == BPF_JEQ ? true_reg : false_reg;
4551
Jiong Wang092ed092019-01-26 12:26:01 -05004552 if (is_jmp32) {
4553 u64 old_v = reg->var_off.value;
4554 u64 hi_mask = ~0xffffffffULL;
4555
4556 reg->var_off.value = (old_v & hi_mask) | val;
4557 reg->var_off.mask &= hi_mask;
4558 } else {
4559 __mark_reg_known(reg, val);
4560 }
Josef Bacik48461132016-09-28 10:54:32 -04004561 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004562 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004563 case BPF_JSET:
4564 false_reg->var_off = tnum_and(false_reg->var_off,
4565 tnum_const(~val));
4566 if (is_power_of_2(val))
4567 true_reg->var_off = tnum_or(true_reg->var_off,
4568 tnum_const(val));
4569 break;
Josef Bacik48461132016-09-28 10:54:32 -04004570 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004571 case BPF_JGT:
4572 {
4573 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
4574 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4575
Jiong Wang092ed092019-01-26 12:26:01 -05004576 if (is_jmp32) {
4577 false_umin += gen_hi_min(false_reg->var_off);
4578 true_umax += gen_hi_max(true_reg->var_off);
4579 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004580 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4581 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01004582 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004583 }
Josef Bacik48461132016-09-28 10:54:32 -04004584 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004585 case BPF_JSGT:
4586 {
4587 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
4588 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
4589
Jiong Wang092ed092019-01-26 12:26:01 -05004590 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4591 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004592 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4593 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04004594 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004595 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004596 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004597 case BPF_JLT:
4598 {
4599 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
4600 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
4601
Jiong Wang092ed092019-01-26 12:26:01 -05004602 if (is_jmp32) {
4603 false_umax += gen_hi_max(false_reg->var_off);
4604 true_umin += gen_hi_min(true_reg->var_off);
4605 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004606 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4607 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004608 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004609 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004610 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004611 case BPF_JSLT:
4612 {
4613 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
4614 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
4615
Jiong Wang092ed092019-01-26 12:26:01 -05004616 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4617 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004618 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4619 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004620 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004621 }
Josef Bacik48461132016-09-28 10:54:32 -04004622 default:
4623 break;
4624 }
4625
Edward Creeb03c9f92017-08-07 15:26:36 +01004626 __reg_deduce_bounds(false_reg);
4627 __reg_deduce_bounds(true_reg);
4628 /* We might have learned some bits from the bounds. */
4629 __reg_bound_offset(false_reg);
4630 __reg_bound_offset(true_reg);
4631 /* Intersecting with the old var_off might have improved our bounds
4632 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4633 * then new var_off is (0; 0x7f...fc) which improves our umax.
4634 */
4635 __update_reg_bounds(false_reg);
4636 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004637}
4638
4639/* Regs are known to be equal, so intersect their min/max/var_off */
4640static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
4641 struct bpf_reg_state *dst_reg)
4642{
Edward Creeb03c9f92017-08-07 15:26:36 +01004643 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
4644 dst_reg->umin_value);
4645 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
4646 dst_reg->umax_value);
4647 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
4648 dst_reg->smin_value);
4649 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
4650 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01004651 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
4652 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004653 /* We might have learned new bounds from the var_off. */
4654 __update_reg_bounds(src_reg);
4655 __update_reg_bounds(dst_reg);
4656 /* We might have learned something about the sign bit. */
4657 __reg_deduce_bounds(src_reg);
4658 __reg_deduce_bounds(dst_reg);
4659 /* We might have learned some bits from the bounds. */
4660 __reg_bound_offset(src_reg);
4661 __reg_bound_offset(dst_reg);
4662 /* Intersecting with the old var_off might have improved our bounds
4663 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4664 * then new var_off is (0; 0x7f...fc) which improves our umax.
4665 */
4666 __update_reg_bounds(src_reg);
4667 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004668}
4669
4670static void reg_combine_min_max(struct bpf_reg_state *true_src,
4671 struct bpf_reg_state *true_dst,
4672 struct bpf_reg_state *false_src,
4673 struct bpf_reg_state *false_dst,
4674 u8 opcode)
4675{
4676 switch (opcode) {
4677 case BPF_JEQ:
4678 __reg_combine_min_max(true_src, true_dst);
4679 break;
4680 case BPF_JNE:
4681 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01004682 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004683 }
Josef Bacik48461132016-09-28 10:54:32 -04004684}
4685
Joe Stringerfd978bf72018-10-02 13:35:35 -07004686static void mark_ptr_or_null_reg(struct bpf_func_state *state,
4687 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07004688 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004689{
Joe Stringer840b9612018-10-02 13:35:32 -07004690 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01004691 /* Old offset (both fixed and variable parts) should
4692 * have been known-zero, because we don't allow pointer
4693 * arithmetic on pointers that might be NULL.
4694 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004695 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
4696 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01004697 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004698 __mark_reg_known_zero(reg);
4699 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004700 }
4701 if (is_null) {
4702 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07004703 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
4704 if (reg->map_ptr->inner_map_meta) {
4705 reg->type = CONST_PTR_TO_MAP;
4706 reg->map_ptr = reg->map_ptr->inner_map_meta;
4707 } else {
4708 reg->type = PTR_TO_MAP_VALUE;
4709 }
Joe Stringerc64b7982018-10-02 13:35:33 -07004710 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
4711 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08004712 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
4713 reg->type = PTR_TO_SOCK_COMMON;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08004714 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
4715 reg->type = PTR_TO_TCP_SOCK;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004716 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004717 if (is_null) {
4718 /* We don't need id and ref_obj_id from this point
4719 * onwards anymore, thus we should better reset it,
4720 * so that state pruning has chances to take effect.
4721 */
4722 reg->id = 0;
4723 reg->ref_obj_id = 0;
4724 } else if (!reg_may_point_to_spin_lock(reg)) {
4725 /* For not-NULL ptr, reg->ref_obj_id will be reset
4726 * in release_reg_references().
4727 *
4728 * reg->id is still used by spin_lock ptr. Other
4729 * than spin_lock ptr type, reg->id can be reset.
Joe Stringerfd978bf72018-10-02 13:35:35 -07004730 */
4731 reg->id = 0;
4732 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004733 }
4734}
4735
4736/* The logic is similar to find_good_pkt_pointers(), both could eventually
4737 * be folded together at some point.
4738 */
Joe Stringer840b9612018-10-02 13:35:32 -07004739static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4740 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004741{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004742 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Joe Stringerf3709f62018-10-02 13:35:29 -07004743 struct bpf_reg_state *reg, *regs = state->regs;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004744 u32 ref_obj_id = regs[regno].ref_obj_id;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01004745 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004746 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02004747
Martin KaFai Lau1b986582019-03-12 10:23:02 -07004748 if (ref_obj_id && ref_obj_id == id && is_null)
4749 /* regs[regno] is in the " == NULL" branch.
4750 * No one could have freed the reference state before
4751 * doing the NULL check.
4752 */
4753 WARN_ON_ONCE(release_reference_state(state, id));
Joe Stringerfd978bf72018-10-02 13:35:35 -07004754
Thomas Graf57a09bf2016-10-18 19:51:19 +02004755 for (i = 0; i < MAX_BPF_REG; i++)
Joe Stringerfd978bf72018-10-02 13:35:35 -07004756 mark_ptr_or_null_reg(state, &regs[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02004757
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004758 for (j = 0; j <= vstate->curframe; j++) {
4759 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004760 bpf_for_each_spilled_reg(i, state, reg) {
4761 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004762 continue;
Joe Stringerfd978bf72018-10-02 13:35:35 -07004763 mark_ptr_or_null_reg(state, reg, id, is_null);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004764 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004765 }
4766}
4767
Daniel Borkmann5beca082017-11-01 23:58:10 +01004768static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4769 struct bpf_reg_state *dst_reg,
4770 struct bpf_reg_state *src_reg,
4771 struct bpf_verifier_state *this_branch,
4772 struct bpf_verifier_state *other_branch)
4773{
4774 if (BPF_SRC(insn->code) != BPF_X)
4775 return false;
4776
Jiong Wang092ed092019-01-26 12:26:01 -05004777 /* Pointers are always 64-bit. */
4778 if (BPF_CLASS(insn->code) == BPF_JMP32)
4779 return false;
4780
Daniel Borkmann5beca082017-11-01 23:58:10 +01004781 switch (BPF_OP(insn->code)) {
4782 case BPF_JGT:
4783 if ((dst_reg->type == PTR_TO_PACKET &&
4784 src_reg->type == PTR_TO_PACKET_END) ||
4785 (dst_reg->type == PTR_TO_PACKET_META &&
4786 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4787 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4788 find_good_pkt_pointers(this_branch, dst_reg,
4789 dst_reg->type, false);
4790 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4791 src_reg->type == PTR_TO_PACKET) ||
4792 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4793 src_reg->type == PTR_TO_PACKET_META)) {
4794 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4795 find_good_pkt_pointers(other_branch, src_reg,
4796 src_reg->type, true);
4797 } else {
4798 return false;
4799 }
4800 break;
4801 case BPF_JLT:
4802 if ((dst_reg->type == PTR_TO_PACKET &&
4803 src_reg->type == PTR_TO_PACKET_END) ||
4804 (dst_reg->type == PTR_TO_PACKET_META &&
4805 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4806 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4807 find_good_pkt_pointers(other_branch, dst_reg,
4808 dst_reg->type, true);
4809 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4810 src_reg->type == PTR_TO_PACKET) ||
4811 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4812 src_reg->type == PTR_TO_PACKET_META)) {
4813 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4814 find_good_pkt_pointers(this_branch, src_reg,
4815 src_reg->type, false);
4816 } else {
4817 return false;
4818 }
4819 break;
4820 case BPF_JGE:
4821 if ((dst_reg->type == PTR_TO_PACKET &&
4822 src_reg->type == PTR_TO_PACKET_END) ||
4823 (dst_reg->type == PTR_TO_PACKET_META &&
4824 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4825 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
4826 find_good_pkt_pointers(this_branch, dst_reg,
4827 dst_reg->type, true);
4828 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4829 src_reg->type == PTR_TO_PACKET) ||
4830 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4831 src_reg->type == PTR_TO_PACKET_META)) {
4832 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
4833 find_good_pkt_pointers(other_branch, src_reg,
4834 src_reg->type, false);
4835 } else {
4836 return false;
4837 }
4838 break;
4839 case BPF_JLE:
4840 if ((dst_reg->type == PTR_TO_PACKET &&
4841 src_reg->type == PTR_TO_PACKET_END) ||
4842 (dst_reg->type == PTR_TO_PACKET_META &&
4843 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4844 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
4845 find_good_pkt_pointers(other_branch, dst_reg,
4846 dst_reg->type, false);
4847 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4848 src_reg->type == PTR_TO_PACKET) ||
4849 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4850 src_reg->type == PTR_TO_PACKET_META)) {
4851 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
4852 find_good_pkt_pointers(this_branch, src_reg,
4853 src_reg->type, true);
4854 } else {
4855 return false;
4856 }
4857 break;
4858 default:
4859 return false;
4860 }
4861
4862 return true;
4863}
4864
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004865static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004866 struct bpf_insn *insn, int *insn_idx)
4867{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004868 struct bpf_verifier_state *this_branch = env->cur_state;
4869 struct bpf_verifier_state *other_branch;
4870 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
4871 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004872 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05004873 bool is_jmp32;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004874 int err;
4875
Jiong Wang092ed092019-01-26 12:26:01 -05004876 /* Only conditional jumps are expected to reach here. */
4877 if (opcode == BPF_JA || opcode > BPF_JSLE) {
4878 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004879 return -EINVAL;
4880 }
4881
4882 if (BPF_SRC(insn->code) == BPF_X) {
4883 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05004884 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004885 return -EINVAL;
4886 }
4887
4888 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004889 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004890 if (err)
4891 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004892
4893 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004894 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004895 insn->src_reg);
4896 return -EACCES;
4897 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004898 } else {
4899 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05004900 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004901 return -EINVAL;
4902 }
4903 }
4904
4905 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004906 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004907 if (err)
4908 return err;
4909
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004910 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05004911 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004912
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004913 if (BPF_SRC(insn->code) == BPF_K) {
Jiong Wang092ed092019-01-26 12:26:01 -05004914 int pred = is_branch_taken(dst_reg, insn->imm, opcode,
4915 is_jmp32);
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004916
4917 if (pred == 1) {
4918 /* only follow the goto, ignore fall-through */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004919 *insn_idx += insn->off;
4920 return 0;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004921 } else if (pred == 0) {
4922 /* only follow fall-through branch, since
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004923 * that's where the program will go
4924 */
4925 return 0;
4926 }
4927 }
4928
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004929 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
4930 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004931 if (!other_branch)
4932 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004933 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004934
Josef Bacik48461132016-09-28 10:54:32 -04004935 /* detect if we are comparing against a constant value so we can adjust
4936 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01004937 * this is only legit if both are scalars (or pointers to the same
4938 * object, I suppose, but we don't support that right now), because
4939 * otherwise the different base pointers mean the offsets aren't
4940 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04004941 */
4942 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05004943 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
4944 struct bpf_reg_state lo_reg0 = *dst_reg;
4945 struct bpf_reg_state lo_reg1 = *src_reg;
4946 struct bpf_reg_state *src_lo, *dst_lo;
4947
4948 dst_lo = &lo_reg0;
4949 src_lo = &lo_reg1;
4950 coerce_reg_to_size(dst_lo, 4);
4951 coerce_reg_to_size(src_lo, 4);
4952
Edward Creef1174f72017-08-07 15:26:19 +01004953 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05004954 src_reg->type == SCALAR_VALUE) {
4955 if (tnum_is_const(src_reg->var_off) ||
4956 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004957 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004958 dst_reg,
4959 is_jmp32
4960 ? src_lo->var_off.value
4961 : src_reg->var_off.value,
4962 opcode, is_jmp32);
4963 else if (tnum_is_const(dst_reg->var_off) ||
4964 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004965 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004966 src_reg,
4967 is_jmp32
4968 ? dst_lo->var_off.value
4969 : dst_reg->var_off.value,
4970 opcode, is_jmp32);
4971 else if (!is_jmp32 &&
4972 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01004973 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004974 reg_combine_min_max(&other_branch_regs[insn->src_reg],
4975 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004976 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01004977 }
4978 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004979 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004980 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04004981 }
4982
Jiong Wang092ed092019-01-26 12:26:01 -05004983 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
4984 * NOTE: these optimizations below are related with pointer comparison
4985 * which will never be JMP32.
4986 */
4987 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004988 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07004989 reg_type_may_be_null(dst_reg->type)) {
4990 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02004991 * safe or unknown depending R == 0 or R != 0 conditional.
4992 */
Joe Stringer840b9612018-10-02 13:35:32 -07004993 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
4994 opcode == BPF_JNE);
4995 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
4996 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01004997 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
4998 this_branch, other_branch) &&
4999 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005000 verbose(env, "R%d pointer comparison prohibited\n",
5001 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005002 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005003 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005004 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005005 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005006 return 0;
5007}
5008
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005009/* return the map pointer stored inside BPF_LD_IMM64 instruction */
5010static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
5011{
5012 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
5013
5014 return (struct bpf_map *) (unsigned long) imm64;
5015}
5016
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005017/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005018static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005019{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005020 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005021 int err;
5022
5023 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005024 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005025 return -EINVAL;
5026 }
5027 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005028 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005029 return -EINVAL;
5030 }
5031
Edward Creedc503a82017-08-15 20:34:35 +01005032 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005033 if (err)
5034 return err;
5035
Jakub Kicinski6b173872016-09-21 11:43:59 +01005036 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01005037 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5038
Edward Creef1174f72017-08-07 15:26:19 +01005039 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01005040 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005041 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01005042 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005043
5044 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
5045 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
5046
5047 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5048 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
5049 return 0;
5050}
5051
Daniel Borkmann96be4322015-03-01 12:31:46 +01005052static bool may_access_skb(enum bpf_prog_type type)
5053{
5054 switch (type) {
5055 case BPF_PROG_TYPE_SOCKET_FILTER:
5056 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01005057 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01005058 return true;
5059 default:
5060 return false;
5061 }
5062}
5063
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005064/* verify safety of LD_ABS|LD_IND instructions:
5065 * - they can only appear in the programs where ctx == skb
5066 * - since they are wrappers of function calls, they scratch R1-R5 registers,
5067 * preserve R6-R9, and store return value into R0
5068 *
5069 * Implicit input:
5070 * ctx == skb == R6 == CTX
5071 *
5072 * Explicit input:
5073 * SRC == any register
5074 * IMM == 32-bit immediate
5075 *
5076 * Output:
5077 * R0 - 8/16/32-bit skb data converted to cpu endianness
5078 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005079static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005080{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005081 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005082 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005083 int i, err;
5084
Daniel Borkmann24701ec2015-03-01 12:31:47 +01005085 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005086 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005087 return -EINVAL;
5088 }
5089
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02005090 if (!env->ops->gen_ld_abs) {
5091 verbose(env, "bpf verifier is misconfigured\n");
5092 return -EINVAL;
5093 }
5094
Jiong Wangf910cef2018-05-02 16:17:17 -04005095 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005096 /* when program has LD_ABS insn JITs and interpreter assume
5097 * that r1 == ctx == skb which is not the case for callees
5098 * that can have arbitrary arguments. It's problematic
5099 * for main prog as well since JITs would need to analyze
5100 * all functions in order to make proper register save/restore
5101 * decisions in the main prog. Hence disallow LD_ABS with calls
5102 */
5103 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
5104 return -EINVAL;
5105 }
5106
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005107 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07005108 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005109 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005110 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005111 return -EINVAL;
5112 }
5113
5114 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01005115 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005116 if (err)
5117 return err;
5118
Joe Stringerfd978bf72018-10-02 13:35:35 -07005119 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
5120 * gen_ld_abs() may terminate the program at runtime, leading to
5121 * reference leak.
5122 */
5123 err = check_reference_leak(env);
5124 if (err) {
5125 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
5126 return err;
5127 }
5128
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08005129 if (env->cur_state->active_spin_lock) {
5130 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
5131 return -EINVAL;
5132 }
5133
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005134 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005135 verbose(env,
5136 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005137 return -EINVAL;
5138 }
5139
5140 if (mode == BPF_IND) {
5141 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01005142 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005143 if (err)
5144 return err;
5145 }
5146
5147 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01005148 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005149 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01005150 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
5151 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005152
5153 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01005154 * the value fetched from the packet.
5155 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005156 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005157 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005158 return 0;
5159}
5160
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005161static int check_return_code(struct bpf_verifier_env *env)
5162{
5163 struct bpf_reg_state *reg;
5164 struct tnum range = tnum_range(0, 1);
5165
5166 switch (env->prog->type) {
5167 case BPF_PROG_TYPE_CGROUP_SKB:
5168 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07005169 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005170 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05005171 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005172 break;
5173 default:
5174 return 0;
5175 }
5176
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005177 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005178 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005179 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005180 reg_type_str[reg->type]);
5181 return -EINVAL;
5182 }
5183
5184 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005185 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005186 if (!tnum_is_unknown(reg->var_off)) {
5187 char tn_buf[48];
5188
5189 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005190 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005191 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005192 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005193 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005194 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005195 return -EINVAL;
5196 }
5197 return 0;
5198}
5199
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005200/* non-recursive DFS pseudo code
5201 * 1 procedure DFS-iterative(G,v):
5202 * 2 label v as discovered
5203 * 3 let S be a stack
5204 * 4 S.push(v)
5205 * 5 while S is not empty
5206 * 6 t <- S.pop()
5207 * 7 if t is what we're looking for:
5208 * 8 return t
5209 * 9 for all edges e in G.adjacentEdges(t) do
5210 * 10 if edge e is already labelled
5211 * 11 continue with the next edge
5212 * 12 w <- G.adjacentVertex(t,e)
5213 * 13 if vertex w is not discovered and not explored
5214 * 14 label e as tree-edge
5215 * 15 label w as discovered
5216 * 16 S.push(w)
5217 * 17 continue at 5
5218 * 18 else if vertex w is discovered
5219 * 19 label e as back-edge
5220 * 20 else
5221 * 21 // vertex w is explored
5222 * 22 label e as forward- or cross-edge
5223 * 23 label t as explored
5224 * 24 S.pop()
5225 *
5226 * convention:
5227 * 0x10 - discovered
5228 * 0x11 - discovered and fall-through edge labelled
5229 * 0x12 - discovered and fall-through and branch edges labelled
5230 * 0x20 - explored
5231 */
5232
5233enum {
5234 DISCOVERED = 0x10,
5235 EXPLORED = 0x20,
5236 FALLTHROUGH = 1,
5237 BRANCH = 2,
5238};
5239
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005240#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005241
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005242static int *insn_stack; /* stack of insns to process */
5243static int cur_stack; /* current stack index */
5244static int *insn_state;
5245
5246/* t, w, e - match pseudo-code above:
5247 * t - index of current instruction
5248 * w - next instruction
5249 * e - edge
5250 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005251static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005252{
5253 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
5254 return 0;
5255
5256 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
5257 return 0;
5258
5259 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005260 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005261 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005262 return -EINVAL;
5263 }
5264
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005265 if (e == BRANCH)
5266 /* mark branch target for state pruning */
5267 env->explored_states[w] = STATE_LIST_MARK;
5268
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005269 if (insn_state[w] == 0) {
5270 /* tree-edge */
5271 insn_state[t] = DISCOVERED | e;
5272 insn_state[w] = DISCOVERED;
5273 if (cur_stack >= env->prog->len)
5274 return -E2BIG;
5275 insn_stack[cur_stack++] = w;
5276 return 1;
5277 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005278 verbose_linfo(env, t, "%d: ", t);
5279 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005280 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005281 return -EINVAL;
5282 } else if (insn_state[w] == EXPLORED) {
5283 /* forward- or cross-edge */
5284 insn_state[t] = DISCOVERED | e;
5285 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005286 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005287 return -EFAULT;
5288 }
5289 return 0;
5290}
5291
5292/* non-recursive depth-first-search to detect loops in BPF program
5293 * loop == back-edge in directed graph
5294 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005295static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005296{
5297 struct bpf_insn *insns = env->prog->insnsi;
5298 int insn_cnt = env->prog->len;
5299 int ret = 0;
5300 int i, t;
5301
5302 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5303 if (!insn_state)
5304 return -ENOMEM;
5305
5306 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5307 if (!insn_stack) {
5308 kfree(insn_state);
5309 return -ENOMEM;
5310 }
5311
5312 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5313 insn_stack[0] = 0; /* 0 is the first instruction */
5314 cur_stack = 1;
5315
5316peek_stack:
5317 if (cur_stack == 0)
5318 goto check_state;
5319 t = insn_stack[cur_stack - 1];
5320
Jiong Wang092ed092019-01-26 12:26:01 -05005321 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5322 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005323 u8 opcode = BPF_OP(insns[t].code);
5324
5325 if (opcode == BPF_EXIT) {
5326 goto mark_explored;
5327 } else if (opcode == BPF_CALL) {
5328 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5329 if (ret == 1)
5330 goto peek_stack;
5331 else if (ret < 0)
5332 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02005333 if (t + 1 < insn_cnt)
5334 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005335 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
5336 env->explored_states[t] = STATE_LIST_MARK;
5337 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
5338 if (ret == 1)
5339 goto peek_stack;
5340 else if (ret < 0)
5341 goto err_free;
5342 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005343 } else if (opcode == BPF_JA) {
5344 if (BPF_SRC(insns[t].code) != BPF_K) {
5345 ret = -EINVAL;
5346 goto err_free;
5347 }
5348 /* unconditional jump with single edge */
5349 ret = push_insn(t, t + insns[t].off + 1,
5350 FALLTHROUGH, env);
5351 if (ret == 1)
5352 goto peek_stack;
5353 else if (ret < 0)
5354 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005355 /* tell verifier to check for equivalent states
5356 * after every call and jump
5357 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07005358 if (t + 1 < insn_cnt)
5359 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005360 } else {
5361 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02005362 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005363 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5364 if (ret == 1)
5365 goto peek_stack;
5366 else if (ret < 0)
5367 goto err_free;
5368
5369 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
5370 if (ret == 1)
5371 goto peek_stack;
5372 else if (ret < 0)
5373 goto err_free;
5374 }
5375 } else {
5376 /* all other non-branch instructions with single
5377 * fall-through edge
5378 */
5379 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5380 if (ret == 1)
5381 goto peek_stack;
5382 else if (ret < 0)
5383 goto err_free;
5384 }
5385
5386mark_explored:
5387 insn_state[t] = EXPLORED;
5388 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005389 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005390 ret = -EFAULT;
5391 goto err_free;
5392 }
5393 goto peek_stack;
5394
5395check_state:
5396 for (i = 0; i < insn_cnt; i++) {
5397 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005398 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005399 ret = -EINVAL;
5400 goto err_free;
5401 }
5402 }
5403 ret = 0; /* cfg looks good */
5404
5405err_free:
5406 kfree(insn_state);
5407 kfree(insn_stack);
5408 return ret;
5409}
5410
Yonghong Song838e9692018-11-19 15:29:11 -08005411/* The minimum supported BTF func info size */
5412#define MIN_BPF_FUNCINFO_SIZE 8
5413#define MAX_FUNCINFO_REC_SIZE 252
5414
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005415static int check_btf_func(struct bpf_verifier_env *env,
5416 const union bpf_attr *attr,
5417 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08005418{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005419 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08005420 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005421 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08005422 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005423 struct bpf_prog *prog;
5424 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005425 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005426 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08005427 int ret = 0;
5428
5429 nfuncs = attr->func_info_cnt;
5430 if (!nfuncs)
5431 return 0;
5432
5433 if (nfuncs != env->subprog_cnt) {
5434 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5435 return -EINVAL;
5436 }
5437
5438 urec_size = attr->func_info_rec_size;
5439 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5440 urec_size > MAX_FUNCINFO_REC_SIZE ||
5441 urec_size % sizeof(u32)) {
5442 verbose(env, "invalid func info rec size %u\n", urec_size);
5443 return -EINVAL;
5444 }
5445
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005446 prog = env->prog;
5447 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005448
5449 urecord = u64_to_user_ptr(attr->func_info);
5450 min_size = min_t(u32, krec_size, urec_size);
5451
Yonghong Songba64e7d2018-11-24 23:20:44 -08005452 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005453 if (!krecord)
5454 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005455
Yonghong Song838e9692018-11-19 15:29:11 -08005456 for (i = 0; i < nfuncs; i++) {
5457 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5458 if (ret) {
5459 if (ret == -E2BIG) {
5460 verbose(env, "nonzero tailing record in func info");
5461 /* set the size kernel expects so loader can zero
5462 * out the rest of the record.
5463 */
5464 if (put_user(min_size, &uattr->func_info_rec_size))
5465 ret = -EFAULT;
5466 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005467 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005468 }
5469
Yonghong Songba64e7d2018-11-24 23:20:44 -08005470 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08005471 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005472 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005473 }
5474
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005475 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08005476 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005477 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005478 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005479 "nonzero insn_off %u for the first func info record",
5480 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08005481 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005482 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005483 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005484 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08005485 verbose(env,
5486 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005487 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08005488 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005489 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005490 }
5491
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005492 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005493 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5494 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005495 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005496 }
5497
5498 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08005499 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005500 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5501 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08005502 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005503 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005504 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005505 }
5506
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005507 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08005508 urecord += urec_size;
5509 }
5510
Yonghong Songba64e7d2018-11-24 23:20:44 -08005511 prog->aux->func_info = krecord;
5512 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08005513 return 0;
5514
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005515err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08005516 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08005517 return ret;
5518}
5519
Yonghong Songba64e7d2018-11-24 23:20:44 -08005520static void adjust_btf_func(struct bpf_verifier_env *env)
5521{
5522 int i;
5523
5524 if (!env->prog->aux->func_info)
5525 return;
5526
5527 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005528 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005529}
5530
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005531#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
5532 sizeof(((struct bpf_line_info *)(0))->line_col))
5533#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
5534
5535static int check_btf_line(struct bpf_verifier_env *env,
5536 const union bpf_attr *attr,
5537 union bpf_attr __user *uattr)
5538{
5539 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
5540 struct bpf_subprog_info *sub;
5541 struct bpf_line_info *linfo;
5542 struct bpf_prog *prog;
5543 const struct btf *btf;
5544 void __user *ulinfo;
5545 int err;
5546
5547 nr_linfo = attr->line_info_cnt;
5548 if (!nr_linfo)
5549 return 0;
5550
5551 rec_size = attr->line_info_rec_size;
5552 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
5553 rec_size > MAX_LINEINFO_REC_SIZE ||
5554 rec_size & (sizeof(u32) - 1))
5555 return -EINVAL;
5556
5557 /* Need to zero it in case the userspace may
5558 * pass in a smaller bpf_line_info object.
5559 */
5560 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
5561 GFP_KERNEL | __GFP_NOWARN);
5562 if (!linfo)
5563 return -ENOMEM;
5564
5565 prog = env->prog;
5566 btf = prog->aux->btf;
5567
5568 s = 0;
5569 sub = env->subprog_info;
5570 ulinfo = u64_to_user_ptr(attr->line_info);
5571 expected_size = sizeof(struct bpf_line_info);
5572 ncopy = min_t(u32, expected_size, rec_size);
5573 for (i = 0; i < nr_linfo; i++) {
5574 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
5575 if (err) {
5576 if (err == -E2BIG) {
5577 verbose(env, "nonzero tailing record in line_info");
5578 if (put_user(expected_size,
5579 &uattr->line_info_rec_size))
5580 err = -EFAULT;
5581 }
5582 goto err_free;
5583 }
5584
5585 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
5586 err = -EFAULT;
5587 goto err_free;
5588 }
5589
5590 /*
5591 * Check insn_off to ensure
5592 * 1) strictly increasing AND
5593 * 2) bounded by prog->len
5594 *
5595 * The linfo[0].insn_off == 0 check logically falls into
5596 * the later "missing bpf_line_info for func..." case
5597 * because the first linfo[0].insn_off must be the
5598 * first sub also and the first sub must have
5599 * subprog_info[0].start == 0.
5600 */
5601 if ((i && linfo[i].insn_off <= prev_offset) ||
5602 linfo[i].insn_off >= prog->len) {
5603 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
5604 i, linfo[i].insn_off, prev_offset,
5605 prog->len);
5606 err = -EINVAL;
5607 goto err_free;
5608 }
5609
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08005610 if (!prog->insnsi[linfo[i].insn_off].code) {
5611 verbose(env,
5612 "Invalid insn code at line_info[%u].insn_off\n",
5613 i);
5614 err = -EINVAL;
5615 goto err_free;
5616 }
5617
Martin KaFai Lau23127b32018-12-13 10:41:46 -08005618 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
5619 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005620 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
5621 err = -EINVAL;
5622 goto err_free;
5623 }
5624
5625 if (s != env->subprog_cnt) {
5626 if (linfo[i].insn_off == sub[s].start) {
5627 sub[s].linfo_idx = i;
5628 s++;
5629 } else if (sub[s].start < linfo[i].insn_off) {
5630 verbose(env, "missing bpf_line_info for func#%u\n", s);
5631 err = -EINVAL;
5632 goto err_free;
5633 }
5634 }
5635
5636 prev_offset = linfo[i].insn_off;
5637 ulinfo += rec_size;
5638 }
5639
5640 if (s != env->subprog_cnt) {
5641 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
5642 env->subprog_cnt - s, s);
5643 err = -EINVAL;
5644 goto err_free;
5645 }
5646
5647 prog->aux->linfo = linfo;
5648 prog->aux->nr_linfo = nr_linfo;
5649
5650 return 0;
5651
5652err_free:
5653 kvfree(linfo);
5654 return err;
5655}
5656
5657static int check_btf_info(struct bpf_verifier_env *env,
5658 const union bpf_attr *attr,
5659 union bpf_attr __user *uattr)
5660{
5661 struct btf *btf;
5662 int err;
5663
5664 if (!attr->func_info_cnt && !attr->line_info_cnt)
5665 return 0;
5666
5667 btf = btf_get_by_fd(attr->prog_btf_fd);
5668 if (IS_ERR(btf))
5669 return PTR_ERR(btf);
5670 env->prog->aux->btf = btf;
5671
5672 err = check_btf_func(env, attr, uattr);
5673 if (err)
5674 return err;
5675
5676 err = check_btf_line(env, attr, uattr);
5677 if (err)
5678 return err;
5679
5680 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005681}
5682
Edward Creef1174f72017-08-07 15:26:19 +01005683/* check %cur's range satisfies %old's */
5684static bool range_within(struct bpf_reg_state *old,
5685 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005686{
Edward Creeb03c9f92017-08-07 15:26:36 +01005687 return old->umin_value <= cur->umin_value &&
5688 old->umax_value >= cur->umax_value &&
5689 old->smin_value <= cur->smin_value &&
5690 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01005691}
5692
5693/* Maximum number of register states that can exist at once */
5694#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
5695struct idpair {
5696 u32 old;
5697 u32 cur;
5698};
5699
5700/* If in the old state two registers had the same id, then they need to have
5701 * the same id in the new state as well. But that id could be different from
5702 * the old state, so we need to track the mapping from old to new ids.
5703 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
5704 * regs with old id 5 must also have new id 9 for the new state to be safe. But
5705 * regs with a different old id could still have new id 9, we don't care about
5706 * that.
5707 * So we look through our idmap to see if this old id has been seen before. If
5708 * so, we require the new id to match; otherwise, we add the id pair to the map.
5709 */
5710static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
5711{
5712 unsigned int i;
5713
5714 for (i = 0; i < ID_MAP_SIZE; i++) {
5715 if (!idmap[i].old) {
5716 /* Reached an empty slot; haven't seen this id before */
5717 idmap[i].old = old_id;
5718 idmap[i].cur = cur_id;
5719 return true;
5720 }
5721 if (idmap[i].old == old_id)
5722 return idmap[i].cur == cur_id;
5723 }
5724 /* We ran out of idmap slots, which should be impossible */
5725 WARN_ON_ONCE(1);
5726 return false;
5727}
5728
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08005729static void clean_func_state(struct bpf_verifier_env *env,
5730 struct bpf_func_state *st)
5731{
5732 enum bpf_reg_liveness live;
5733 int i, j;
5734
5735 for (i = 0; i < BPF_REG_FP; i++) {
5736 live = st->regs[i].live;
5737 /* liveness must not touch this register anymore */
5738 st->regs[i].live |= REG_LIVE_DONE;
5739 if (!(live & REG_LIVE_READ))
5740 /* since the register is unused, clear its state
5741 * to make further comparison simpler
5742 */
5743 __mark_reg_not_init(&st->regs[i]);
5744 }
5745
5746 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5747 live = st->stack[i].spilled_ptr.live;
5748 /* liveness must not touch this stack slot anymore */
5749 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5750 if (!(live & REG_LIVE_READ)) {
5751 __mark_reg_not_init(&st->stack[i].spilled_ptr);
5752 for (j = 0; j < BPF_REG_SIZE; j++)
5753 st->stack[i].slot_type[j] = STACK_INVALID;
5754 }
5755 }
5756}
5757
5758static void clean_verifier_state(struct bpf_verifier_env *env,
5759 struct bpf_verifier_state *st)
5760{
5761 int i;
5762
5763 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5764 /* all regs in this state in all frames were already marked */
5765 return;
5766
5767 for (i = 0; i <= st->curframe; i++)
5768 clean_func_state(env, st->frame[i]);
5769}
5770
5771/* the parentage chains form a tree.
5772 * the verifier states are added to state lists at given insn and
5773 * pushed into state stack for future exploration.
5774 * when the verifier reaches bpf_exit insn some of the verifer states
5775 * stored in the state lists have their final liveness state already,
5776 * but a lot of states will get revised from liveness point of view when
5777 * the verifier explores other branches.
5778 * Example:
5779 * 1: r0 = 1
5780 * 2: if r1 == 100 goto pc+1
5781 * 3: r0 = 2
5782 * 4: exit
5783 * when the verifier reaches exit insn the register r0 in the state list of
5784 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5785 * of insn 2 and goes exploring further. At the insn 4 it will walk the
5786 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5787 *
5788 * Since the verifier pushes the branch states as it sees them while exploring
5789 * the program the condition of walking the branch instruction for the second
5790 * time means that all states below this branch were already explored and
5791 * their final liveness markes are already propagated.
5792 * Hence when the verifier completes the search of state list in is_state_visited()
5793 * we can call this clean_live_states() function to mark all liveness states
5794 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5795 * will not be used.
5796 * This function also clears the registers and stack for states that !READ
5797 * to simplify state merging.
5798 *
5799 * Important note here that walking the same branch instruction in the callee
5800 * doesn't meant that the states are DONE. The verifier has to compare
5801 * the callsites
5802 */
5803static void clean_live_states(struct bpf_verifier_env *env, int insn,
5804 struct bpf_verifier_state *cur)
5805{
5806 struct bpf_verifier_state_list *sl;
5807 int i;
5808
5809 sl = env->explored_states[insn];
5810 if (!sl)
5811 return;
5812
5813 while (sl != STATE_LIST_MARK) {
5814 if (sl->state.curframe != cur->curframe)
5815 goto next;
5816 for (i = 0; i <= cur->curframe; i++)
5817 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
5818 goto next;
5819 clean_verifier_state(env, &sl->state);
5820next:
5821 sl = sl->next;
5822 }
5823}
5824
Edward Creef1174f72017-08-07 15:26:19 +01005825/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01005826static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
5827 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01005828{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005829 bool equal;
5830
Edward Creedc503a82017-08-15 20:34:35 +01005831 if (!(rold->live & REG_LIVE_READ))
5832 /* explored state didn't use this */
5833 return true;
5834
Edward Cree679c7822018-08-22 20:02:19 +01005835 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005836
5837 if (rold->type == PTR_TO_STACK)
5838 /* two stack pointers are equal only if they're pointing to
5839 * the same stack frame, since fp-8 in foo != fp-8 in bar
5840 */
5841 return equal && rold->frameno == rcur->frameno;
5842
5843 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01005844 return true;
5845
5846 if (rold->type == NOT_INIT)
5847 /* explored state can't have used this */
5848 return true;
5849 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005850 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005851 switch (rold->type) {
5852 case SCALAR_VALUE:
5853 if (rcur->type == SCALAR_VALUE) {
5854 /* new val must satisfy old val knowledge */
5855 return range_within(rold, rcur) &&
5856 tnum_in(rold->var_off, rcur->var_off);
5857 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08005858 /* We're trying to use a pointer in place of a scalar.
5859 * Even if the scalar was unbounded, this could lead to
5860 * pointer leaks because scalars are allowed to leak
5861 * while pointers are not. We could make this safe in
5862 * special cases if root is calling us, but it's
5863 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01005864 */
Jann Horn179d1c52017-12-18 20:11:59 -08005865 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005866 }
5867 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01005868 /* If the new min/max/var_off satisfy the old ones and
5869 * everything else matches, we are OK.
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08005870 * 'id' is not compared, since it's only used for maps with
5871 * bpf_spin_lock inside map element and in such cases if
5872 * the rest of the prog is valid for one map element then
5873 * it's valid for all map elements regardless of the key
5874 * used in bpf_map_lookup()
Edward Cree1b688a12017-08-23 15:10:50 +01005875 */
5876 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
5877 range_within(rold, rcur) &&
5878 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01005879 case PTR_TO_MAP_VALUE_OR_NULL:
5880 /* a PTR_TO_MAP_VALUE could be safe to use as a
5881 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
5882 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
5883 * checked, doing so could have affected others with the same
5884 * id, and we can't check for that because we lost the id when
5885 * we converted to a PTR_TO_MAP_VALUE.
5886 */
5887 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
5888 return false;
5889 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
5890 return false;
5891 /* Check our ids match any regs they're supposed to */
5892 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005893 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01005894 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005895 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01005896 return false;
5897 /* We must have at least as much range as the old ptr
5898 * did, so that any accesses which were safe before are
5899 * still safe. This is true even if old range < old off,
5900 * since someone could have accessed through (ptr - k), or
5901 * even done ptr -= k in a register, to get a safe access.
5902 */
5903 if (rold->range > rcur->range)
5904 return false;
5905 /* If the offsets don't match, we can't trust our alignment;
5906 * nor can we be sure that we won't fall out of range.
5907 */
5908 if (rold->off != rcur->off)
5909 return false;
5910 /* id relations must be preserved */
5911 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
5912 return false;
5913 /* new val must satisfy old val knowledge */
5914 return range_within(rold, rcur) &&
5915 tnum_in(rold->var_off, rcur->var_off);
5916 case PTR_TO_CTX:
5917 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01005918 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07005919 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07005920 case PTR_TO_SOCKET:
5921 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08005922 case PTR_TO_SOCK_COMMON:
5923 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08005924 case PTR_TO_TCP_SOCK:
5925 case PTR_TO_TCP_SOCK_OR_NULL:
Edward Creef1174f72017-08-07 15:26:19 +01005926 /* Only valid matches are exact, which memcmp() above
5927 * would have accepted
5928 */
5929 default:
5930 /* Don't know what's going on, just say it's not safe */
5931 return false;
5932 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005933
Edward Creef1174f72017-08-07 15:26:19 +01005934 /* Shouldn't get here; if we do, say it's not safe */
5935 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005936 return false;
5937}
5938
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005939static bool stacksafe(struct bpf_func_state *old,
5940 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005941 struct idpair *idmap)
5942{
5943 int i, spi;
5944
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005945 /* walk slots of the explored stack and ignore any additional
5946 * slots in the current stack, since explored(safe) state
5947 * didn't use them
5948 */
5949 for (i = 0; i < old->allocated_stack; i++) {
5950 spi = i / BPF_REG_SIZE;
5951
Alexei Starovoitovb2339202018-12-13 11:42:31 -08005952 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
5953 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005954 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00005955 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08005956 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005957
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005958 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
5959 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08005960
5961 /* explored stack has more populated slots than current stack
5962 * and these slots were used
5963 */
5964 if (i >= cur->allocated_stack)
5965 return false;
5966
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005967 /* if old state was safe with misc data in the stack
5968 * it will be safe with zero-initialized stack.
5969 * The opposite is not true
5970 */
5971 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
5972 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
5973 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005974 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
5975 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
5976 /* Ex: old explored (safe) state has STACK_SPILL in
5977 * this stack slot, but current has has STACK_MISC ->
5978 * this verifier states are not equivalent,
5979 * return false to continue verification of this path
5980 */
5981 return false;
5982 if (i % BPF_REG_SIZE)
5983 continue;
5984 if (old->stack[spi].slot_type[0] != STACK_SPILL)
5985 continue;
5986 if (!regsafe(&old->stack[spi].spilled_ptr,
5987 &cur->stack[spi].spilled_ptr,
5988 idmap))
5989 /* when explored and current stack slot are both storing
5990 * spilled registers, check that stored pointers types
5991 * are the same as well.
5992 * Ex: explored safe path could have stored
5993 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
5994 * but current path has stored:
5995 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
5996 * such verifier states are not equivalent.
5997 * return false to continue verification of this path
5998 */
5999 return false;
6000 }
6001 return true;
6002}
6003
Joe Stringerfd978bf72018-10-02 13:35:35 -07006004static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6005{
6006 if (old->acquired_refs != cur->acquired_refs)
6007 return false;
6008 return !memcmp(old->refs, cur->refs,
6009 sizeof(*old->refs) * old->acquired_refs);
6010}
6011
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006012/* compare two verifier states
6013 *
6014 * all states stored in state_list are known to be valid, since
6015 * verifier reached 'bpf_exit' instruction through them
6016 *
6017 * this function is called when verifier exploring different branches of
6018 * execution popped from the state stack. If it sees an old state that has
6019 * more strict register state and more strict stack state then this execution
6020 * branch doesn't need to be explored further, since verifier already
6021 * concluded that more strict state leads to valid finish.
6022 *
6023 * Therefore two states are equivalent if register state is more conservative
6024 * and explored stack state is more conservative than the current one.
6025 * Example:
6026 * explored current
6027 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
6028 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
6029 *
6030 * In other words if current stack state (one being explored) has more
6031 * valid slots than old one that already passed validation, it means
6032 * the verifier can stop exploring and conclude that current state is valid too
6033 *
6034 * Similarly with registers. If explored state has register type as invalid
6035 * whereas register type in current state is meaningful, it means that
6036 * the current state will reach 'bpf_exit' instruction safely
6037 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006038static bool func_states_equal(struct bpf_func_state *old,
6039 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006040{
Edward Creef1174f72017-08-07 15:26:19 +01006041 struct idpair *idmap;
6042 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006043 int i;
6044
Edward Creef1174f72017-08-07 15:26:19 +01006045 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
6046 /* If we failed to allocate the idmap, just say it's not safe */
6047 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07006048 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006049
6050 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01006051 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01006052 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006053 }
6054
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006055 if (!stacksafe(old, cur, idmap))
6056 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006057
6058 if (!refsafe(old, cur))
6059 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01006060 ret = true;
6061out_free:
6062 kfree(idmap);
6063 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006064}
6065
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006066static bool states_equal(struct bpf_verifier_env *env,
6067 struct bpf_verifier_state *old,
6068 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01006069{
Edward Creedc503a82017-08-15 20:34:35 +01006070 int i;
6071
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006072 if (old->curframe != cur->curframe)
6073 return false;
6074
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006075 /* Verification state from speculative execution simulation
6076 * must never prune a non-speculative execution one.
6077 */
6078 if (old->speculative && !cur->speculative)
6079 return false;
6080
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006081 if (old->active_spin_lock != cur->active_spin_lock)
6082 return false;
6083
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006084 /* for states to be equal callsites have to be the same
6085 * and all frame states need to be equivalent
6086 */
6087 for (i = 0; i <= old->curframe; i++) {
6088 if (old->frame[i]->callsite != cur->frame[i]->callsite)
6089 return false;
6090 if (!func_states_equal(old->frame[i], cur->frame[i]))
6091 return false;
6092 }
6093 return true;
6094}
6095
6096/* A write screens off any subsequent reads; but write marks come from the
6097 * straight-line code between a state and its parent. When we arrive at an
6098 * equivalent state (jump target or such) we didn't arrive by the straight-line
6099 * code, so read marks in the state must propagate to the parent regardless
6100 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01006101 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006102 */
6103static int propagate_liveness(struct bpf_verifier_env *env,
6104 const struct bpf_verifier_state *vstate,
6105 struct bpf_verifier_state *vparent)
6106{
6107 int i, frame, err = 0;
6108 struct bpf_func_state *state, *parent;
6109
6110 if (vparent->curframe != vstate->curframe) {
6111 WARN(1, "propagate_live: parent frame %d current frame %d\n",
6112 vparent->curframe, vstate->curframe);
6113 return -EFAULT;
6114 }
Edward Creedc503a82017-08-15 20:34:35 +01006115 /* Propagate read liveness of registers... */
6116 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Jakub Kicinski83d16312019-03-21 14:34:36 -07006117 for (frame = 0; frame <= vstate->curframe; frame++) {
6118 /* We don't need to worry about FP liveness, it's read-only */
6119 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
6120 if (vparent->frame[frame]->regs[i].live & REG_LIVE_READ)
6121 continue;
6122 if (vstate->frame[frame]->regs[i].live & REG_LIVE_READ) {
6123 err = mark_reg_read(env, &vstate->frame[frame]->regs[i],
6124 &vparent->frame[frame]->regs[i]);
6125 if (err)
6126 return err;
6127 }
Edward Creedc503a82017-08-15 20:34:35 +01006128 }
6129 }
Edward Creedc503a82017-08-15 20:34:35 +01006130
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006131 /* ... and stack slots */
6132 for (frame = 0; frame <= vstate->curframe; frame++) {
6133 state = vstate->frame[frame];
6134 parent = vparent->frame[frame];
6135 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
6136 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006137 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
6138 continue;
6139 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
Edward Cree679c7822018-08-22 20:02:19 +01006140 mark_reg_read(env, &state->stack[i].spilled_ptr,
6141 &parent->stack[i].spilled_ptr);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006142 }
Edward Creedc503a82017-08-15 20:34:35 +01006143 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006144 return err;
Edward Creedc503a82017-08-15 20:34:35 +01006145}
6146
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006147static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006148{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006149 struct bpf_verifier_state_list *new_sl;
6150 struct bpf_verifier_state_list *sl;
Edward Cree679c7822018-08-22 20:02:19 +01006151 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006152 int i, j, err, states_cnt = 0;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006153
6154 sl = env->explored_states[insn_idx];
6155 if (!sl)
6156 /* this 'insn_idx' instruction wasn't marked, so we will not
6157 * be doing state search here
6158 */
6159 return 0;
6160
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006161 clean_live_states(env, insn_idx, cur);
6162
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006163 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006164 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006165 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01006166 * prune the search.
6167 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006168 * If we have any write marks in env->cur_state, they
6169 * will prevent corresponding reads in the continuation
6170 * from reaching our parent (an explored_state). Our
6171 * own state will get the read marks recorded, but
6172 * they'll be immediately forgotten as we're pruning
6173 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006174 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006175 err = propagate_liveness(env, &sl->state, cur);
6176 if (err)
6177 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006178 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01006179 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006180 sl = sl->next;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006181 states_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006182 }
6183
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006184 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
6185 return 0;
6186
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006187 /* there were no equivalent states, remember current one.
6188 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006189 * but it will either reach outer most bpf_exit (which means it's safe)
6190 * or it will be rejected. Since there are no loops, we won't be
6191 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
6192 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006193 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006194 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006195 if (!new_sl)
6196 return -ENOMEM;
6197
6198 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01006199 new = &new_sl->state;
6200 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006201 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01006202 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006203 kfree(new_sl);
6204 return err;
6205 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006206 new_sl->next = env->explored_states[insn_idx];
6207 env->explored_states[insn_idx] = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08006208 /* connect new state to parentage chain. Current frame needs all
6209 * registers connected. Only r6 - r9 of the callers are alive (pushed
6210 * to the stack implicitly by JITs) so in callers' frames connect just
6211 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
6212 * the state of the call instruction (with WRITTEN set), and r0 comes
6213 * from callee with its full parentage chain, anyway.
6214 */
6215 for (j = 0; j <= cur->curframe; j++)
6216 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
6217 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006218 /* clear write marks in current state: the writes we did are not writes
6219 * our child did, so they don't screen off its reads from us.
6220 * (There are no read marks in current state, because reads always mark
6221 * their parent and current state never has children yet. Only
6222 * explored_states can get read marks.)
6223 */
Edward Creedc503a82017-08-15 20:34:35 +01006224 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006225 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
6226
6227 /* all stack frames are accessible from callee, clear them all */
6228 for (j = 0; j <= cur->curframe; j++) {
6229 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01006230 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006231
Edward Cree679c7822018-08-22 20:02:19 +01006232 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006233 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01006234 frame->stack[i].spilled_ptr.parent =
6235 &newframe->stack[i].spilled_ptr;
6236 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006237 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006238 return 0;
6239}
6240
Joe Stringerc64b7982018-10-02 13:35:33 -07006241/* Return true if it's OK to have the same insn return a different type. */
6242static bool reg_type_mismatch_ok(enum bpf_reg_type type)
6243{
6244 switch (type) {
6245 case PTR_TO_CTX:
6246 case PTR_TO_SOCKET:
6247 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006248 case PTR_TO_SOCK_COMMON:
6249 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006250 case PTR_TO_TCP_SOCK:
6251 case PTR_TO_TCP_SOCK_OR_NULL:
Joe Stringerc64b7982018-10-02 13:35:33 -07006252 return false;
6253 default:
6254 return true;
6255 }
6256}
6257
6258/* If an instruction was previously used with particular pointer types, then we
6259 * need to be careful to avoid cases such as the below, where it may be ok
6260 * for one branch accessing the pointer, but not ok for the other branch:
6261 *
6262 * R1 = sock_ptr
6263 * goto X;
6264 * ...
6265 * R1 = some_other_valid_ptr;
6266 * goto X;
6267 * ...
6268 * R2 = *(u32 *)(R1 + 0);
6269 */
6270static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
6271{
6272 return src != prev && (!reg_type_mismatch_ok(src) ||
6273 !reg_type_mismatch_ok(prev));
6274}
6275
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006276static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006277{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006278 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006279 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006280 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006281 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006282 int insn_processed = 0;
6283 bool do_print_state = false;
6284
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006285 env->prev_linfo = NULL;
6286
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006287 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6288 if (!state)
6289 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006290 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006291 state->speculative = false;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006292 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6293 if (!state->frame[0]) {
6294 kfree(state);
6295 return -ENOMEM;
6296 }
6297 env->cur_state = state;
6298 init_func_state(env, state->frame[0],
6299 BPF_MAIN_FUNC /* callsite */,
6300 0 /* frameno */,
6301 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006302
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006303 for (;;) {
6304 struct bpf_insn *insn;
6305 u8 class;
6306 int err;
6307
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006308 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006309 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006310 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006311 return -EFAULT;
6312 }
6313
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006314 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006315 class = BPF_CLASS(insn->code);
6316
Daniel Borkmann07016152016-04-05 22:33:17 +02006317 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006318 verbose(env,
6319 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006320 insn_processed);
6321 return -E2BIG;
6322 }
6323
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006324 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006325 if (err < 0)
6326 return err;
6327 if (err == 1) {
6328 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006329 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006330 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006331 verbose(env, "\nfrom %d to %d%s: safe\n",
6332 env->prev_insn_idx, env->insn_idx,
6333 env->cur_state->speculative ?
6334 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006335 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006336 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006337 }
6338 goto process_bpf_exit;
6339 }
6340
Alexei Starovoitovc3494802018-12-03 22:46:04 -08006341 if (signal_pending(current))
6342 return -EAGAIN;
6343
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02006344 if (need_resched())
6345 cond_resched();
6346
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006347 if (env->log.level > 1 || (env->log.level && do_print_state)) {
6348 if (env->log.level > 1)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006349 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07006350 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006351 verbose(env, "\nfrom %d to %d%s:",
6352 env->prev_insn_idx, env->insn_idx,
6353 env->cur_state->speculative ?
6354 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006355 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006356 do_print_state = false;
6357 }
6358
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006359 if (env->log.level) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006360 const struct bpf_insn_cbs cbs = {
6361 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01006362 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01006363 };
6364
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006365 verbose_linfo(env, env->insn_idx, "; ");
6366 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01006367 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006368 }
6369
Jakub Kicinskicae19272017-12-27 18:39:05 -08006370 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006371 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6372 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08006373 if (err)
6374 return err;
6375 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01006376
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006377 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006378 env->insn_aux_data[env->insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006379
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006380 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006381 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006382 if (err)
6383 return err;
6384
6385 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006386 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006387
6388 /* check for reserved fields is already done */
6389
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006390 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006391 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006392 if (err)
6393 return err;
6394
Edward Creedc503a82017-08-15 20:34:35 +01006395 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006396 if (err)
6397 return err;
6398
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07006399 src_reg_type = regs[insn->src_reg].type;
6400
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006401 /* check that memory (src_reg + off) is readable,
6402 * the state of dst_reg will be updated by this func
6403 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006404 err = check_mem_access(env, env->insn_idx, insn->src_reg,
6405 insn->off, BPF_SIZE(insn->code),
6406 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006407 if (err)
6408 return err;
6409
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006410 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006411
6412 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006413 /* saw a valid insn
6414 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006415 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006416 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006417 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006418
Joe Stringerc64b7982018-10-02 13:35:33 -07006419 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006420 /* ABuser program is trying to use the same insn
6421 * dst_reg = *(u32*) (src_reg + off)
6422 * with different pointer types:
6423 * src_reg == ctx in one branch and
6424 * src_reg == stack|map in some other branch.
6425 * Reject it.
6426 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006427 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006428 return -EINVAL;
6429 }
6430
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006431 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006432 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006433
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006434 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006435 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006436 if (err)
6437 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006438 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006439 continue;
6440 }
6441
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006442 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006443 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006444 if (err)
6445 return err;
6446 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006447 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006448 if (err)
6449 return err;
6450
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006451 dst_reg_type = regs[insn->dst_reg].type;
6452
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006453 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006454 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6455 insn->off, BPF_SIZE(insn->code),
6456 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006457 if (err)
6458 return err;
6459
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006460 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006461
6462 if (*prev_dst_type == NOT_INIT) {
6463 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07006464 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006465 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006466 return -EINVAL;
6467 }
6468
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006469 } else if (class == BPF_ST) {
6470 if (BPF_MODE(insn->code) != BPF_MEM ||
6471 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006472 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006473 return -EINVAL;
6474 }
6475 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006476 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006477 if (err)
6478 return err;
6479
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006480 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07006481 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02006482 insn->dst_reg,
6483 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006484 return -EACCES;
6485 }
6486
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006487 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006488 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6489 insn->off, BPF_SIZE(insn->code),
6490 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006491 if (err)
6492 return err;
6493
Jiong Wang092ed092019-01-26 12:26:01 -05006494 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006495 u8 opcode = BPF_OP(insn->code);
6496
6497 if (opcode == BPF_CALL) {
6498 if (BPF_SRC(insn->code) != BPF_K ||
6499 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006500 (insn->src_reg != BPF_REG_0 &&
6501 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05006502 insn->dst_reg != BPF_REG_0 ||
6503 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006504 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006505 return -EINVAL;
6506 }
6507
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006508 if (env->cur_state->active_spin_lock &&
6509 (insn->src_reg == BPF_PSEUDO_CALL ||
6510 insn->imm != BPF_FUNC_spin_unlock)) {
6511 verbose(env, "function calls are not allowed while holding a lock\n");
6512 return -EINVAL;
6513 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006514 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006515 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006516 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006517 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006518 if (err)
6519 return err;
6520
6521 } else if (opcode == BPF_JA) {
6522 if (BPF_SRC(insn->code) != BPF_K ||
6523 insn->imm != 0 ||
6524 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006525 insn->dst_reg != BPF_REG_0 ||
6526 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006527 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006528 return -EINVAL;
6529 }
6530
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006531 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006532 continue;
6533
6534 } else if (opcode == BPF_EXIT) {
6535 if (BPF_SRC(insn->code) != BPF_K ||
6536 insn->imm != 0 ||
6537 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006538 insn->dst_reg != BPF_REG_0 ||
6539 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006540 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006541 return -EINVAL;
6542 }
6543
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006544 if (env->cur_state->active_spin_lock) {
6545 verbose(env, "bpf_spin_unlock is missing\n");
6546 return -EINVAL;
6547 }
6548
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006549 if (state->curframe) {
6550 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006551 env->prev_insn_idx = env->insn_idx;
6552 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006553 if (err)
6554 return err;
6555 do_print_state = true;
6556 continue;
6557 }
6558
Joe Stringerfd978bf72018-10-02 13:35:35 -07006559 err = check_reference_leak(env);
6560 if (err)
6561 return err;
6562
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006563 /* eBPF calling convetion is such that R0 is used
6564 * to return the value from eBPF program.
6565 * Make sure that it's readable at this time
6566 * of bpf_exit, which means that program wrote
6567 * something into it earlier
6568 */
Edward Creedc503a82017-08-15 20:34:35 +01006569 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006570 if (err)
6571 return err;
6572
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006573 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006574 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006575 return -EACCES;
6576 }
6577
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006578 err = check_return_code(env);
6579 if (err)
6580 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006581process_bpf_exit:
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006582 err = pop_stack(env, &env->prev_insn_idx,
6583 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006584 if (err < 0) {
6585 if (err != -ENOENT)
6586 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006587 break;
6588 } else {
6589 do_print_state = true;
6590 continue;
6591 }
6592 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006593 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006594 if (err)
6595 return err;
6596 }
6597 } else if (class == BPF_LD) {
6598 u8 mode = BPF_MODE(insn->code);
6599
6600 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006601 err = check_ld_abs(env, insn);
6602 if (err)
6603 return err;
6604
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006605 } else if (mode == BPF_IMM) {
6606 err = check_ld_imm(env, insn);
6607 if (err)
6608 return err;
6609
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006610 env->insn_idx++;
6611 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006612 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006613 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006614 return -EINVAL;
6615 }
6616 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006617 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006618 return -EINVAL;
6619 }
6620
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006621 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006622 }
6623
Daniel Borkmann4bd95f42018-01-20 01:24:36 +01006624 verbose(env, "processed %d insns (limit %d), stack depth ",
6625 insn_processed, BPF_COMPLEXITY_LIMIT_INSNS);
Jiong Wangf910cef2018-05-02 16:17:17 -04006626 for (i = 0; i < env->subprog_cnt; i++) {
Jiong Wang9c8105b2018-05-02 16:17:18 -04006627 u32 depth = env->subprog_info[i].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006628
6629 verbose(env, "%d", depth);
Jiong Wangf910cef2018-05-02 16:17:17 -04006630 if (i + 1 < env->subprog_cnt)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006631 verbose(env, "+");
6632 }
6633 verbose(env, "\n");
Jiong Wang9c8105b2018-05-02 16:17:18 -04006634 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006635 return 0;
6636}
6637
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006638static int check_map_prealloc(struct bpf_map *map)
6639{
6640 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07006641 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
6642 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006643 !(map->map_flags & BPF_F_NO_PREALLOC);
6644}
6645
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006646static bool is_tracing_prog_type(enum bpf_prog_type type)
6647{
6648 switch (type) {
6649 case BPF_PROG_TYPE_KPROBE:
6650 case BPF_PROG_TYPE_TRACEPOINT:
6651 case BPF_PROG_TYPE_PERF_EVENT:
6652 case BPF_PROG_TYPE_RAW_TRACEPOINT:
6653 return true;
6654 default:
6655 return false;
6656 }
6657}
6658
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006659static int check_map_prog_compatibility(struct bpf_verifier_env *env,
6660 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006661 struct bpf_prog *prog)
6662
6663{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006664 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
6665 * preallocated hash maps, since doing memory allocation
6666 * in overflow_handler can crash depending on where nmi got
6667 * triggered.
6668 */
6669 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
6670 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006671 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006672 return -EINVAL;
6673 }
6674 if (map->inner_map_meta &&
6675 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006676 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006677 return -EINVAL;
6678 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006679 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08006680
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006681 if ((is_tracing_prog_type(prog->type) ||
6682 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
6683 map_value_has_spin_lock(map)) {
6684 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
6685 return -EINVAL;
6686 }
6687
Jakub Kicinskia3884572018-01-11 20:29:09 -08006688 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07006689 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08006690 verbose(env, "offload device mismatch between prog and map\n");
6691 return -EINVAL;
6692 }
6693
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006694 return 0;
6695}
6696
Roman Gushchinb741f162018-09-28 14:45:43 +00006697static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
6698{
6699 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
6700 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
6701}
6702
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006703/* look for pseudo eBPF instructions that access map FDs and
6704 * replace them with actual map pointers
6705 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006706static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006707{
6708 struct bpf_insn *insn = env->prog->insnsi;
6709 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006710 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006711
Daniel Borkmannf1f77142017-01-13 23:38:15 +01006712 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01006713 if (err)
6714 return err;
6715
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006716 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006717 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006718 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006719 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006720 return -EINVAL;
6721 }
6722
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006723 if (BPF_CLASS(insn->code) == BPF_STX &&
6724 ((BPF_MODE(insn->code) != BPF_MEM &&
6725 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006726 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006727 return -EINVAL;
6728 }
6729
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006730 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
6731 struct bpf_map *map;
6732 struct fd f;
6733
6734 if (i == insn_cnt - 1 || insn[1].code != 0 ||
6735 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
6736 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006737 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006738 return -EINVAL;
6739 }
6740
6741 if (insn->src_reg == 0)
6742 /* valid generic load 64-bit imm */
6743 goto next_insn;
6744
Daniel Borkmann20182392019-03-04 21:08:53 +01006745 if (insn[0].src_reg != BPF_PSEUDO_MAP_FD ||
6746 insn[1].imm != 0) {
6747 verbose(env, "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006748 return -EINVAL;
6749 }
6750
Daniel Borkmann20182392019-03-04 21:08:53 +01006751 f = fdget(insn[0].imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01006752 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006753 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006754 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Daniel Borkmann20182392019-03-04 21:08:53 +01006755 insn[0].imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006756 return PTR_ERR(map);
6757 }
6758
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006759 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006760 if (err) {
6761 fdput(f);
6762 return err;
6763 }
6764
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006765 /* store map pointer inside BPF_LD_IMM64 instruction */
6766 insn[0].imm = (u32) (unsigned long) map;
6767 insn[1].imm = ((u64) (unsigned long) map) >> 32;
6768
6769 /* check whether we recorded this map already */
6770 for (j = 0; j < env->used_map_cnt; j++)
6771 if (env->used_maps[j] == map) {
6772 fdput(f);
6773 goto next_insn;
6774 }
6775
6776 if (env->used_map_cnt >= MAX_USED_MAPS) {
6777 fdput(f);
6778 return -E2BIG;
6779 }
6780
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006781 /* hold the map. If the program is rejected by verifier,
6782 * the map will be released by release_maps() or it
6783 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07006784 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006785 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07006786 map = bpf_map_inc(map, false);
6787 if (IS_ERR(map)) {
6788 fdput(f);
6789 return PTR_ERR(map);
6790 }
6791 env->used_maps[env->used_map_cnt++] = map;
6792
Roman Gushchinb741f162018-09-28 14:45:43 +00006793 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006794 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00006795 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006796 fdput(f);
6797 return -EBUSY;
6798 }
6799
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006800 fdput(f);
6801next_insn:
6802 insn++;
6803 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01006804 continue;
6805 }
6806
6807 /* Basic sanity check before we invest more work here. */
6808 if (!bpf_opcode_in_insntable(insn->code)) {
6809 verbose(env, "unknown opcode %02x\n", insn->code);
6810 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006811 }
6812 }
6813
6814 /* now all pseudo BPF_LD_IMM64 instructions load valid
6815 * 'struct bpf_map *' into a register instead of user map_fd.
6816 * These pointers will be used later by verifier to validate map access.
6817 */
6818 return 0;
6819}
6820
6821/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006822static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006823{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006824 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006825 int i;
6826
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006827 for_each_cgroup_storage_type(stype) {
6828 if (!env->prog->aux->cgroup_storage[stype])
6829 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006830 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006831 env->prog->aux->cgroup_storage[stype]);
6832 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006833
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006834 for (i = 0; i < env->used_map_cnt; i++)
6835 bpf_map_put(env->used_maps[i]);
6836}
6837
6838/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006839static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006840{
6841 struct bpf_insn *insn = env->prog->insnsi;
6842 int insn_cnt = env->prog->len;
6843 int i;
6844
6845 for (i = 0; i < insn_cnt; i++, insn++)
6846 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
6847 insn->src_reg = 0;
6848}
6849
Alexei Starovoitov80419022017-03-15 18:26:41 -07006850/* single env->prog->insni[off] instruction was replaced with the range
6851 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
6852 * [0, off) and [off, end) to new locations, so the patched range stays zero
6853 */
6854static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
6855 u32 off, u32 cnt)
6856{
6857 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006858 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006859
6860 if (cnt == 1)
6861 return 0;
Kees Cookfad953c2018-06-12 14:27:37 -07006862 new_data = vzalloc(array_size(prog_len,
6863 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07006864 if (!new_data)
6865 return -ENOMEM;
6866 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
6867 memcpy(new_data + off + cnt - 1, old_data + off,
6868 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006869 for (i = off; i < off + cnt - 1; i++)
6870 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006871 env->insn_aux_data = new_data;
6872 vfree(old_data);
6873 return 0;
6874}
6875
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006876static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
6877{
6878 int i;
6879
6880 if (len == 1)
6881 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04006882 /* NOTE: fake 'exit' subprog should be updated as well. */
6883 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00006884 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006885 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04006886 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006887 }
6888}
6889
Alexei Starovoitov80419022017-03-15 18:26:41 -07006890static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
6891 const struct bpf_insn *patch, u32 len)
6892{
6893 struct bpf_prog *new_prog;
6894
6895 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
6896 if (!new_prog)
6897 return NULL;
6898 if (adjust_insn_aux_data(env, new_prog->len, off, len))
6899 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006900 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07006901 return new_prog;
6902}
6903
Jakub Kicinski52875a02019-01-22 22:45:20 -08006904static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
6905 u32 off, u32 cnt)
6906{
6907 int i, j;
6908
6909 /* find first prog starting at or after off (first to remove) */
6910 for (i = 0; i < env->subprog_cnt; i++)
6911 if (env->subprog_info[i].start >= off)
6912 break;
6913 /* find first prog starting at or after off + cnt (first to stay) */
6914 for (j = i; j < env->subprog_cnt; j++)
6915 if (env->subprog_info[j].start >= off + cnt)
6916 break;
6917 /* if j doesn't start exactly at off + cnt, we are just removing
6918 * the front of previous prog
6919 */
6920 if (env->subprog_info[j].start != off + cnt)
6921 j--;
6922
6923 if (j > i) {
6924 struct bpf_prog_aux *aux = env->prog->aux;
6925 int move;
6926
6927 /* move fake 'exit' subprog as well */
6928 move = env->subprog_cnt + 1 - j;
6929
6930 memmove(env->subprog_info + i,
6931 env->subprog_info + j,
6932 sizeof(*env->subprog_info) * move);
6933 env->subprog_cnt -= j - i;
6934
6935 /* remove func_info */
6936 if (aux->func_info) {
6937 move = aux->func_info_cnt - j;
6938
6939 memmove(aux->func_info + i,
6940 aux->func_info + j,
6941 sizeof(*aux->func_info) * move);
6942 aux->func_info_cnt -= j - i;
6943 /* func_info->insn_off is set after all code rewrites,
6944 * in adjust_btf_func() - no need to adjust
6945 */
6946 }
6947 } else {
6948 /* convert i from "first prog to remove" to "first to adjust" */
6949 if (env->subprog_info[i].start == off)
6950 i++;
6951 }
6952
6953 /* update fake 'exit' subprog as well */
6954 for (; i <= env->subprog_cnt; i++)
6955 env->subprog_info[i].start -= cnt;
6956
6957 return 0;
6958}
6959
6960static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
6961 u32 cnt)
6962{
6963 struct bpf_prog *prog = env->prog;
6964 u32 i, l_off, l_cnt, nr_linfo;
6965 struct bpf_line_info *linfo;
6966
6967 nr_linfo = prog->aux->nr_linfo;
6968 if (!nr_linfo)
6969 return 0;
6970
6971 linfo = prog->aux->linfo;
6972
6973 /* find first line info to remove, count lines to be removed */
6974 for (i = 0; i < nr_linfo; i++)
6975 if (linfo[i].insn_off >= off)
6976 break;
6977
6978 l_off = i;
6979 l_cnt = 0;
6980 for (; i < nr_linfo; i++)
6981 if (linfo[i].insn_off < off + cnt)
6982 l_cnt++;
6983 else
6984 break;
6985
6986 /* First live insn doesn't match first live linfo, it needs to "inherit"
6987 * last removed linfo. prog is already modified, so prog->len == off
6988 * means no live instructions after (tail of the program was removed).
6989 */
6990 if (prog->len != off && l_cnt &&
6991 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
6992 l_cnt--;
6993 linfo[--i].insn_off = off + cnt;
6994 }
6995
6996 /* remove the line info which refer to the removed instructions */
6997 if (l_cnt) {
6998 memmove(linfo + l_off, linfo + i,
6999 sizeof(*linfo) * (nr_linfo - i));
7000
7001 prog->aux->nr_linfo -= l_cnt;
7002 nr_linfo = prog->aux->nr_linfo;
7003 }
7004
7005 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
7006 for (i = l_off; i < nr_linfo; i++)
7007 linfo[i].insn_off -= cnt;
7008
7009 /* fix up all subprogs (incl. 'exit') which start >= off */
7010 for (i = 0; i <= env->subprog_cnt; i++)
7011 if (env->subprog_info[i].linfo_idx > l_off) {
7012 /* program may have started in the removed region but
7013 * may not be fully removed
7014 */
7015 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
7016 env->subprog_info[i].linfo_idx -= l_cnt;
7017 else
7018 env->subprog_info[i].linfo_idx = l_off;
7019 }
7020
7021 return 0;
7022}
7023
7024static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
7025{
7026 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7027 unsigned int orig_prog_len = env->prog->len;
7028 int err;
7029
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007030 if (bpf_prog_is_dev_bound(env->prog->aux))
7031 bpf_prog_offload_remove_insns(env, off, cnt);
7032
Jakub Kicinski52875a02019-01-22 22:45:20 -08007033 err = bpf_remove_insns(env->prog, off, cnt);
7034 if (err)
7035 return err;
7036
7037 err = adjust_subprog_starts_after_remove(env, off, cnt);
7038 if (err)
7039 return err;
7040
7041 err = bpf_adj_linfo_after_remove(env, off, cnt);
7042 if (err)
7043 return err;
7044
7045 memmove(aux_data + off, aux_data + off + cnt,
7046 sizeof(*aux_data) * (orig_prog_len - off - cnt));
7047
7048 return 0;
7049}
7050
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007051/* The verifier does more data flow analysis than llvm and will not
7052 * explore branches that are dead at run time. Malicious programs can
7053 * have dead code too. Therefore replace all dead at-run-time code
7054 * with 'ja -1'.
7055 *
7056 * Just nops are not optimal, e.g. if they would sit at the end of the
7057 * program and through another bug we would manage to jump there, then
7058 * we'd execute beyond program memory otherwise. Returning exception
7059 * code also wouldn't work since we can have subprogs where the dead
7060 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007061 */
7062static void sanitize_dead_code(struct bpf_verifier_env *env)
7063{
7064 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007065 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007066 struct bpf_insn *insn = env->prog->insnsi;
7067 const int insn_cnt = env->prog->len;
7068 int i;
7069
7070 for (i = 0; i < insn_cnt; i++) {
7071 if (aux_data[i].seen)
7072 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007073 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007074 }
7075}
7076
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007077static bool insn_is_cond_jump(u8 code)
7078{
7079 u8 op;
7080
Jiong Wang092ed092019-01-26 12:26:01 -05007081 if (BPF_CLASS(code) == BPF_JMP32)
7082 return true;
7083
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007084 if (BPF_CLASS(code) != BPF_JMP)
7085 return false;
7086
7087 op = BPF_OP(code);
7088 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
7089}
7090
7091static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
7092{
7093 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7094 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7095 struct bpf_insn *insn = env->prog->insnsi;
7096 const int insn_cnt = env->prog->len;
7097 int i;
7098
7099 for (i = 0; i < insn_cnt; i++, insn++) {
7100 if (!insn_is_cond_jump(insn->code))
7101 continue;
7102
7103 if (!aux_data[i + 1].seen)
7104 ja.off = insn->off;
7105 else if (!aux_data[i + 1 + insn->off].seen)
7106 ja.off = 0;
7107 else
7108 continue;
7109
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007110 if (bpf_prog_is_dev_bound(env->prog->aux))
7111 bpf_prog_offload_replace_insn(env, i, &ja);
7112
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007113 memcpy(insn, &ja, sizeof(ja));
7114 }
7115}
7116
Jakub Kicinski52875a02019-01-22 22:45:20 -08007117static int opt_remove_dead_code(struct bpf_verifier_env *env)
7118{
7119 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7120 int insn_cnt = env->prog->len;
7121 int i, err;
7122
7123 for (i = 0; i < insn_cnt; i++) {
7124 int j;
7125
7126 j = 0;
7127 while (i + j < insn_cnt && !aux_data[i + j].seen)
7128 j++;
7129 if (!j)
7130 continue;
7131
7132 err = verifier_remove_insns(env, i, j);
7133 if (err)
7134 return err;
7135 insn_cnt = env->prog->len;
7136 }
7137
7138 return 0;
7139}
7140
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007141static int opt_remove_nops(struct bpf_verifier_env *env)
7142{
7143 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7144 struct bpf_insn *insn = env->prog->insnsi;
7145 int insn_cnt = env->prog->len;
7146 int i, err;
7147
7148 for (i = 0; i < insn_cnt; i++) {
7149 if (memcmp(&insn[i], &ja, sizeof(ja)))
7150 continue;
7151
7152 err = verifier_remove_insns(env, i, 1);
7153 if (err)
7154 return err;
7155 insn_cnt--;
7156 i--;
7157 }
7158
7159 return 0;
7160}
7161
Joe Stringerc64b7982018-10-02 13:35:33 -07007162/* convert load instructions that access fields of a context type into a
7163 * sequence of instructions that access fields of the underlying structure:
7164 * struct __sk_buff -> struct sk_buff
7165 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007166 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007167static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007168{
Jakub Kicinski00176a32017-10-16 16:40:54 -07007169 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007170 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007171 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007172 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007173 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007174 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007175 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007176 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007177
Daniel Borkmannb09928b2018-10-24 22:05:49 +02007178 if (ops->gen_prologue || env->seen_direct_write) {
7179 if (!ops->gen_prologue) {
7180 verbose(env, "bpf verifier is misconfigured\n");
7181 return -EINVAL;
7182 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007183 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
7184 env->prog);
7185 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007186 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007187 return -EINVAL;
7188 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07007189 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007190 if (!new_prog)
7191 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007192
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007193 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007194 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007195 }
7196 }
7197
Joe Stringerc64b7982018-10-02 13:35:33 -07007198 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007199 return 0;
7200
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007201 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007202
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007203 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07007204 bpf_convert_ctx_access_t convert_ctx_access;
7205
Daniel Borkmann62c79892017-01-12 11:51:33 +01007206 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
7207 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
7208 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007209 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007210 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01007211 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
7212 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
7213 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007214 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007215 type = BPF_WRITE;
7216 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007217 continue;
7218
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07007219 if (type == BPF_WRITE &&
7220 env->insn_aux_data[i + delta].sanitize_stack_off) {
7221 struct bpf_insn patch[] = {
7222 /* Sanitize suspicious stack slot with zero.
7223 * There are no memory dependencies for this store,
7224 * since it's only using frame pointer and immediate
7225 * constant of zero
7226 */
7227 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
7228 env->insn_aux_data[i + delta].sanitize_stack_off,
7229 0),
7230 /* the original STX instruction will immediately
7231 * overwrite the same stack slot with appropriate value
7232 */
7233 *insn,
7234 };
7235
7236 cnt = ARRAY_SIZE(patch);
7237 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
7238 if (!new_prog)
7239 return -ENOMEM;
7240
7241 delta += cnt - 1;
7242 env->prog = new_prog;
7243 insn = new_prog->insnsi + i + delta;
7244 continue;
7245 }
7246
Joe Stringerc64b7982018-10-02 13:35:33 -07007247 switch (env->insn_aux_data[i + delta].ptr_type) {
7248 case PTR_TO_CTX:
7249 if (!ops->convert_ctx_access)
7250 continue;
7251 convert_ctx_access = ops->convert_ctx_access;
7252 break;
7253 case PTR_TO_SOCKET:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08007254 case PTR_TO_SOCK_COMMON:
Joe Stringerc64b7982018-10-02 13:35:33 -07007255 convert_ctx_access = bpf_sock_convert_ctx_access;
7256 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08007257 case PTR_TO_TCP_SOCK:
7258 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
7259 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07007260 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007261 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07007262 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007263
Yonghong Song31fd8582017-06-13 15:52:13 -07007264 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007265 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07007266
7267 /* If the read access is a narrower load of the field,
7268 * convert to a 4/8-byte load, to minimum program type specific
7269 * convert_ctx_access changes. If conversion is successful,
7270 * we will apply proper mask to the result.
7271 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02007272 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007273 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
7274 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07007275 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02007276 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07007277
Daniel Borkmannf96da092017-07-02 02:13:27 +02007278 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007279 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02007280 return -EINVAL;
7281 }
7282
7283 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07007284 if (ctx_field_size == 4)
7285 size_code = BPF_W;
7286 else if (ctx_field_size == 8)
7287 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007288
Daniel Borkmannbc231052018-06-02 23:06:39 +02007289 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07007290 insn->code = BPF_LDX | BPF_MEM | size_code;
7291 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007292
7293 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07007294 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
7295 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02007296 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
7297 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007298 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007299 return -EINVAL;
7300 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02007301
7302 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007303 u8 shift = (off & (size_default - 1)) * 8;
7304
7305 if (ctx_field_size <= 4) {
7306 if (shift)
7307 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
7308 insn->dst_reg,
7309 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007310 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007311 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007312 } else {
7313 if (shift)
7314 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
7315 insn->dst_reg,
7316 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007317 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007318 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007319 }
Yonghong Song31fd8582017-06-13 15:52:13 -07007320 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007321
Alexei Starovoitov80419022017-03-15 18:26:41 -07007322 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007323 if (!new_prog)
7324 return -ENOMEM;
7325
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007326 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007327
7328 /* keep walking new program and skip insns we just inserted */
7329 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007330 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007331 }
7332
7333 return 0;
7334}
7335
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007336static int jit_subprogs(struct bpf_verifier_env *env)
7337{
7338 struct bpf_prog *prog = env->prog, **func, *tmp;
7339 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007340 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007341 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007342 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007343
Jiong Wangf910cef2018-05-02 16:17:17 -04007344 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007345 return 0;
7346
Daniel Borkmann7105e822017-12-20 13:42:57 +01007347 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007348 if (insn->code != (BPF_JMP | BPF_CALL) ||
7349 insn->src_reg != BPF_PSEUDO_CALL)
7350 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007351 /* Upon error here we cannot fall back to interpreter but
7352 * need a hard reject of the program. Thus -EFAULT is
7353 * propagated in any case.
7354 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007355 subprog = find_subprog(env, i + insn->imm + 1);
7356 if (subprog < 0) {
7357 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
7358 i + insn->imm + 1);
7359 return -EFAULT;
7360 }
7361 /* temporarily remember subprog id inside insn instead of
7362 * aux_data, since next loop will split up all insns into funcs
7363 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007364 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007365 /* remember original imm in case JIT fails and fallback
7366 * to interpreter will be needed
7367 */
7368 env->insn_aux_data[i].call_imm = insn->imm;
7369 /* point imm to __bpf_call_base+1 from JITs point of view */
7370 insn->imm = 1;
7371 }
7372
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007373 err = bpf_prog_alloc_jited_linfo(prog);
7374 if (err)
7375 goto out_undo_insn;
7376
7377 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07007378 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007379 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007380 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007381
Jiong Wangf910cef2018-05-02 16:17:17 -04007382 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007383 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007384 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007385
7386 len = subprog_end - subprog_start;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08007387 /* BPF_PROG_RUN doesn't call subprogs directly,
7388 * hence main prog stats include the runtime of subprogs.
7389 * subprogs don't have IDs and not reachable via prog_get_next_id
7390 * func[i]->aux->stats will never be accessed and stays NULL
7391 */
7392 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007393 if (!func[i])
7394 goto out_free;
7395 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
7396 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007397 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007398 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007399 if (bpf_prog_calc_tag(func[i]))
7400 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007401 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08007402 func[i]->aux->func_idx = i;
7403 /* the btf and func_info will be freed only at prog->aux */
7404 func[i]->aux->btf = prog->aux->btf;
7405 func[i]->aux->func_info = prog->aux->func_info;
7406
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007407 /* Use bpf_prog_F_tag to indicate functions in stack traces.
7408 * Long term would need debug info to populate names
7409 */
7410 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04007411 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007412 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007413 func[i]->aux->linfo = prog->aux->linfo;
7414 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
7415 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
7416 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007417 func[i] = bpf_int_jit_compile(func[i]);
7418 if (!func[i]->jited) {
7419 err = -ENOTSUPP;
7420 goto out_free;
7421 }
7422 cond_resched();
7423 }
7424 /* at this point all bpf functions were successfully JITed
7425 * now populate all bpf_calls with correct addresses and
7426 * run last pass of JIT
7427 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007428 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007429 insn = func[i]->insnsi;
7430 for (j = 0; j < func[i]->len; j++, insn++) {
7431 if (insn->code != (BPF_JMP | BPF_CALL) ||
7432 insn->src_reg != BPF_PSEUDO_CALL)
7433 continue;
7434 subprog = insn->off;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007435 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
7436 func[subprog]->bpf_func -
7437 __bpf_call_base;
7438 }
Sandipan Das2162fed2018-05-24 12:26:45 +05307439
7440 /* we use the aux data to keep a list of the start addresses
7441 * of the JITed images for each function in the program
7442 *
7443 * for some architectures, such as powerpc64, the imm field
7444 * might not be large enough to hold the offset of the start
7445 * address of the callee's JITed image from __bpf_call_base
7446 *
7447 * in such cases, we can lookup the start address of a callee
7448 * by using its subprog id, available from the off field of
7449 * the call instruction, as an index for this list
7450 */
7451 func[i]->aux->func = func;
7452 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007453 }
Jiong Wangf910cef2018-05-02 16:17:17 -04007454 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007455 old_bpf_func = func[i]->bpf_func;
7456 tmp = bpf_int_jit_compile(func[i]);
7457 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
7458 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007459 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007460 goto out_free;
7461 }
7462 cond_resched();
7463 }
7464
7465 /* finally lock prog and jit images for all functions and
7466 * populate kallsysm
7467 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007468 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007469 bpf_prog_lock_ro(func[i]);
7470 bpf_prog_kallsyms_add(func[i]);
7471 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01007472
7473 /* Last step: make now unused interpreter insns from main
7474 * prog consistent for later dump requests, so they can
7475 * later look the same as if they were interpreted only.
7476 */
7477 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01007478 if (insn->code != (BPF_JMP | BPF_CALL) ||
7479 insn->src_reg != BPF_PSEUDO_CALL)
7480 continue;
7481 insn->off = env->insn_aux_data[i].call_imm;
7482 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05307483 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007484 }
7485
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007486 prog->jited = 1;
7487 prog->bpf_func = func[0]->bpf_func;
7488 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04007489 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007490 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007491 return 0;
7492out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04007493 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007494 if (func[i])
7495 bpf_jit_free(func[i]);
7496 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007497out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007498 /* cleanup main prog to be interpreted */
7499 prog->jit_requested = 0;
7500 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7501 if (insn->code != (BPF_JMP | BPF_CALL) ||
7502 insn->src_reg != BPF_PSEUDO_CALL)
7503 continue;
7504 insn->off = 0;
7505 insn->imm = env->insn_aux_data[i].call_imm;
7506 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007507 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007508 return err;
7509}
7510
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007511static int fixup_call_args(struct bpf_verifier_env *env)
7512{
David S. Miller19d28fb2018-01-11 21:27:54 -05007513#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007514 struct bpf_prog *prog = env->prog;
7515 struct bpf_insn *insn = prog->insnsi;
7516 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05007517#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01007518 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007519
Quentin Monnete4052d02018-10-07 12:56:58 +01007520 if (env->prog->jit_requested &&
7521 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05007522 err = jit_subprogs(env);
7523 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007524 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007525 if (err == -EFAULT)
7526 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05007527 }
7528#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007529 for (i = 0; i < prog->len; i++, insn++) {
7530 if (insn->code != (BPF_JMP | BPF_CALL) ||
7531 insn->src_reg != BPF_PSEUDO_CALL)
7532 continue;
7533 depth = get_callee_stack_depth(env, insn, i);
7534 if (depth < 0)
7535 return depth;
7536 bpf_patch_call_args(insn, depth);
7537 }
David S. Miller19d28fb2018-01-11 21:27:54 -05007538 err = 0;
7539#endif
7540 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007541}
7542
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007543/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007544 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007545 *
7546 * this function is called after eBPF program passed verification
7547 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007548static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007549{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007550 struct bpf_prog *prog = env->prog;
7551 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007552 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007553 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007554 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007555 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007556 struct bpf_insn insn_buf[16];
7557 struct bpf_prog *new_prog;
7558 struct bpf_map *map_ptr;
7559 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007560
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007561 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007562 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
7563 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7564 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007565 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007566 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
7567 struct bpf_insn mask_and_div[] = {
7568 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7569 /* Rx div 0 -> 0 */
7570 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
7571 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
7572 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
7573 *insn,
7574 };
7575 struct bpf_insn mask_and_mod[] = {
7576 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7577 /* Rx mod 0 -> Rx */
7578 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
7579 *insn,
7580 };
7581 struct bpf_insn *patchlet;
7582
7583 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7584 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7585 patchlet = mask_and_div + (is64 ? 1 : 0);
7586 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
7587 } else {
7588 patchlet = mask_and_mod + (is64 ? 1 : 0);
7589 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
7590 }
7591
7592 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007593 if (!new_prog)
7594 return -ENOMEM;
7595
7596 delta += cnt - 1;
7597 env->prog = prog = new_prog;
7598 insn = new_prog->insnsi + i + delta;
7599 continue;
7600 }
7601
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02007602 if (BPF_CLASS(insn->code) == BPF_LD &&
7603 (BPF_MODE(insn->code) == BPF_ABS ||
7604 BPF_MODE(insn->code) == BPF_IND)) {
7605 cnt = env->ops->gen_ld_abs(insn, insn_buf);
7606 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7607 verbose(env, "bpf verifier is misconfigured\n");
7608 return -EINVAL;
7609 }
7610
7611 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7612 if (!new_prog)
7613 return -ENOMEM;
7614
7615 delta += cnt - 1;
7616 env->prog = prog = new_prog;
7617 insn = new_prog->insnsi + i + delta;
7618 continue;
7619 }
7620
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007621 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
7622 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
7623 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
7624 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
7625 struct bpf_insn insn_buf[16];
7626 struct bpf_insn *patch = &insn_buf[0];
7627 bool issrc, isneg;
7628 u32 off_reg;
7629
7630 aux = &env->insn_aux_data[i + delta];
Daniel Borkmann3612af72019-03-01 22:05:29 +01007631 if (!aux->alu_state ||
7632 aux->alu_state == BPF_ALU_NON_POINTER)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007633 continue;
7634
7635 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
7636 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
7637 BPF_ALU_SANITIZE_SRC;
7638
7639 off_reg = issrc ? insn->src_reg : insn->dst_reg;
7640 if (isneg)
7641 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7642 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
7643 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
7644 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
7645 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
7646 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
7647 if (issrc) {
7648 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
7649 off_reg);
7650 insn->src_reg = BPF_REG_AX;
7651 } else {
7652 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
7653 BPF_REG_AX);
7654 }
7655 if (isneg)
7656 insn->code = insn->code == code_add ?
7657 code_sub : code_add;
7658 *patch++ = *insn;
7659 if (issrc && isneg)
7660 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7661 cnt = patch - insn_buf;
7662
7663 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7664 if (!new_prog)
7665 return -ENOMEM;
7666
7667 delta += cnt - 1;
7668 env->prog = prog = new_prog;
7669 insn = new_prog->insnsi + i + delta;
7670 continue;
7671 }
7672
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007673 if (insn->code != (BPF_JMP | BPF_CALL))
7674 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007675 if (insn->src_reg == BPF_PSEUDO_CALL)
7676 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007677
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007678 if (insn->imm == BPF_FUNC_get_route_realm)
7679 prog->dst_needed = 1;
7680 if (insn->imm == BPF_FUNC_get_prandom_u32)
7681 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05007682 if (insn->imm == BPF_FUNC_override_return)
7683 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007684 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04007685 /* If we tail call into other programs, we
7686 * cannot make any assumptions since they can
7687 * be replaced dynamically during runtime in
7688 * the program array.
7689 */
7690 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07007691 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05007692 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04007693
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007694 /* mark bpf_tail_call as different opcode to avoid
7695 * conditional branch in the interpeter for every normal
7696 * call and to prevent accidental JITing by JIT compiler
7697 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007698 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007699 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07007700 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007701
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007702 aux = &env->insn_aux_data[i + delta];
7703 if (!bpf_map_ptr_unpriv(aux))
7704 continue;
7705
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007706 /* instead of changing every JIT dealing with tail_call
7707 * emit two extra insns:
7708 * if (index >= max_entries) goto out;
7709 * index &= array->index_mask;
7710 * to avoid out-of-bounds cpu speculation
7711 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007712 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00007713 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007714 return -EINVAL;
7715 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007716
7717 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007718 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
7719 map_ptr->max_entries, 2);
7720 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
7721 container_of(map_ptr,
7722 struct bpf_array,
7723 map)->index_mask);
7724 insn_buf[2] = *insn;
7725 cnt = 3;
7726 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7727 if (!new_prog)
7728 return -ENOMEM;
7729
7730 delta += cnt - 1;
7731 env->prog = prog = new_prog;
7732 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007733 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007734 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007735
Daniel Borkmann89c63072017-08-19 03:12:45 +02007736 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02007737 * and other inlining handlers are currently limited to 64 bit
7738 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02007739 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08007740 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02007741 (insn->imm == BPF_FUNC_map_lookup_elem ||
7742 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02007743 insn->imm == BPF_FUNC_map_delete_elem ||
7744 insn->imm == BPF_FUNC_map_push_elem ||
7745 insn->imm == BPF_FUNC_map_pop_elem ||
7746 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007747 aux = &env->insn_aux_data[i + delta];
7748 if (bpf_map_ptr_poisoned(aux))
7749 goto patch_call_imm;
7750
7751 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02007752 ops = map_ptr->ops;
7753 if (insn->imm == BPF_FUNC_map_lookup_elem &&
7754 ops->map_gen_lookup) {
7755 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
7756 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7757 verbose(env, "bpf verifier is misconfigured\n");
7758 return -EINVAL;
7759 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007760
Daniel Borkmann09772d92018-06-02 23:06:35 +02007761 new_prog = bpf_patch_insn_data(env, i + delta,
7762 insn_buf, cnt);
7763 if (!new_prog)
7764 return -ENOMEM;
7765
7766 delta += cnt - 1;
7767 env->prog = prog = new_prog;
7768 insn = new_prog->insnsi + i + delta;
7769 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007770 }
7771
Daniel Borkmann09772d92018-06-02 23:06:35 +02007772 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
7773 (void *(*)(struct bpf_map *map, void *key))NULL));
7774 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
7775 (int (*)(struct bpf_map *map, void *key))NULL));
7776 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
7777 (int (*)(struct bpf_map *map, void *key, void *value,
7778 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02007779 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
7780 (int (*)(struct bpf_map *map, void *value,
7781 u64 flags))NULL));
7782 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
7783 (int (*)(struct bpf_map *map, void *value))NULL));
7784 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
7785 (int (*)(struct bpf_map *map, void *value))NULL));
7786
Daniel Borkmann09772d92018-06-02 23:06:35 +02007787 switch (insn->imm) {
7788 case BPF_FUNC_map_lookup_elem:
7789 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
7790 __bpf_call_base;
7791 continue;
7792 case BPF_FUNC_map_update_elem:
7793 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
7794 __bpf_call_base;
7795 continue;
7796 case BPF_FUNC_map_delete_elem:
7797 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
7798 __bpf_call_base;
7799 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02007800 case BPF_FUNC_map_push_elem:
7801 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
7802 __bpf_call_base;
7803 continue;
7804 case BPF_FUNC_map_pop_elem:
7805 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
7806 __bpf_call_base;
7807 continue;
7808 case BPF_FUNC_map_peek_elem:
7809 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
7810 __bpf_call_base;
7811 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007812 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007813
Daniel Borkmann09772d92018-06-02 23:06:35 +02007814 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007815 }
7816
7817patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07007818 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007819 /* all functions that have prototype and verifier allowed
7820 * programs to call them, must be real in-kernel functions
7821 */
7822 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007823 verbose(env,
7824 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007825 func_id_name(insn->imm), insn->imm);
7826 return -EFAULT;
7827 }
7828 insn->imm = fn->func - __bpf_call_base;
7829 }
7830
7831 return 0;
7832}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007833
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007834static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007835{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007836 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007837 int i;
7838
7839 if (!env->explored_states)
7840 return;
7841
7842 for (i = 0; i < env->prog->len; i++) {
7843 sl = env->explored_states[i];
7844
7845 if (sl)
7846 while (sl != STATE_LIST_MARK) {
7847 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07007848 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007849 kfree(sl);
7850 sl = sln;
7851 }
7852 }
7853
7854 kfree(env->explored_states);
7855}
7856
Yonghong Song838e9692018-11-19 15:29:11 -08007857int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
7858 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007859{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007860 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07007861 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007862 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007863 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007864
Arnd Bergmanneba0c922017-11-02 12:05:52 +01007865 /* no program is valid */
7866 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
7867 return -EINVAL;
7868
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007869 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007870 * allocate/free it every time bpf_check() is called
7871 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007872 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007873 if (!env)
7874 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007875 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007876
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007877 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07007878 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007879 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007880 ret = -ENOMEM;
7881 if (!env->insn_aux_data)
7882 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007883 for (i = 0; i < len; i++)
7884 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007885 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07007886 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007887
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007888 /* grab the mutex to protect few globals used by verifier */
7889 mutex_lock(&bpf_verifier_lock);
7890
7891 if (attr->log_level || attr->log_buf || attr->log_size) {
7892 /* user requested verbose verifier output
7893 * and supplied buffer to store the verification trace
7894 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07007895 log->level = attr->log_level;
7896 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
7897 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007898
7899 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07007900 /* log attributes have to be sane */
7901 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
7902 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007903 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007904 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02007905
7906 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
7907 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07007908 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08007909 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
7910 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007911
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007912 is_priv = capable(CAP_SYS_ADMIN);
7913 env->allow_ptr_leaks = is_priv;
7914
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007915 ret = replace_map_fd_with_map_ptr(env);
7916 if (ret < 0)
7917 goto skip_full_check;
7918
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07007919 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00007920 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07007921 if (ret)
7922 goto skip_full_check;
7923 }
7924
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007925 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007926 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007927 GFP_USER);
7928 ret = -ENOMEM;
7929 if (!env->explored_states)
7930 goto skip_full_check;
7931
Martin KaFai Laud9762e82018-12-13 10:41:48 -08007932 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07007933 if (ret < 0)
7934 goto skip_full_check;
7935
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007936 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08007937 if (ret < 0)
7938 goto skip_full_check;
7939
Martin KaFai Laud9762e82018-12-13 10:41:48 -08007940 ret = check_cfg(env);
7941 if (ret < 0)
7942 goto skip_full_check;
7943
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007944 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04007945 if (env->cur_state) {
7946 free_verifier_state(env->cur_state, true);
7947 env->cur_state = NULL;
7948 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007949
Quentin Monnetc941ce92018-10-07 12:56:47 +01007950 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
7951 ret = bpf_prog_offload_finalize(env);
7952
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007953skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007954 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007955 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007956
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007957 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08007958 ret = check_max_stack_depth(env);
7959
Jakub Kicinski9b38c402018-12-19 22:13:06 -08007960 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007961 if (is_priv) {
7962 if (ret == 0)
7963 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08007964 if (ret == 0)
7965 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007966 if (ret == 0)
7967 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08007968 } else {
7969 if (ret == 0)
7970 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007971 }
7972
Jakub Kicinski9b38c402018-12-19 22:13:06 -08007973 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007974 /* program is valid, convert *(u32*)(ctx + off) accesses */
7975 ret = convert_ctx_accesses(env);
7976
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007977 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007978 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007979
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007980 if (ret == 0)
7981 ret = fixup_call_args(env);
7982
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007983 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007984 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007985 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007986 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007987 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007988 }
7989
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007990 if (ret == 0 && env->used_map_cnt) {
7991 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007992 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
7993 sizeof(env->used_maps[0]),
7994 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007995
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007996 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007997 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007998 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007999 }
8000
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008001 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008002 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008003 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008004
8005 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
8006 * bpf_ld_imm64 instructions
8007 */
8008 convert_pseudo_ld_imm64(env);
8009 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008010
Yonghong Songba64e7d2018-11-24 23:20:44 -08008011 if (ret == 0)
8012 adjust_btf_func(env);
8013
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008014err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008015 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008016 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07008017 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008018 */
8019 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008020 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008021err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008022 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008023 vfree(env->insn_aux_data);
8024err_free_env:
8025 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008026 return ret;
8027}