blob: 25baa3c8cdd2908d3b29c09ff78e20e3bedffa35 [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
Alexei Starovoitovb285fcb2019-05-21 20:14:19 -0700179#define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192
Alexei Starovoitovceefbc92018-12-03 22:46:06 -0800180#define BPF_COMPLEXITY_LIMIT_STATES 64
Daniel Borkmann07016152016-04-05 22:33:17 +0200181
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200182#define BPF_MAP_PTR_UNPRIV 1UL
183#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
184 POISON_POINTER_DELTA))
185#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
186
187static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
188{
189 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
190}
191
192static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
193{
194 return aux->map_state & BPF_MAP_PTR_UNPRIV;
195}
196
197static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
198 const struct bpf_map *map, bool unpriv)
199{
200 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
201 unpriv |= bpf_map_ptr_unpriv(aux);
202 aux->map_state = (unsigned long)map |
203 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
204}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700205
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200206struct bpf_call_arg_meta {
207 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200208 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200209 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200210 int regno;
211 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700212 s64 msize_smax_value;
213 u64 msize_umax_value;
Martin KaFai Lau1b986582019-03-12 10:23:02 -0700214 int ref_obj_id;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800215 int func_id;
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200216};
217
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700218static DEFINE_MUTEX(bpf_verifier_lock);
219
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800220static const struct bpf_line_info *
221find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
222{
223 const struct bpf_line_info *linfo;
224 const struct bpf_prog *prog;
225 u32 i, nr_linfo;
226
227 prog = env->prog;
228 nr_linfo = prog->aux->nr_linfo;
229
230 if (!nr_linfo || insn_off >= prog->len)
231 return NULL;
232
233 linfo = prog->aux->linfo;
234 for (i = 1; i < nr_linfo; i++)
235 if (insn_off < linfo[i].insn_off)
236 break;
237
238 return &linfo[i - 1];
239}
240
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700241void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
242 va_list args)
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700243{
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700244 unsigned int n;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700245
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700246 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700247
248 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
249 "verifier log line truncated - local buffer too short\n");
250
251 n = min(log->len_total - log->len_used - 1, n);
252 log->kbuf[n] = '\0';
253
254 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
255 log->len_used += n;
256 else
257 log->ubuf = NULL;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700258}
Jiri Olsaabe08842018-03-23 11:41:28 +0100259
260/* log_level controls verbosity level of eBPF verifier.
261 * bpf_verifier_log_write() is used to dump the verification trace to the log,
262 * so the user can figure out what's wrong with the program
Quentin Monnet430e68d2018-01-10 12:26:06 +0000263 */
Jiri Olsaabe08842018-03-23 11:41:28 +0100264__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
265 const char *fmt, ...)
266{
267 va_list args;
268
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700269 if (!bpf_verifier_log_needed(&env->log))
270 return;
271
Jiri Olsaabe08842018-03-23 11:41:28 +0100272 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700273 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100274 va_end(args);
275}
276EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
277
278__printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
279{
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700280 struct bpf_verifier_env *env = private_data;
Jiri Olsaabe08842018-03-23 11:41:28 +0100281 va_list args;
282
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700283 if (!bpf_verifier_log_needed(&env->log))
284 return;
285
Jiri Olsaabe08842018-03-23 11:41:28 +0100286 va_start(args, fmt);
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700287 bpf_verifier_vlog(&env->log, fmt, args);
Jiri Olsaabe08842018-03-23 11:41:28 +0100288 va_end(args);
289}
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700290
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800291static const char *ltrim(const char *s)
292{
293 while (isspace(*s))
294 s++;
295
296 return s;
297}
298
299__printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
300 u32 insn_off,
301 const char *prefix_fmt, ...)
302{
303 const struct bpf_line_info *linfo;
304
305 if (!bpf_verifier_log_needed(&env->log))
306 return;
307
308 linfo = find_linfo(env, insn_off);
309 if (!linfo || linfo == env->prev_linfo)
310 return;
311
312 if (prefix_fmt) {
313 va_list args;
314
315 va_start(args, prefix_fmt);
316 bpf_verifier_vlog(&env->log, prefix_fmt, args);
317 va_end(args);
318 }
319
320 verbose(env, "%s\n",
321 ltrim(btf_name_by_offset(env->prog->aux->btf,
322 linfo->line_off)));
323
324 env->prev_linfo = linfo;
325}
326
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200327static bool type_is_pkt_pointer(enum bpf_reg_type type)
328{
329 return type == PTR_TO_PACKET ||
330 type == PTR_TO_PACKET_META;
331}
332
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800333static bool type_is_sk_pointer(enum bpf_reg_type type)
334{
335 return type == PTR_TO_SOCKET ||
Martin KaFai Lau655a51e2019-02-09 23:22:24 -0800336 type == PTR_TO_SOCK_COMMON ||
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700337 type == PTR_TO_TCP_SOCK ||
338 type == PTR_TO_XDP_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",
Matt Mullins9df1c282019-04-26 11:49:47 -0700409 [PTR_TO_TP_BUFFER] = "tp_buffer",
Jonathan Lemonfada7fd2019-06-06 13:59:40 -0700410 [PTR_TO_XDP_SOCK] = "xdp_sock",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700411};
412
Edward Cree8efea212018-08-22 20:02:44 +0100413static char slot_type_char[] = {
414 [STACK_INVALID] = '?',
415 [STACK_SPILL] = 'r',
416 [STACK_MISC] = 'm',
417 [STACK_ZERO] = '0',
418};
419
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800420static void print_liveness(struct bpf_verifier_env *env,
421 enum bpf_reg_liveness live)
422{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800423 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800424 verbose(env, "_");
425 if (live & REG_LIVE_READ)
426 verbose(env, "r");
427 if (live & REG_LIVE_WRITTEN)
428 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800429 if (live & REG_LIVE_DONE)
430 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800431}
432
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800433static struct bpf_func_state *func(struct bpf_verifier_env *env,
434 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700435{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800436 struct bpf_verifier_state *cur = env->cur_state;
437
438 return cur->frame[reg->frameno];
439}
440
441static void print_verifier_state(struct bpf_verifier_env *env,
442 const struct bpf_func_state *state)
443{
444 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700445 enum bpf_reg_type t;
446 int i;
447
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800448 if (state->frameno)
449 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700450 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700451 reg = &state->regs[i];
452 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700453 if (t == NOT_INIT)
454 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800455 verbose(env, " R%d", i);
456 print_liveness(env, reg->live);
457 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100458 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
459 tnum_is_const(reg->var_off)) {
460 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700461 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800462 if (t == PTR_TO_STACK)
463 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100464 } else {
Martin KaFai Laucba368c2019-03-18 10:37:13 -0700465 verbose(env, "(id=%d", reg->id);
466 if (reg_type_may_be_refcounted_or_null(t))
467 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
Edward Creef1174f72017-08-07 15:26:19 +0100468 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700469 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200470 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700471 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100472 else if (t == CONST_PTR_TO_MAP ||
473 t == PTR_TO_MAP_VALUE ||
474 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700475 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100476 reg->map_ptr->key_size,
477 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100478 if (tnum_is_const(reg->var_off)) {
479 /* Typically an immediate SCALAR_VALUE, but
480 * could be a pointer whose offset is too big
481 * for reg->off
482 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700483 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100484 } else {
485 if (reg->smin_value != reg->umin_value &&
486 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700487 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100488 (long long)reg->smin_value);
489 if (reg->smax_value != reg->umax_value &&
490 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700491 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100492 (long long)reg->smax_value);
493 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700494 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100495 (unsigned long long)reg->umin_value);
496 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700497 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100498 (unsigned long long)reg->umax_value);
499 if (!tnum_is_unknown(reg->var_off)) {
500 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100501
Edward Cree7d1238f2017-08-07 15:26:56 +0100502 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700503 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100504 }
Edward Creef1174f72017-08-07 15:26:19 +0100505 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700506 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100507 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700508 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700509 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100510 char types_buf[BPF_REG_SIZE + 1];
511 bool valid = false;
512 int j;
513
514 for (j = 0; j < BPF_REG_SIZE; j++) {
515 if (state->stack[i].slot_type[j] != STACK_INVALID)
516 valid = true;
517 types_buf[j] = slot_type_char[
518 state->stack[i].slot_type[j]];
519 }
520 types_buf[BPF_REG_SIZE] = 0;
521 if (!valid)
522 continue;
523 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
524 print_liveness(env, state->stack[i].spilled_ptr.live);
525 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800526 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700527 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100528 else
529 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700530 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700531 if (state->acquired_refs && state->refs[0].id) {
532 verbose(env, " refs=%d", state->refs[0].id);
533 for (i = 1; i < state->acquired_refs; i++)
534 if (state->refs[i].id)
535 verbose(env, ",%d", state->refs[i].id);
536 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700537 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700538}
539
Joe Stringer84dbf352018-10-02 13:35:34 -0700540#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
541static int copy_##NAME##_state(struct bpf_func_state *dst, \
542 const struct bpf_func_state *src) \
543{ \
544 if (!src->FIELD) \
545 return 0; \
546 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
547 /* internal bug, make state invalid to reject the program */ \
548 memset(dst, 0, sizeof(*dst)); \
549 return -EFAULT; \
550 } \
551 memcpy(dst->FIELD, src->FIELD, \
552 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
553 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700554}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700555/* copy_reference_state() */
556COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700557/* copy_stack_state() */
558COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
559#undef COPY_STATE_FN
560
561#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
562static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
563 bool copy_old) \
564{ \
565 u32 old_size = state->COUNT; \
566 struct bpf_##NAME##_state *new_##FIELD; \
567 int slot = size / SIZE; \
568 \
569 if (size <= old_size || !size) { \
570 if (copy_old) \
571 return 0; \
572 state->COUNT = slot * SIZE; \
573 if (!size && old_size) { \
574 kfree(state->FIELD); \
575 state->FIELD = NULL; \
576 } \
577 return 0; \
578 } \
579 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
580 GFP_KERNEL); \
581 if (!new_##FIELD) \
582 return -ENOMEM; \
583 if (copy_old) { \
584 if (state->FIELD) \
585 memcpy(new_##FIELD, state->FIELD, \
586 sizeof(*new_##FIELD) * (old_size / SIZE)); \
587 memset(new_##FIELD + old_size / SIZE, 0, \
588 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
589 } \
590 state->COUNT = slot * SIZE; \
591 kfree(state->FIELD); \
592 state->FIELD = new_##FIELD; \
593 return 0; \
594}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700595/* realloc_reference_state() */
596REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700597/* realloc_stack_state() */
598REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
599#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700600
601/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
602 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800603 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700604 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
605 * which realloc_stack_state() copies over. It points to previous
606 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700607 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700608static int realloc_func_state(struct bpf_func_state *state, int stack_size,
609 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700610{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700611 int err = realloc_reference_state(state, refs_size, copy_old);
612 if (err)
613 return err;
614 return realloc_stack_state(state, stack_size, copy_old);
615}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700616
Joe Stringerfd978bf72018-10-02 13:35:35 -0700617/* Acquire a pointer id from the env and update the state->refs to include
618 * this new pointer reference.
619 * On success, returns a valid pointer id to associate with the register
620 * On failure, returns a negative errno.
621 */
622static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
623{
624 struct bpf_func_state *state = cur_func(env);
625 int new_ofs = state->acquired_refs;
626 int id, err;
627
628 err = realloc_reference_state(state, state->acquired_refs + 1, true);
629 if (err)
630 return err;
631 id = ++env->id_gen;
632 state->refs[new_ofs].id = id;
633 state->refs[new_ofs].insn_idx = insn_idx;
634
635 return id;
636}
637
638/* release function corresponding to acquire_reference_state(). Idempotent. */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800639static int release_reference_state(struct bpf_func_state *state, int ptr_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -0700640{
641 int i, last_idx;
642
Joe Stringerfd978bf72018-10-02 13:35:35 -0700643 last_idx = state->acquired_refs - 1;
644 for (i = 0; i < state->acquired_refs; i++) {
645 if (state->refs[i].id == ptr_id) {
646 if (last_idx && i != last_idx)
647 memcpy(&state->refs[i], &state->refs[last_idx],
648 sizeof(*state->refs));
649 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
650 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700651 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700652 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700653 }
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -0800654 return -EINVAL;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700655}
656
657static int transfer_reference_state(struct bpf_func_state *dst,
658 struct bpf_func_state *src)
659{
660 int err = realloc_reference_state(dst, src->acquired_refs, false);
661 if (err)
662 return err;
663 err = copy_reference_state(dst, src);
664 if (err)
665 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700666 return 0;
667}
668
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800669static void free_func_state(struct bpf_func_state *state)
670{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800671 if (!state)
672 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700673 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800674 kfree(state->stack);
675 kfree(state);
676}
677
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700678static void free_verifier_state(struct bpf_verifier_state *state,
679 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700680{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800681 int i;
682
683 for (i = 0; i <= state->curframe; i++) {
684 free_func_state(state->frame[i]);
685 state->frame[i] = NULL;
686 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700687 if (free_self)
688 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700689}
690
691/* copy verifier state from src to dst growing dst stack space
692 * when necessary to accommodate larger src stack
693 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800694static int copy_func_state(struct bpf_func_state *dst,
695 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700696{
697 int err;
698
Joe Stringerfd978bf72018-10-02 13:35:35 -0700699 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
700 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700701 if (err)
702 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700703 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
704 err = copy_reference_state(dst, src);
705 if (err)
706 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700707 return copy_stack_state(dst, src);
708}
709
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800710static int copy_verifier_state(struct bpf_verifier_state *dst_state,
711 const struct bpf_verifier_state *src)
712{
713 struct bpf_func_state *dst;
714 int i, err;
715
716 /* if dst has more stack frames then src frame, free them */
717 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
718 free_func_state(dst_state->frame[i]);
719 dst_state->frame[i] = NULL;
720 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100721 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800722 dst_state->curframe = src->curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800723 dst_state->active_spin_lock = src->active_spin_lock;
Alexei Starovoitov25897262019-06-15 12:12:20 -0700724 dst_state->branches = src->branches;
725 dst_state->parent = src->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800726 for (i = 0; i <= src->curframe; i++) {
727 dst = dst_state->frame[i];
728 if (!dst) {
729 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
730 if (!dst)
731 return -ENOMEM;
732 dst_state->frame[i] = dst;
733 }
734 err = copy_func_state(dst, src->frame[i]);
735 if (err)
736 return err;
737 }
738 return 0;
739}
740
Alexei Starovoitov25897262019-06-15 12:12:20 -0700741static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
742{
743 while (st) {
744 u32 br = --st->branches;
745
746 /* WARN_ON(br > 1) technically makes sense here,
747 * but see comment in push_stack(), hence:
748 */
749 WARN_ONCE((int)br < 0,
750 "BUG update_branch_counts:branches_to_explore=%d\n",
751 br);
752 if (br)
753 break;
754 st = st->parent;
755 }
756}
757
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700758static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
759 int *insn_idx)
760{
761 struct bpf_verifier_state *cur = env->cur_state;
762 struct bpf_verifier_stack_elem *elem, *head = env->head;
763 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700764
765 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700766 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700767
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700768 if (cur) {
769 err = copy_verifier_state(cur, &head->st);
770 if (err)
771 return err;
772 }
773 if (insn_idx)
774 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700775 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700776 *prev_insn_idx = head->prev_insn_idx;
777 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700778 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700779 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700780 env->head = elem;
781 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700782 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700783}
784
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100785static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100786 int insn_idx, int prev_insn_idx,
787 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700788{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700789 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100790 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700791 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700792
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700793 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700794 if (!elem)
795 goto err;
796
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700797 elem->insn_idx = insn_idx;
798 elem->prev_insn_idx = prev_insn_idx;
799 elem->next = env->head;
800 env->head = elem;
801 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700802 err = copy_verifier_state(&elem->st, cur);
803 if (err)
804 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100805 elem->st.speculative |= speculative;
Alexei Starovoitovb285fcb2019-05-21 20:14:19 -0700806 if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) {
807 verbose(env, "The sequence of %d jumps is too complex.\n",
808 env->stack_size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700809 goto err;
810 }
Alexei Starovoitov25897262019-06-15 12:12:20 -0700811 if (elem->st.parent) {
812 ++elem->st.parent->branches;
813 /* WARN_ON(branches > 2) technically makes sense here,
814 * but
815 * 1. speculative states will bump 'branches' for non-branch
816 * instructions
817 * 2. is_state_visited() heuristics may decide not to create
818 * a new state for a sequence of branches and all such current
819 * and cloned states will be pointing to a single parent state
820 * which might have large 'branches' count.
821 */
822 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700823 return &elem->st;
824err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800825 free_verifier_state(env->cur_state, true);
826 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700827 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700828 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700829 return NULL;
830}
831
832#define CALLER_SAVED_REGS 6
833static const int caller_saved[CALLER_SAVED_REGS] = {
834 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
835};
836
Edward Creef1174f72017-08-07 15:26:19 +0100837static void __mark_reg_not_init(struct bpf_reg_state *reg);
838
Edward Creeb03c9f92017-08-07 15:26:36 +0100839/* Mark the unknown part of a register (variable offset or scalar value) as
840 * known to have the value @imm.
841 */
842static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
843{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700844 /* Clear id, off, and union(map_ptr, range) */
845 memset(((u8 *)reg) + sizeof(reg->type), 0,
846 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100847 reg->var_off = tnum_const(imm);
848 reg->smin_value = (s64)imm;
849 reg->smax_value = (s64)imm;
850 reg->umin_value = imm;
851 reg->umax_value = imm;
852}
853
Edward Creef1174f72017-08-07 15:26:19 +0100854/* Mark the 'variable offset' part of a register as zero. This should be
855 * used only on registers holding a pointer type.
856 */
857static void __mark_reg_known_zero(struct bpf_reg_state *reg)
858{
Edward Creeb03c9f92017-08-07 15:26:36 +0100859 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100860}
861
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800862static void __mark_reg_const_zero(struct bpf_reg_state *reg)
863{
864 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800865 reg->type = SCALAR_VALUE;
866}
867
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700868static void mark_reg_known_zero(struct bpf_verifier_env *env,
869 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100870{
871 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700872 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100873 /* Something bad happened, let's kill all regs */
874 for (regno = 0; regno < MAX_BPF_REG; regno++)
875 __mark_reg_not_init(regs + regno);
876 return;
877 }
878 __mark_reg_known_zero(regs + regno);
879}
880
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200881static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
882{
883 return type_is_pkt_pointer(reg->type);
884}
885
886static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
887{
888 return reg_is_pkt_pointer(reg) ||
889 reg->type == PTR_TO_PACKET_END;
890}
891
892/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
893static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
894 enum bpf_reg_type which)
895{
896 /* The register can already have a range from prior markings.
897 * This is fine as long as it hasn't been advanced from its
898 * origin.
899 */
900 return reg->type == which &&
901 reg->id == 0 &&
902 reg->off == 0 &&
903 tnum_equals_const(reg->var_off, 0);
904}
905
Edward Creeb03c9f92017-08-07 15:26:36 +0100906/* Attempts to improve min/max values based on var_off information */
907static void __update_reg_bounds(struct bpf_reg_state *reg)
908{
909 /* min signed is max(sign bit) | min(other bits) */
910 reg->smin_value = max_t(s64, reg->smin_value,
911 reg->var_off.value | (reg->var_off.mask & S64_MIN));
912 /* max signed is min(sign bit) | max(other bits) */
913 reg->smax_value = min_t(s64, reg->smax_value,
914 reg->var_off.value | (reg->var_off.mask & S64_MAX));
915 reg->umin_value = max(reg->umin_value, reg->var_off.value);
916 reg->umax_value = min(reg->umax_value,
917 reg->var_off.value | reg->var_off.mask);
918}
919
920/* Uses signed min/max values to inform unsigned, and vice-versa */
921static void __reg_deduce_bounds(struct bpf_reg_state *reg)
922{
923 /* Learn sign from signed bounds.
924 * If we cannot cross the sign boundary, then signed and unsigned bounds
925 * are the same, so combine. This works even in the negative case, e.g.
926 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
927 */
928 if (reg->smin_value >= 0 || reg->smax_value < 0) {
929 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
930 reg->umin_value);
931 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
932 reg->umax_value);
933 return;
934 }
935 /* Learn sign from unsigned bounds. Signed bounds cross the sign
936 * boundary, so we must be careful.
937 */
938 if ((s64)reg->umax_value >= 0) {
939 /* Positive. We can't learn anything from the smin, but smax
940 * is positive, hence safe.
941 */
942 reg->smin_value = reg->umin_value;
943 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
944 reg->umax_value);
945 } else if ((s64)reg->umin_value < 0) {
946 /* Negative. We can't learn anything from the smax, but smin
947 * is negative, hence safe.
948 */
949 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
950 reg->umin_value);
951 reg->smax_value = reg->umax_value;
952 }
953}
954
955/* Attempts to improve var_off based on unsigned min/max information */
956static void __reg_bound_offset(struct bpf_reg_state *reg)
957{
958 reg->var_off = tnum_intersect(reg->var_off,
959 tnum_range(reg->umin_value,
960 reg->umax_value));
961}
962
963/* Reset the min/max bounds of a register */
964static void __mark_reg_unbounded(struct bpf_reg_state *reg)
965{
966 reg->smin_value = S64_MIN;
967 reg->smax_value = S64_MAX;
968 reg->umin_value = 0;
969 reg->umax_value = U64_MAX;
970}
971
Edward Creef1174f72017-08-07 15:26:19 +0100972/* Mark a register as having a completely unknown (scalar) value. */
973static void __mark_reg_unknown(struct bpf_reg_state *reg)
974{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700975 /*
976 * Clear type, id, off, and union(map_ptr, range) and
977 * padding between 'type' and union
978 */
979 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100980 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100981 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800982 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100983 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100984}
985
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700986static void mark_reg_unknown(struct bpf_verifier_env *env,
987 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100988{
989 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700990 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800991 /* Something bad happened, let's kill all regs except FP */
992 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100993 __mark_reg_not_init(regs + regno);
994 return;
995 }
996 __mark_reg_unknown(regs + regno);
997}
998
999static void __mark_reg_not_init(struct bpf_reg_state *reg)
1000{
1001 __mark_reg_unknown(reg);
1002 reg->type = NOT_INIT;
1003}
1004
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001005static void mark_reg_not_init(struct bpf_verifier_env *env,
1006 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001007{
Edward Creef1174f72017-08-07 15:26:19 +01001008 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001009 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -08001010 /* Something bad happened, let's kill all regs except FP */
1011 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +01001012 __mark_reg_not_init(regs + regno);
1013 return;
1014 }
1015 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +02001016}
1017
Jiong Wang5327ed32019-05-24 23:25:12 +01001018#define DEF_NOT_SUBREG (0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001019static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001020 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001021{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001022 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001023 int i;
1024
Edward Creedc503a82017-08-15 20:34:35 +01001025 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001026 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +01001027 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01001028 regs[i].parent = NULL;
Jiong Wang5327ed32019-05-24 23:25:12 +01001029 regs[i].subreg_def = DEF_NOT_SUBREG;
Edward Creedc503a82017-08-15 20:34:35 +01001030 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001031
1032 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +01001033 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001034 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001035 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001036
1037 /* 1st arg to a function */
1038 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001039 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +01001040}
1041
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001042#define BPF_MAIN_FUNC (-1)
1043static void init_func_state(struct bpf_verifier_env *env,
1044 struct bpf_func_state *state,
1045 int callsite, int frameno, int subprogno)
1046{
1047 state->callsite = callsite;
1048 state->frameno = frameno;
1049 state->subprogno = subprogno;
1050 init_reg_state(env, state);
1051}
1052
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001053enum reg_arg_type {
1054 SRC_OP, /* register is used as source operand */
1055 DST_OP, /* register is used as destination operand */
1056 DST_OP_NO_MARK /* same as above, check only, don't mark */
1057};
1058
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001059static int cmp_subprogs(const void *a, const void *b)
1060{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001061 return ((struct bpf_subprog_info *)a)->start -
1062 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001063}
1064
1065static int find_subprog(struct bpf_verifier_env *env, int off)
1066{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001067 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001068
Jiong Wang9c8105b2018-05-02 16:17:18 -04001069 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1070 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001071 if (!p)
1072 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001073 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001074
1075}
1076
1077static int add_subprog(struct bpf_verifier_env *env, int off)
1078{
1079 int insn_cnt = env->prog->len;
1080 int ret;
1081
1082 if (off >= insn_cnt || off < 0) {
1083 verbose(env, "call to invalid destination\n");
1084 return -EINVAL;
1085 }
1086 ret = find_subprog(env, off);
1087 if (ret >= 0)
1088 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001089 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001090 verbose(env, "too many subprograms\n");
1091 return -E2BIG;
1092 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001093 env->subprog_info[env->subprog_cnt++].start = off;
1094 sort(env->subprog_info, env->subprog_cnt,
1095 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001096 return 0;
1097}
1098
1099static int check_subprogs(struct bpf_verifier_env *env)
1100{
1101 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001102 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001103 struct bpf_insn *insn = env->prog->insnsi;
1104 int insn_cnt = env->prog->len;
1105
Jiong Wangf910cef2018-05-02 16:17:17 -04001106 /* Add entry function. */
1107 ret = add_subprog(env, 0);
1108 if (ret < 0)
1109 return ret;
1110
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001111 /* determine subprog starts. The end is one before the next starts */
1112 for (i = 0; i < insn_cnt; i++) {
1113 if (insn[i].code != (BPF_JMP | BPF_CALL))
1114 continue;
1115 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1116 continue;
1117 if (!env->allow_ptr_leaks) {
1118 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1119 return -EPERM;
1120 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001121 ret = add_subprog(env, i + insn[i].imm + 1);
1122 if (ret < 0)
1123 return ret;
1124 }
1125
Jiong Wang4cb3d992018-05-02 16:17:19 -04001126 /* Add a fake 'exit' subprog which could simplify subprog iteration
1127 * logic. 'subprog_cnt' should not be increased.
1128 */
1129 subprog[env->subprog_cnt].start = insn_cnt;
1130
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001131 if (env->log.level & BPF_LOG_LEVEL2)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001132 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001133 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001134
1135 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001136 subprog_start = subprog[cur_subprog].start;
1137 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001138 for (i = 0; i < insn_cnt; i++) {
1139 u8 code = insn[i].code;
1140
Jiong Wang092ed092019-01-26 12:26:01 -05001141 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001142 goto next;
1143 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1144 goto next;
1145 off = i + insn[i].off + 1;
1146 if (off < subprog_start || off >= subprog_end) {
1147 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1148 return -EINVAL;
1149 }
1150next:
1151 if (i == subprog_end - 1) {
1152 /* to avoid fall-through from one subprog into another
1153 * the last insn of the subprog should be either exit
1154 * or unconditional jump back
1155 */
1156 if (code != (BPF_JMP | BPF_EXIT) &&
1157 code != (BPF_JMP | BPF_JA)) {
1158 verbose(env, "last insn is not an exit or jmp\n");
1159 return -EINVAL;
1160 }
1161 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001162 cur_subprog++;
1163 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001164 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001165 }
1166 }
1167 return 0;
1168}
1169
Edward Cree679c7822018-08-22 20:02:19 +01001170/* Parentage chain of this register (or stack slot) should take care of all
1171 * issues like callee-saved registers, stack slot allocation time, etc.
1172 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001173static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001174 const struct bpf_reg_state *state,
Jiong Wang5327ed32019-05-24 23:25:12 +01001175 struct bpf_reg_state *parent, u8 flag)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001176{
1177 bool writes = parent == state->parent; /* Observe write marks */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001178 int cnt = 0;
Edward Creedc503a82017-08-15 20:34:35 +01001179
1180 while (parent) {
1181 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001182 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001183 break;
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08001184 if (parent->live & REG_LIVE_DONE) {
1185 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1186 reg_type_str[parent->type],
1187 parent->var_off.value, parent->off);
1188 return -EFAULT;
1189 }
Jiong Wang5327ed32019-05-24 23:25:12 +01001190 /* The first condition is more likely to be true than the
1191 * second, checked it first.
1192 */
1193 if ((parent->live & REG_LIVE_READ) == flag ||
1194 parent->live & REG_LIVE_READ64)
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001195 /* The parentage chain never changes and
1196 * this parent was already marked as LIVE_READ.
1197 * There is no need to keep walking the chain again and
1198 * keep re-marking all parents as LIVE_READ.
1199 * This case happens when the same register is read
1200 * multiple times without writes into it in-between.
Jiong Wang5327ed32019-05-24 23:25:12 +01001201 * Also, if parent has the stronger REG_LIVE_READ64 set,
1202 * then no need to set the weak REG_LIVE_READ32.
Alexei Starovoitov25af32d2019-04-01 21:27:42 -07001203 */
1204 break;
Edward Creedc503a82017-08-15 20:34:35 +01001205 /* ... then we depend on parent's value */
Jiong Wang5327ed32019-05-24 23:25:12 +01001206 parent->live |= flag;
1207 /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */
1208 if (flag == REG_LIVE_READ64)
1209 parent->live &= ~REG_LIVE_READ32;
Edward Creedc503a82017-08-15 20:34:35 +01001210 state = parent;
1211 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001212 writes = true;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001213 cnt++;
Edward Creedc503a82017-08-15 20:34:35 +01001214 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001215
1216 if (env->longest_mark_read_walk < cnt)
1217 env->longest_mark_read_walk = cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001218 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001219}
1220
Jiong Wang5327ed32019-05-24 23:25:12 +01001221/* This function is supposed to be used by the following 32-bit optimization
1222 * code only. It returns TRUE if the source or destination register operates
1223 * on 64-bit, otherwise return FALSE.
1224 */
1225static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn,
1226 u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t)
1227{
1228 u8 code, class, op;
1229
1230 code = insn->code;
1231 class = BPF_CLASS(code);
1232 op = BPF_OP(code);
1233 if (class == BPF_JMP) {
1234 /* BPF_EXIT for "main" will reach here. Return TRUE
1235 * conservatively.
1236 */
1237 if (op == BPF_EXIT)
1238 return true;
1239 if (op == BPF_CALL) {
1240 /* BPF to BPF call will reach here because of marking
1241 * caller saved clobber with DST_OP_NO_MARK for which we
1242 * don't care the register def because they are anyway
1243 * marked as NOT_INIT already.
1244 */
1245 if (insn->src_reg == BPF_PSEUDO_CALL)
1246 return false;
1247 /* Helper call will reach here because of arg type
1248 * check, conservatively return TRUE.
1249 */
1250 if (t == SRC_OP)
1251 return true;
1252
1253 return false;
1254 }
1255 }
1256
1257 if (class == BPF_ALU64 || class == BPF_JMP ||
1258 /* BPF_END always use BPF_ALU class. */
1259 (class == BPF_ALU && op == BPF_END && insn->imm == 64))
1260 return true;
1261
1262 if (class == BPF_ALU || class == BPF_JMP32)
1263 return false;
1264
1265 if (class == BPF_LDX) {
1266 if (t != SRC_OP)
1267 return BPF_SIZE(code) == BPF_DW;
1268 /* LDX source must be ptr. */
1269 return true;
1270 }
1271
1272 if (class == BPF_STX) {
1273 if (reg->type != SCALAR_VALUE)
1274 return true;
1275 return BPF_SIZE(code) == BPF_DW;
1276 }
1277
1278 if (class == BPF_LD) {
1279 u8 mode = BPF_MODE(code);
1280
1281 /* LD_IMM64 */
1282 if (mode == BPF_IMM)
1283 return true;
1284
1285 /* Both LD_IND and LD_ABS return 32-bit data. */
1286 if (t != SRC_OP)
1287 return false;
1288
1289 /* Implicit ctx ptr. */
1290 if (regno == BPF_REG_6)
1291 return true;
1292
1293 /* Explicit source could be any width. */
1294 return true;
1295 }
1296
1297 if (class == BPF_ST)
1298 /* The only source register for BPF_ST is a ptr. */
1299 return true;
1300
1301 /* Conservatively return true at default. */
1302 return true;
1303}
1304
Jiong Wangb325fbc2019-05-24 23:25:13 +01001305/* Return TRUE if INSN doesn't have explicit value define. */
1306static bool insn_no_def(struct bpf_insn *insn)
1307{
1308 u8 class = BPF_CLASS(insn->code);
1309
1310 return (class == BPF_JMP || class == BPF_JMP32 ||
1311 class == BPF_STX || class == BPF_ST);
1312}
1313
1314/* Return TRUE if INSN has defined any 32-bit value explicitly. */
1315static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn)
1316{
1317 if (insn_no_def(insn))
1318 return false;
1319
1320 return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP);
1321}
1322
Jiong Wang5327ed32019-05-24 23:25:12 +01001323static void mark_insn_zext(struct bpf_verifier_env *env,
1324 struct bpf_reg_state *reg)
1325{
1326 s32 def_idx = reg->subreg_def;
1327
1328 if (def_idx == DEF_NOT_SUBREG)
1329 return;
1330
1331 env->insn_aux_data[def_idx - 1].zext_dst = true;
1332 /* The dst will be zero extended, so won't be sub-register anymore. */
1333 reg->subreg_def = DEF_NOT_SUBREG;
1334}
1335
Edward Creedc503a82017-08-15 20:34:35 +01001336static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001337 enum reg_arg_type t)
1338{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001339 struct bpf_verifier_state *vstate = env->cur_state;
1340 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jiong Wang5327ed32019-05-24 23:25:12 +01001341 struct bpf_insn *insn = env->prog->insnsi + env->insn_idx;
Jiong Wangc342dc12019-04-12 22:59:37 +01001342 struct bpf_reg_state *reg, *regs = state->regs;
Jiong Wang5327ed32019-05-24 23:25:12 +01001343 bool rw64;
Edward Creedc503a82017-08-15 20:34:35 +01001344
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001345 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001346 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001347 return -EINVAL;
1348 }
1349
Jiong Wangc342dc12019-04-12 22:59:37 +01001350 reg = &regs[regno];
Jiong Wang5327ed32019-05-24 23:25:12 +01001351 rw64 = is_reg64(env, insn, regno, reg, t);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001352 if (t == SRC_OP) {
1353 /* check whether register used as source operand can be read */
Jiong Wangc342dc12019-04-12 22:59:37 +01001354 if (reg->type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001355 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001356 return -EACCES;
1357 }
Edward Cree679c7822018-08-22 20:02:19 +01001358 /* We don't need to worry about FP liveness because it's read-only */
Jiong Wangc342dc12019-04-12 22:59:37 +01001359 if (regno == BPF_REG_FP)
1360 return 0;
1361
Jiong Wang5327ed32019-05-24 23:25:12 +01001362 if (rw64)
1363 mark_insn_zext(env, reg);
1364
1365 return mark_reg_read(env, reg, reg->parent,
1366 rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001367 } else {
1368 /* check whether register used as dest operand can be written to */
1369 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001370 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001371 return -EACCES;
1372 }
Jiong Wangc342dc12019-04-12 22:59:37 +01001373 reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01001374 reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001375 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001376 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001377 }
1378 return 0;
1379}
1380
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001381static bool is_spillable_regtype(enum bpf_reg_type type)
1382{
1383 switch (type) {
1384 case PTR_TO_MAP_VALUE:
1385 case PTR_TO_MAP_VALUE_OR_NULL:
1386 case PTR_TO_STACK:
1387 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001388 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001389 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001390 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001391 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001392 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001393 case PTR_TO_SOCKET:
1394 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001395 case PTR_TO_SOCK_COMMON:
1396 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001397 case PTR_TO_TCP_SOCK:
1398 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07001399 case PTR_TO_XDP_SOCK:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001400 return true;
1401 default:
1402 return false;
1403 }
1404}
1405
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001406/* Does this register contain a constant zero? */
1407static bool register_is_null(struct bpf_reg_state *reg)
1408{
1409 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1410}
1411
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001412static bool register_is_const(struct bpf_reg_state *reg)
1413{
1414 return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off);
1415}
1416
1417static void save_register_state(struct bpf_func_state *state,
1418 int spi, struct bpf_reg_state *reg)
1419{
1420 int i;
1421
1422 state->stack[spi].spilled_ptr = *reg;
1423 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1424
1425 for (i = 0; i < BPF_REG_SIZE; i++)
1426 state->stack[spi].slot_type[i] = STACK_SPILL;
1427}
1428
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001429/* check_stack_read/write functions track spill/fill of registers,
1430 * stack boundary and alignment are checked in check_mem_access()
1431 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001432static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001433 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001434 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001435{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001436 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001437 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001438 struct bpf_reg_state *reg = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001439
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001440 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001441 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001442 if (err)
1443 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001444 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1445 * so it's aligned access and [off, off + size) are within stack limits
1446 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001447 if (!env->allow_ptr_leaks &&
1448 state->stack[spi].slot_type[0] == STACK_SPILL &&
1449 size != BPF_REG_SIZE) {
1450 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1451 return -EACCES;
1452 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001453
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001454 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001455 if (value_regno >= 0)
1456 reg = &cur->regs[value_regno];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001457
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001458 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&
1459 !register_is_null(reg) && env->allow_ptr_leaks) {
1460 save_register_state(state, spi, reg);
1461 } else if (reg && is_spillable_regtype(reg->type)) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001462 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001463 if (size != BPF_REG_SIZE) {
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001464 verbose_linfo(env, insn_idx, "; ");
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001465 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001466 return -EACCES;
1467 }
1468
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001469 if (state != cur && reg->type == PTR_TO_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001470 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1471 return -EINVAL;
1472 }
1473
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001474 if (!env->allow_ptr_leaks) {
1475 bool sanitize = false;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001476
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001477 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
1478 register_is_const(&state->stack[spi].spilled_ptr))
1479 sanitize = true;
1480 for (i = 0; i < BPF_REG_SIZE; i++)
1481 if (state->stack[spi].slot_type[i] == STACK_MISC) {
1482 sanitize = true;
1483 break;
1484 }
1485 if (sanitize) {
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001486 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1487 int soff = (-spi - 1) * BPF_REG_SIZE;
1488
1489 /* detected reuse of integer stack slot with a pointer
1490 * which means either llvm is reusing stack slot or
1491 * an attacker is trying to exploit CVE-2018-3639
1492 * (speculative store bypass)
1493 * Have to sanitize that slot with preemptive
1494 * store of zero.
1495 */
1496 if (*poff && *poff != soff) {
1497 /* disallow programs where single insn stores
1498 * into two different stack slots, since verifier
1499 * cannot sanitize them
1500 */
1501 verbose(env,
1502 "insn %d cannot access two stack slots fp%d and fp%d",
1503 insn_idx, *poff, soff);
1504 return -EINVAL;
1505 }
1506 *poff = soff;
1507 }
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001508 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001509 save_register_state(state, spi, reg);
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001510 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001511 u8 type = STACK_MISC;
1512
Edward Cree679c7822018-08-22 20:02:19 +01001513 /* regular write of data into stack destroys any spilled ptr */
1514 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001515 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1516 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1517 for (i = 0; i < BPF_REG_SIZE; i++)
1518 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001519
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001520 /* only mark the slot as written if all 8 bytes were written
1521 * otherwise read propagation may incorrectly stop too soon
1522 * when stack slots are partially written.
1523 * This heuristic means that read propagation will be
1524 * conservative, since it will add reg_live_read marks
1525 * to stack slots all the way to first state when programs
1526 * writes+reads less than 8 bytes
1527 */
1528 if (size == BPF_REG_SIZE)
1529 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1530
1531 /* when we zero initialize stack slots mark them as such */
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001532 if (reg && register_is_null(reg))
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001533 type = STACK_ZERO;
1534
Jiong Wang0bae2d42018-12-15 03:34:40 -05001535 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001536 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001537 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001538 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001539 }
1540 return 0;
1541}
1542
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001543static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001544 struct bpf_func_state *reg_state /* func where register points to */,
1545 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001546{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001547 struct bpf_verifier_state *vstate = env->cur_state;
1548 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001549 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001550 struct bpf_reg_state *reg;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001551 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001552
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001553 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001554 verbose(env, "invalid read from stack off %d+0 size %d\n",
1555 off, size);
1556 return -EACCES;
1557 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001558 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001559 reg = &reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001560
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001561 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001562 if (size != BPF_REG_SIZE) {
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001563 if (reg->type != SCALAR_VALUE) {
1564 verbose_linfo(env, env->insn_idx, "; ");
1565 verbose(env, "invalid size of register fill\n");
1566 return -EACCES;
1567 }
1568 if (value_regno >= 0) {
1569 mark_reg_unknown(env, state->regs, value_regno);
1570 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1571 }
1572 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
1573 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001574 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001575 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001576 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001577 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001578 return -EACCES;
1579 }
1580 }
1581
Edward Creedc503a82017-08-15 20:34:35 +01001582 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001583 /* restore register state from stack */
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001584 state->regs[value_regno] = *reg;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001585 /* mark reg as written since spilled pointer state likely
1586 * has its liveness marks cleared by is_state_visited()
1587 * which resets stack/reg liveness for state transitions
1588 */
1589 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001590 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001591 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001592 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001593 int zeros = 0;
1594
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001595 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001596 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1597 continue;
1598 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1599 zeros++;
1600 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001601 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001602 verbose(env, "invalid read from stack off %d+%d size %d\n",
1603 off, i, size);
1604 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001605 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001606 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001607 if (value_regno >= 0) {
1608 if (zeros == size) {
1609 /* any size read into register is zero extended,
1610 * so the whole register == const_zero
1611 */
1612 __mark_reg_const_zero(&state->regs[value_regno]);
1613 } else {
1614 /* have read misc data from the stack */
1615 mark_reg_unknown(env, state->regs, value_regno);
1616 }
1617 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1618 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001619 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07001620 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001621}
1622
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001623static int check_stack_access(struct bpf_verifier_env *env,
1624 const struct bpf_reg_state *reg,
1625 int off, int size)
1626{
1627 /* Stack accesses must be at a fixed offset, so that we
1628 * can determine what type of data were returned. See
1629 * check_stack_read().
1630 */
1631 if (!tnum_is_const(reg->var_off)) {
1632 char tn_buf[48];
1633
1634 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Andrey Ignatov1fbd20f2019-04-03 23:22:43 -07001635 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001636 tn_buf, off, size);
1637 return -EACCES;
1638 }
1639
1640 if (off >= 0 || off < -MAX_BPF_STACK) {
1641 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1642 return -EACCES;
1643 }
1644
1645 return 0;
1646}
1647
Daniel Borkmann591fe982019-04-09 23:20:05 +02001648static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
1649 int off, int size, enum bpf_access_type type)
1650{
1651 struct bpf_reg_state *regs = cur_regs(env);
1652 struct bpf_map *map = regs[regno].map_ptr;
1653 u32 cap = bpf_map_flags_to_cap(map);
1654
1655 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
1656 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
1657 map->value_size, off, size);
1658 return -EACCES;
1659 }
1660
1661 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
1662 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
1663 map->value_size, off, size);
1664 return -EACCES;
1665 }
1666
1667 return 0;
1668}
1669
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001670/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001671static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001672 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001673{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001674 struct bpf_reg_state *regs = cur_regs(env);
1675 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001676
Yonghong Song9fd29c02017-11-12 14:49:09 -08001677 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1678 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001679 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001680 map->value_size, off, size);
1681 return -EACCES;
1682 }
1683 return 0;
1684}
1685
Edward Creef1174f72017-08-07 15:26:19 +01001686/* check read/write into a map element with possible variable offset */
1687static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001688 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001689{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001690 struct bpf_verifier_state *vstate = env->cur_state;
1691 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001692 struct bpf_reg_state *reg = &state->regs[regno];
1693 int err;
1694
Edward Creef1174f72017-08-07 15:26:19 +01001695 /* We may have adjusted the register to this map value, so we
1696 * need to try adding each of min_value and max_value to off
1697 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001698 */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07001699 if (env->log.level & BPF_LOG_LEVEL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001700 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001701
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001702 /* The minimum value is only important with signed
1703 * comparisons where we can't assume the floor of a
1704 * value is 0. If we are using signed variables for our
1705 * index'es we need to make sure that whatever we use
1706 * will have a set floor within our range.
1707 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001708 if (reg->smin_value < 0 &&
1709 (reg->smin_value == S64_MIN ||
1710 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1711 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001712 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 -08001713 regno);
1714 return -EACCES;
1715 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001716 err = __check_map_access(env, regno, reg->smin_value + off, size,
1717 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001718 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001719 verbose(env, "R%d min value is outside of the array range\n",
1720 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001721 return err;
1722 }
1723
Edward Creeb03c9f92017-08-07 15:26:36 +01001724 /* If we haven't set a max value then we need to bail since we can't be
1725 * sure we won't do bad things.
1726 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001727 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001728 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001729 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 -08001730 regno);
1731 return -EACCES;
1732 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001733 err = __check_map_access(env, regno, reg->umax_value + off, size,
1734 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001735 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001736 verbose(env, "R%d max value is outside of the array range\n",
1737 regno);
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001738
1739 if (map_value_has_spin_lock(reg->map_ptr)) {
1740 u32 lock = reg->map_ptr->spin_lock_off;
1741
1742 /* if any part of struct bpf_spin_lock can be touched by
1743 * load/store reject this program.
1744 * To check that [x1, x2) overlaps with [y1, y2)
1745 * it is sufficient to check x1 < y2 && y1 < x2.
1746 */
1747 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
1748 lock < reg->umax_value + off + size) {
1749 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
1750 return -EACCES;
1751 }
1752 }
Edward Creef1174f72017-08-07 15:26:19 +01001753 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001754}
1755
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001756#define MAX_PACKET_OFF 0xffff
1757
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001758static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001759 const struct bpf_call_arg_meta *meta,
1760 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001761{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001762 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001763 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001764 case BPF_PROG_TYPE_LWT_IN:
1765 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001766 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001767 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001768 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001769 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001770 if (t == BPF_WRITE)
1771 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001772 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001773
1774 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001775 case BPF_PROG_TYPE_SCHED_CLS:
1776 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001777 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001778 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001779 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001780 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001781 if (meta)
1782 return meta->pkt_access;
1783
1784 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001785 return true;
1786 default:
1787 return false;
1788 }
1789}
1790
Edward Creef1174f72017-08-07 15:26:19 +01001791static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001792 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001793{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001794 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001795 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001796
Yonghong Song9fd29c02017-11-12 14:49:09 -08001797 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1798 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001799 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 -07001800 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001801 return -EACCES;
1802 }
1803 return 0;
1804}
1805
Edward Creef1174f72017-08-07 15:26:19 +01001806static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001807 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001808{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001809 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001810 struct bpf_reg_state *reg = &regs[regno];
1811 int err;
1812
1813 /* We may have added a variable offset to the packet pointer; but any
1814 * reg->range we have comes after that. We are only checking the fixed
1815 * offset.
1816 */
1817
1818 /* We don't allow negative numbers, because we aren't tracking enough
1819 * detail to prove they're safe.
1820 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001821 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001822 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 +01001823 regno);
1824 return -EACCES;
1825 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001826 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001827 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001828 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001829 return err;
1830 }
Jiong Wange6478152018-11-08 04:08:42 -05001831
1832 /* __check_packet_access has made sure "off + size - 1" is within u16.
1833 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1834 * otherwise find_good_pkt_pointers would have refused to set range info
1835 * that __check_packet_access would have rejected this pkt access.
1836 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1837 */
1838 env->prog->aux->max_pkt_offset =
1839 max_t(u32, env->prog->aux->max_pkt_offset,
1840 off + reg->umax_value + size - 1);
1841
Edward Creef1174f72017-08-07 15:26:19 +01001842 return err;
1843}
1844
1845/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001846static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001847 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001848{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001849 struct bpf_insn_access_aux info = {
1850 .reg_type = *reg_type,
1851 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001852
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001853 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001854 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001855 /* A non zero info.ctx_field_size indicates that this field is a
1856 * candidate for later verifier transformation to load the whole
1857 * field and then apply a mask when accessed with a narrower
1858 * access than actual ctx access size. A zero info.ctx_field_size
1859 * will only allow for whole field access and rejects any other
1860 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001861 */
Yonghong Song23994632017-06-22 15:07:39 -07001862 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001863
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001864 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001865 /* remember the offset of last byte accessed in ctx */
1866 if (env->prog->aux->max_ctx_offset < off + size)
1867 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001868 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001869 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001870
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001871 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001872 return -EACCES;
1873}
1874
Petar Penkovd58e4682018-09-14 07:46:18 -07001875static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1876 int size)
1877{
1878 if (size < 0 || off < 0 ||
1879 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1880 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1881 off, size);
1882 return -EACCES;
1883 }
1884 return 0;
1885}
1886
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001887static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
1888 u32 regno, int off, int size,
1889 enum bpf_access_type t)
Joe Stringerc64b7982018-10-02 13:35:33 -07001890{
1891 struct bpf_reg_state *regs = cur_regs(env);
1892 struct bpf_reg_state *reg = &regs[regno];
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001893 struct bpf_insn_access_aux info = {};
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001894 bool valid;
Joe Stringerc64b7982018-10-02 13:35:33 -07001895
1896 if (reg->smin_value < 0) {
1897 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1898 regno);
1899 return -EACCES;
1900 }
1901
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001902 switch (reg->type) {
1903 case PTR_TO_SOCK_COMMON:
1904 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
1905 break;
1906 case PTR_TO_SOCKET:
1907 valid = bpf_sock_is_valid_access(off, size, t, &info);
1908 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08001909 case PTR_TO_TCP_SOCK:
1910 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
1911 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07001912 case PTR_TO_XDP_SOCK:
1913 valid = bpf_xdp_sock_is_valid_access(off, size, t, &info);
1914 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001915 default:
1916 valid = false;
Joe Stringerc64b7982018-10-02 13:35:33 -07001917 }
1918
Martin KaFai Lau5f456642019-02-08 22:25:54 -08001919
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001920 if (valid) {
1921 env->insn_aux_data[insn_idx].ctx_field_size =
1922 info.ctx_field_size;
1923 return 0;
1924 }
1925
1926 verbose(env, "R%d invalid %s access off=%d size=%d\n",
1927 regno, reg_type_str[reg->type], off, size);
1928
1929 return -EACCES;
Joe Stringerc64b7982018-10-02 13:35:33 -07001930}
1931
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001932static bool __is_pointer_value(bool allow_ptr_leaks,
1933 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001934{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001935 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001936 return false;
1937
Edward Creef1174f72017-08-07 15:26:19 +01001938 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001939}
1940
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001941static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1942{
1943 return cur_regs(env) + regno;
1944}
1945
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001946static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1947{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001948 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001949}
1950
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001951static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1952{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001953 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001954
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08001955 return reg->type == PTR_TO_CTX;
1956}
1957
1958static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
1959{
1960 const struct bpf_reg_state *reg = reg_state(env, regno);
1961
1962 return type_is_sk_pointer(reg->type);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001963}
1964
Daniel Borkmannca369602018-02-23 22:29:05 +01001965static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1966{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001967 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001968
1969 return type_is_pkt_pointer(reg->type);
1970}
1971
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001972static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1973{
1974 const struct bpf_reg_state *reg = reg_state(env, regno);
1975
1976 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1977 return reg->type == PTR_TO_FLOW_KEYS;
1978}
1979
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001980static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1981 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001982 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001983{
Edward Creef1174f72017-08-07 15:26:19 +01001984 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001985 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001986
1987 /* Byte size accesses are always allowed. */
1988 if (!strict || size == 1)
1989 return 0;
1990
David S. Millere4eda882017-05-22 12:27:07 -04001991 /* For platforms that do not have a Kconfig enabling
1992 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1993 * NET_IP_ALIGN is universally set to '2'. And on platforms
1994 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1995 * to this code only in strict mode where we want to emulate
1996 * the NET_IP_ALIGN==2 checking. Therefore use an
1997 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001998 */
David S. Millere4eda882017-05-22 12:27:07 -04001999 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01002000
2001 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
2002 if (!tnum_is_aligned(reg_off, size)) {
2003 char tn_buf[48];
2004
2005 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002006 verbose(env,
2007 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01002008 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002009 return -EACCES;
2010 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002011
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002012 return 0;
2013}
2014
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002015static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
2016 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01002017 const char *pointer_desc,
2018 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002019{
Edward Creef1174f72017-08-07 15:26:19 +01002020 struct tnum reg_off;
2021
2022 /* Byte size accesses are always allowed. */
2023 if (!strict || size == 1)
2024 return 0;
2025
2026 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
2027 if (!tnum_is_aligned(reg_off, size)) {
2028 char tn_buf[48];
2029
2030 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002031 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01002032 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002033 return -EACCES;
2034 }
2035
2036 return 0;
2037}
2038
David S. Millere07b98d2017-05-10 11:38:07 -07002039static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01002040 const struct bpf_reg_state *reg, int off,
2041 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002042{
Daniel Borkmannca369602018-02-23 22:29:05 +01002043 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01002044 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07002045
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002046 switch (reg->type) {
2047 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002048 case PTR_TO_PACKET_META:
2049 /* Special case, because of NET_IP_ALIGN. Given metadata sits
2050 * right in front, treat it the very same way.
2051 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002052 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07002053 case PTR_TO_FLOW_KEYS:
2054 pointer_desc = "flow keys ";
2055 break;
Edward Creef1174f72017-08-07 15:26:19 +01002056 case PTR_TO_MAP_VALUE:
2057 pointer_desc = "value ";
2058 break;
2059 case PTR_TO_CTX:
2060 pointer_desc = "context ";
2061 break;
2062 case PTR_TO_STACK:
2063 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08002064 /* The stack spill tracking logic in check_stack_write()
2065 * and check_stack_read() relies on stack accesses being
2066 * aligned.
2067 */
2068 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01002069 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07002070 case PTR_TO_SOCKET:
2071 pointer_desc = "sock ";
2072 break;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002073 case PTR_TO_SOCK_COMMON:
2074 pointer_desc = "sock_common ";
2075 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08002076 case PTR_TO_TCP_SOCK:
2077 pointer_desc = "tcp_sock ";
2078 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07002079 case PTR_TO_XDP_SOCK:
2080 pointer_desc = "xdp_sock ";
2081 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002082 default:
Edward Creef1174f72017-08-07 15:26:19 +01002083 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002084 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002085 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
2086 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02002087}
2088
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002089static int update_stack_depth(struct bpf_verifier_env *env,
2090 const struct bpf_func_state *func,
2091 int off)
2092{
Jiong Wang9c8105b2018-05-02 16:17:18 -04002093 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002094
2095 if (stack >= -off)
2096 return 0;
2097
2098 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04002099 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002100 return 0;
2101}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002102
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002103/* starting from main bpf function walk all instructions of the function
2104 * and recursively walk all callees that given function can call.
2105 * Ignore jump and exit insns.
2106 * Since recursion is prevented by check_cfg() this algorithm
2107 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
2108 */
2109static int check_max_stack_depth(struct bpf_verifier_env *env)
2110{
Jiong Wang9c8105b2018-05-02 16:17:18 -04002111 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
2112 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002113 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002114 int ret_insn[MAX_CALL_FRAMES];
2115 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002116
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002117process_func:
2118 /* round up to 32-bytes, since this is granularity
2119 * of interpreter stack size
2120 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04002121 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002122 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002123 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002124 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002125 return -EACCES;
2126 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002127continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04002128 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002129 for (; i < subprog_end; i++) {
2130 if (insn[i].code != (BPF_JMP | BPF_CALL))
2131 continue;
2132 if (insn[i].src_reg != BPF_PSEUDO_CALL)
2133 continue;
2134 /* remember insn and function to return to */
2135 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002136 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002137
2138 /* find the callee */
2139 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002140 idx = find_subprog(env, i);
2141 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002142 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2143 i);
2144 return -EFAULT;
2145 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002146 frame++;
2147 if (frame >= MAX_CALL_FRAMES) {
Paul Chaignon927cb782019-03-20 13:58:27 +01002148 verbose(env, "the call stack of %d frames is too deep !\n",
2149 frame);
2150 return -E2BIG;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002151 }
2152 goto process_func;
2153 }
2154 /* end of for() loop means the last insn of the 'subprog'
2155 * was reached. Doesn't matter whether it was JA or EXIT
2156 */
2157 if (frame == 0)
2158 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04002159 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002160 frame--;
2161 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04002162 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08002163 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002164}
2165
David S. Miller19d28fb2018-01-11 21:27:54 -05002166#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002167static int get_callee_stack_depth(struct bpf_verifier_env *env,
2168 const struct bpf_insn *insn, int idx)
2169{
2170 int start = idx + insn->imm + 1, subprog;
2171
2172 subprog = find_subprog(env, start);
2173 if (subprog < 0) {
2174 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2175 start);
2176 return -EFAULT;
2177 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04002178 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002179}
David S. Miller19d28fb2018-01-11 21:27:54 -05002180#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08002181
Daniel Borkmann58990d12018-06-07 17:40:03 +02002182static int check_ctx_reg(struct bpf_verifier_env *env,
2183 const struct bpf_reg_state *reg, int regno)
2184{
2185 /* Access to ctx or passing it to a helper is only allowed in
2186 * its original, unmodified form.
2187 */
2188
2189 if (reg->off) {
2190 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
2191 regno, reg->off);
2192 return -EACCES;
2193 }
2194
2195 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2196 char tn_buf[48];
2197
2198 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2199 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
2200 return -EACCES;
2201 }
2202
2203 return 0;
2204}
2205
Matt Mullins9df1c282019-04-26 11:49:47 -07002206static int check_tp_buffer_access(struct bpf_verifier_env *env,
2207 const struct bpf_reg_state *reg,
2208 int regno, int off, int size)
2209{
2210 if (off < 0) {
2211 verbose(env,
2212 "R%d invalid tracepoint buffer access: off=%d, size=%d",
2213 regno, off, size);
2214 return -EACCES;
2215 }
2216 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2217 char tn_buf[48];
2218
2219 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2220 verbose(env,
2221 "R%d invalid variable buffer offset: off=%d, var_off=%s",
2222 regno, off, tn_buf);
2223 return -EACCES;
2224 }
2225 if (off + size > env->prog->aux->max_tp_access)
2226 env->prog->aux->max_tp_access = off + size;
2227
2228 return 0;
2229}
2230
2231
Jann Horn0c17d1d2017-12-18 20:11:55 -08002232/* truncate register to smaller size (in bytes)
2233 * must be called with size < BPF_REG_SIZE
2234 */
2235static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2236{
2237 u64 mask;
2238
2239 /* clear high bits in bit representation */
2240 reg->var_off = tnum_cast(reg->var_off, size);
2241
2242 /* fix arithmetic bounds */
2243 mask = ((u64)1 << (size * 8)) - 1;
2244 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2245 reg->umin_value &= mask;
2246 reg->umax_value &= mask;
2247 } else {
2248 reg->umin_value = 0;
2249 reg->umax_value = mask;
2250 }
2251 reg->smin_value = reg->umin_value;
2252 reg->smax_value = reg->umax_value;
2253}
2254
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002255/* check whether memory at (regno + off) is accessible for t = (read | write)
2256 * if t==write, value_regno is a register which value is stored into memory
2257 * if t==read, value_regno is a register which will receive the value from memory
2258 * if t==write && value_regno==-1, some unknown value is stored into memory
2259 * if t==read && value_regno==-1, don't care what we read from memory
2260 */
Daniel Borkmannca369602018-02-23 22:29:05 +01002261static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2262 int off, int bpf_size, enum bpf_access_type t,
2263 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002264{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002265 struct bpf_reg_state *regs = cur_regs(env);
2266 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002267 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002268 int size, err = 0;
2269
2270 size = bpf_size_to_bytes(bpf_size);
2271 if (size < 0)
2272 return size;
2273
Edward Creef1174f72017-08-07 15:26:19 +01002274 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01002275 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002276 if (err)
2277 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002278
Edward Creef1174f72017-08-07 15:26:19 +01002279 /* for access checks, reg->off is just part of off */
2280 off += reg->off;
2281
2282 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002283 if (t == BPF_WRITE && value_regno >= 0 &&
2284 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002285 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002286 return -EACCES;
2287 }
Daniel Borkmann591fe982019-04-09 23:20:05 +02002288 err = check_map_access_type(env, regno, off, size, t);
2289 if (err)
2290 return err;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002291 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002292 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002293 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002294
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07002295 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01002296 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07002297
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002298 if (t == BPF_WRITE && value_regno >= 0 &&
2299 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002300 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002301 return -EACCES;
2302 }
Edward Creef1174f72017-08-07 15:26:19 +01002303
Daniel Borkmann58990d12018-06-07 17:40:03 +02002304 err = check_ctx_reg(env, reg, regno);
2305 if (err < 0)
2306 return err;
2307
Yonghong Song31fd8582017-06-13 15:52:13 -07002308 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002309 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002310 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002311 * PTR_TO_PACKET[_META,_END]. In the latter
2312 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01002313 */
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002314 if (reg_type == SCALAR_VALUE) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002315 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002316 } else {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002317 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002318 value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002319 if (reg_type_may_be_null(reg_type))
2320 regs[value_regno].id = ++env->id_gen;
Jiong Wang5327ed32019-05-24 23:25:12 +01002321 /* A load of ctx field could have different
2322 * actual load size with the one encoded in the
2323 * insn. When the dst is PTR, it is for sure not
2324 * a sub-register.
2325 */
2326 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002327 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002328 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002329 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002330
Edward Creef1174f72017-08-07 15:26:19 +01002331 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002332 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01002333 err = check_stack_access(env, reg, off, size);
2334 if (err)
2335 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002336
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002337 state = func(env, reg);
2338 err = update_stack_depth(env, state, off);
2339 if (err)
2340 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07002341
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002342 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002343 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002344 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002345 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002346 err = check_stack_read(env, state, off, size,
2347 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002348 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002349 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002350 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002351 return -EACCES;
2352 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002353 if (t == BPF_WRITE && value_regno >= 0 &&
2354 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002355 verbose(env, "R%d leaks addr into packet\n",
2356 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002357 return -EACCES;
2358 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002359 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002360 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002361 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002362 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2363 if (t == BPF_WRITE && value_regno >= 0 &&
2364 is_pointer_value(env, value_regno)) {
2365 verbose(env, "R%d leaks addr into flow keys\n",
2366 value_regno);
2367 return -EACCES;
2368 }
2369
2370 err = check_flow_keys_access(env, off, size);
2371 if (!err && t == BPF_READ && value_regno >= 0)
2372 mark_reg_unknown(env, regs, value_regno);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002373 } else if (type_is_sk_pointer(reg->type)) {
Joe Stringerc64b7982018-10-02 13:35:33 -07002374 if (t == BPF_WRITE) {
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002375 verbose(env, "R%d cannot write into %s\n",
2376 regno, reg_type_str[reg->type]);
Joe Stringerc64b7982018-10-02 13:35:33 -07002377 return -EACCES;
2378 }
Martin KaFai Lau5f456642019-02-08 22:25:54 -08002379 err = check_sock_access(env, insn_idx, regno, off, size, t);
Joe Stringerc64b7982018-10-02 13:35:33 -07002380 if (!err && value_regno >= 0)
2381 mark_reg_unknown(env, regs, value_regno);
Matt Mullins9df1c282019-04-26 11:49:47 -07002382 } else if (reg->type == PTR_TO_TP_BUFFER) {
2383 err = check_tp_buffer_access(env, reg, regno, off, size);
2384 if (!err && t == BPF_READ && value_regno >= 0)
2385 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002386 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002387 verbose(env, "R%d invalid mem access '%s'\n", regno,
2388 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002389 return -EACCES;
2390 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002391
Edward Creef1174f72017-08-07 15:26:19 +01002392 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002393 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002394 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002395 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002396 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002397 return err;
2398}
2399
Yonghong Song31fd8582017-06-13 15:52:13 -07002400static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002401{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002402 int err;
2403
2404 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2405 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002406 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002407 return -EINVAL;
2408 }
2409
2410 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002411 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002412 if (err)
2413 return err;
2414
2415 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002416 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002417 if (err)
2418 return err;
2419
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002420 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002421 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002422 return -EACCES;
2423 }
2424
Daniel Borkmannca369602018-02-23 22:29:05 +01002425 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002426 is_pkt_reg(env, insn->dst_reg) ||
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002427 is_flow_key_reg(env, insn->dst_reg) ||
2428 is_sk_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002429 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002430 insn->dst_reg,
2431 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002432 return -EACCES;
2433 }
2434
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002435 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002436 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002437 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002438 if (err)
2439 return err;
2440
2441 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002442 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002443 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002444}
2445
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002446static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2447 int off, int access_size,
2448 bool zero_size_allowed)
2449{
2450 struct bpf_reg_state *reg = reg_state(env, regno);
2451
2452 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2453 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2454 if (tnum_is_const(reg->var_off)) {
2455 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2456 regno, off, access_size);
2457 } else {
2458 char tn_buf[48];
2459
2460 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2461 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2462 regno, tn_buf, access_size);
2463 }
2464 return -EACCES;
2465 }
2466 return 0;
2467}
2468
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002469/* when register 'regno' is passed into function that will read 'access_size'
2470 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002471 * and all elements of stack are initialized.
2472 * Unlike most pointer bounds-checking functions, this one doesn't take an
2473 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002474 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002475static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002476 int access_size, bool zero_size_allowed,
2477 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002478{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002479 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002480 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002481 int err, min_off, max_off, i, j, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002482
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002483 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002484 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002485 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002486 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002487 return 0;
2488
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002489 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002490 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002491 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002492 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002493 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002494
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002495 if (tnum_is_const(reg->var_off)) {
2496 min_off = max_off = reg->var_off.value + reg->off;
2497 err = __check_stack_boundary(env, regno, min_off, access_size,
2498 zero_size_allowed);
2499 if (err)
2500 return err;
2501 } else {
Andrey Ignatov088ec262019-04-03 23:22:39 -07002502 /* Variable offset is prohibited for unprivileged mode for
2503 * simplicity since it requires corresponding support in
2504 * Spectre masking for stack ALU.
2505 * See also retrieve_ptr_limit().
2506 */
2507 if (!env->allow_ptr_leaks) {
2508 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +01002509
Andrey Ignatov088ec262019-04-03 23:22:39 -07002510 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2511 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
2512 regno, tn_buf);
2513 return -EACCES;
2514 }
Andrey Ignatovf2bcd052019-04-03 23:22:37 -07002515 /* Only initialized buffer on stack is allowed to be accessed
2516 * with variable offset. With uninitialized buffer it's hard to
2517 * guarantee that whole memory is marked as initialized on
2518 * helper return since specific bounds are unknown what may
2519 * cause uninitialized stack leaking.
2520 */
2521 if (meta && meta->raw_mode)
2522 meta = NULL;
2523
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002524 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
2525 reg->smax_value <= -BPF_MAX_VAR_OFF) {
2526 verbose(env, "R%d unbounded indirect variable offset stack access\n",
2527 regno);
2528 return -EACCES;
2529 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002530 min_off = reg->smin_value + reg->off;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002531 max_off = reg->smax_value + reg->off;
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002532 err = __check_stack_boundary(env, regno, min_off, access_size,
2533 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002534 if (err) {
2535 verbose(env, "R%d min value is outside of stack bound\n",
2536 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002537 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002538 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002539 err = __check_stack_boundary(env, regno, max_off, access_size,
2540 zero_size_allowed);
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002541 if (err) {
2542 verbose(env, "R%d max value is outside of stack bound\n",
2543 regno);
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002544 return err;
Andrey Ignatov107c26a72019-04-03 23:22:41 -07002545 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002546 }
2547
Daniel Borkmann435faee12016-04-13 00:10:51 +02002548 if (meta && meta->raw_mode) {
2549 meta->access_size = access_size;
2550 meta->regno = regno;
2551 return 0;
2552 }
2553
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002554 for (i = min_off; i < max_off + access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002555 u8 *stype;
2556
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002557 slot = -i - 1;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002558 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002559 if (state->allocated_stack <= slot)
2560 goto err;
2561 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2562 if (*stype == STACK_MISC)
2563 goto mark;
2564 if (*stype == STACK_ZERO) {
2565 /* helper can write anything into the stack */
2566 *stype = STACK_MISC;
2567 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002568 }
Alexei Starovoitovf7cf25b2019-06-15 12:12:17 -07002569 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
2570 state->stack[spi].spilled_ptr.type == SCALAR_VALUE) {
2571 __mark_reg_unknown(&state->stack[spi].spilled_ptr);
2572 for (j = 0; j < BPF_REG_SIZE; j++)
2573 state->stack[spi].slot_type[j] = STACK_MISC;
2574 goto mark;
2575 }
2576
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002577err:
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002578 if (tnum_is_const(reg->var_off)) {
2579 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2580 min_off, i - min_off, access_size);
2581 } else {
2582 char tn_buf[48];
2583
2584 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2585 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
2586 tn_buf, i - min_off, access_size);
2587 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002588 return -EACCES;
2589mark:
2590 /* reading any byte out of 8-byte 'spill_slot' will cause
2591 * the whole slot to be marked as 'read'
2592 */
Edward Cree679c7822018-08-22 20:02:19 +01002593 mark_reg_read(env, &state->stack[spi].spilled_ptr,
Jiong Wang5327ed32019-05-24 23:25:12 +01002594 state->stack[spi].spilled_ptr.parent,
2595 REG_LIVE_READ64);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002596 }
Andrey Ignatov2011fcc2019-03-28 18:01:57 -07002597 return update_stack_depth(env, state, min_off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002598}
2599
Gianluca Borello06c1c042017-01-09 10:19:49 -08002600static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2601 int access_size, bool zero_size_allowed,
2602 struct bpf_call_arg_meta *meta)
2603{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002604 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002605
Edward Creef1174f72017-08-07 15:26:19 +01002606 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002607 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002608 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002609 return check_packet_access(env, regno, reg->off, access_size,
2610 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002611 case PTR_TO_MAP_VALUE:
Daniel Borkmann591fe982019-04-09 23:20:05 +02002612 if (check_map_access_type(env, regno, reg->off, access_size,
2613 meta && meta->raw_mode ? BPF_WRITE :
2614 BPF_READ))
2615 return -EACCES;
Yonghong Song9fd29c02017-11-12 14:49:09 -08002616 return check_map_access(env, regno, reg->off, access_size,
2617 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002618 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002619 return check_stack_boundary(env, regno, access_size,
2620 zero_size_allowed, meta);
2621 }
2622}
2623
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002624/* Implementation details:
2625 * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
2626 * Two bpf_map_lookups (even with the same key) will have different reg->id.
2627 * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
2628 * value_or_null->value transition, since the verifier only cares about
2629 * the range of access to valid map value pointer and doesn't care about actual
2630 * address of the map element.
2631 * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
2632 * reg->id > 0 after value_or_null->value transition. By doing so
2633 * two bpf_map_lookups will be considered two different pointers that
2634 * point to different bpf_spin_locks.
2635 * The verifier allows taking only one bpf_spin_lock at a time to avoid
2636 * dead-locks.
2637 * Since only one bpf_spin_lock is allowed the checks are simpler than
2638 * reg_is_refcounted() logic. The verifier needs to remember only
2639 * one spin_lock instead of array of acquired_refs.
2640 * cur_state->active_spin_lock remembers which map value element got locked
2641 * and clears it after bpf_spin_unlock.
2642 */
2643static int process_spin_lock(struct bpf_verifier_env *env, int regno,
2644 bool is_lock)
2645{
2646 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2647 struct bpf_verifier_state *cur = env->cur_state;
2648 bool is_const = tnum_is_const(reg->var_off);
2649 struct bpf_map *map = reg->map_ptr;
2650 u64 val = reg->var_off.value;
2651
2652 if (reg->type != PTR_TO_MAP_VALUE) {
2653 verbose(env, "R%d is not a pointer to map_value\n", regno);
2654 return -EINVAL;
2655 }
2656 if (!is_const) {
2657 verbose(env,
2658 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
2659 regno);
2660 return -EINVAL;
2661 }
2662 if (!map->btf) {
2663 verbose(env,
2664 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
2665 map->name);
2666 return -EINVAL;
2667 }
2668 if (!map_value_has_spin_lock(map)) {
2669 if (map->spin_lock_off == -E2BIG)
2670 verbose(env,
2671 "map '%s' has more than one 'struct bpf_spin_lock'\n",
2672 map->name);
2673 else if (map->spin_lock_off == -ENOENT)
2674 verbose(env,
2675 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
2676 map->name);
2677 else
2678 verbose(env,
2679 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
2680 map->name);
2681 return -EINVAL;
2682 }
2683 if (map->spin_lock_off != val + reg->off) {
2684 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
2685 val + reg->off);
2686 return -EINVAL;
2687 }
2688 if (is_lock) {
2689 if (cur->active_spin_lock) {
2690 verbose(env,
2691 "Locking two bpf_spin_locks are not allowed\n");
2692 return -EINVAL;
2693 }
2694 cur->active_spin_lock = reg->id;
2695 } else {
2696 if (!cur->active_spin_lock) {
2697 verbose(env, "bpf_spin_unlock without taking a lock\n");
2698 return -EINVAL;
2699 }
2700 if (cur->active_spin_lock != reg->id) {
2701 verbose(env, "bpf_spin_unlock of different lock\n");
2702 return -EINVAL;
2703 }
2704 cur->active_spin_lock = 0;
2705 }
2706 return 0;
2707}
2708
Daniel Borkmann90133412018-01-20 01:24:29 +01002709static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2710{
2711 return type == ARG_PTR_TO_MEM ||
2712 type == ARG_PTR_TO_MEM_OR_NULL ||
2713 type == ARG_PTR_TO_UNINIT_MEM;
2714}
2715
2716static bool arg_type_is_mem_size(enum bpf_arg_type type)
2717{
2718 return type == ARG_CONST_SIZE ||
2719 type == ARG_CONST_SIZE_OR_ZERO;
2720}
2721
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002722static bool arg_type_is_int_ptr(enum bpf_arg_type type)
2723{
2724 return type == ARG_PTR_TO_INT ||
2725 type == ARG_PTR_TO_LONG;
2726}
2727
2728static int int_ptr_type_to_size(enum bpf_arg_type type)
2729{
2730 if (type == ARG_PTR_TO_INT)
2731 return sizeof(u32);
2732 else if (type == ARG_PTR_TO_LONG)
2733 return sizeof(u64);
2734
2735 return -EINVAL;
2736}
2737
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002738static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002739 enum bpf_arg_type arg_type,
2740 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002741{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002742 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002743 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002744 int err = 0;
2745
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002746 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002747 return 0;
2748
Edward Creedc503a82017-08-15 20:34:35 +01002749 err = check_reg_arg(env, regno, SRC_OP);
2750 if (err)
2751 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002752
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002753 if (arg_type == ARG_ANYTHING) {
2754 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002755 verbose(env, "R%d leaks addr into helper function\n",
2756 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002757 return -EACCES;
2758 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002759 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002760 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002761
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002762 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002763 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002764 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002765 return -EACCES;
2766 }
2767
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002768 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002769 arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002770 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
2771 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002772 expected_type = PTR_TO_STACK;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002773 if (register_is_null(reg) &&
2774 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
2775 /* final test in check_stack_boundary() */;
2776 else if (!type_is_pkt_pointer(type) &&
2777 type != PTR_TO_MAP_VALUE &&
2778 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002779 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002780 } else if (arg_type == ARG_CONST_SIZE ||
2781 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002782 expected_type = SCALAR_VALUE;
2783 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002784 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002785 } else if (arg_type == ARG_CONST_MAP_PTR) {
2786 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002787 if (type != expected_type)
2788 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002789 } else if (arg_type == ARG_PTR_TO_CTX) {
2790 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002791 if (type != expected_type)
2792 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002793 err = check_ctx_reg(env, reg, regno);
2794 if (err < 0)
2795 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08002796 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
2797 expected_type = PTR_TO_SOCK_COMMON;
2798 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
2799 if (!type_is_sk_pointer(type))
2800 goto err_type;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07002801 if (reg->ref_obj_id) {
2802 if (meta->ref_obj_id) {
2803 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
2804 regno, reg->ref_obj_id,
2805 meta->ref_obj_id);
2806 return -EFAULT;
2807 }
2808 meta->ref_obj_id = reg->ref_obj_id;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002809 }
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002810 } else if (arg_type == ARG_PTR_TO_SOCKET) {
2811 expected_type = PTR_TO_SOCKET;
2812 if (type != expected_type)
2813 goto err_type;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08002814 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
2815 if (meta->func_id == BPF_FUNC_spin_lock) {
2816 if (process_spin_lock(env, regno, true))
2817 return -EACCES;
2818 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
2819 if (process_spin_lock(env, regno, false))
2820 return -EACCES;
2821 } else {
2822 verbose(env, "verifier internal error\n");
2823 return -EFAULT;
2824 }
Daniel Borkmann90133412018-01-20 01:24:29 +01002825 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002826 expected_type = PTR_TO_STACK;
2827 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002828 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002829 * happens during stack boundary checking.
2830 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002831 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002832 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002833 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002834 else if (!type_is_pkt_pointer(type) &&
2835 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002836 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002837 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002838 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002839 } else if (arg_type_is_int_ptr(arg_type)) {
2840 expected_type = PTR_TO_STACK;
2841 if (!type_is_pkt_pointer(type) &&
2842 type != PTR_TO_MAP_VALUE &&
2843 type != expected_type)
2844 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002845 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002846 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002847 return -EFAULT;
2848 }
2849
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002850 if (arg_type == ARG_CONST_MAP_PTR) {
2851 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002852 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002853 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2854 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2855 * check that [key, key + map->key_size) are within
2856 * stack limits and initialized
2857 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002858 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002859 /* in function declaration map_ptr must come before
2860 * map_key, so that it's verified and known before
2861 * we have to check map_key here. Otherwise it means
2862 * that kernel subsystem misconfigured verifier
2863 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002864 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002865 return -EACCES;
2866 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002867 err = check_helper_mem_access(env, regno,
2868 meta->map_ptr->key_size, false,
2869 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002870 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07002871 (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
2872 !register_is_null(reg)) ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002873 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002874 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2875 * check [value, value + map->value_size) validity
2876 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002877 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002878 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002879 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002880 return -EACCES;
2881 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002882 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002883 err = check_helper_mem_access(env, regno,
2884 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002885 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002886 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002887 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002888
Yonghong Song849fa502018-04-28 22:28:09 -07002889 /* remember the mem_size which may be used later
2890 * to refine return values.
2891 */
2892 meta->msize_smax_value = reg->smax_value;
2893 meta->msize_umax_value = reg->umax_value;
2894
Edward Creef1174f72017-08-07 15:26:19 +01002895 /* The register is SCALAR_VALUE; the access check
2896 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002897 */
Edward Creef1174f72017-08-07 15:26:19 +01002898 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002899 /* For unprivileged variable accesses, disable raw
2900 * mode so that the program is required to
2901 * initialize all the memory that the helper could
2902 * just partially fill up.
2903 */
2904 meta = NULL;
2905
Edward Creeb03c9f92017-08-07 15:26:36 +01002906 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002907 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002908 regno);
2909 return -EACCES;
2910 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002911
Edward Creeb03c9f92017-08-07 15:26:36 +01002912 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002913 err = check_helper_mem_access(env, regno - 1, 0,
2914 zero_size_allowed,
2915 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002916 if (err)
2917 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002918 }
Edward Creef1174f72017-08-07 15:26:19 +01002919
Edward Creeb03c9f92017-08-07 15:26:36 +01002920 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002921 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002922 regno);
2923 return -EACCES;
2924 }
2925 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002926 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002927 zero_size_allowed, meta);
Andrey Ignatov57c3bb72019-03-18 16:57:10 -07002928 } else if (arg_type_is_int_ptr(arg_type)) {
2929 int size = int_ptr_type_to_size(arg_type);
2930
2931 err = check_helper_mem_access(env, regno, size, false, meta);
2932 if (err)
2933 return err;
2934 err = check_ptr_alignment(env, reg, 0, size, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002935 }
2936
2937 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002938err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002939 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002940 reg_type_str[type], reg_type_str[expected_type]);
2941 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002942}
2943
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002944static int check_map_func_compatibility(struct bpf_verifier_env *env,
2945 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002946{
Kaixu Xia35578d72015-08-06 07:02:35 +00002947 if (!map)
2948 return 0;
2949
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002950 /* We need a two way check, first is from map perspective ... */
2951 switch (map->map_type) {
2952 case BPF_MAP_TYPE_PROG_ARRAY:
2953 if (func_id != BPF_FUNC_tail_call)
2954 goto error;
2955 break;
2956 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2957 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002958 func_id != BPF_FUNC_perf_event_output &&
2959 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002960 goto error;
2961 break;
2962 case BPF_MAP_TYPE_STACK_TRACE:
2963 if (func_id != BPF_FUNC_get_stackid)
2964 goto error;
2965 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002966 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002967 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002968 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002969 goto error;
2970 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002971 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002972 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002973 if (func_id != BPF_FUNC_get_local_storage)
2974 goto error;
2975 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002976 /* devmap returns a pointer to a live net_device ifindex that we cannot
2977 * allow to be modified from bpf side. So do not allow lookup elements
2978 * for now.
2979 */
2980 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002981 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002982 goto error;
2983 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002984 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2985 * appear.
2986 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002987 case BPF_MAP_TYPE_CPUMAP:
2988 if (func_id != BPF_FUNC_redirect_map)
2989 goto error;
2990 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07002991 case BPF_MAP_TYPE_XSKMAP:
2992 if (func_id != BPF_FUNC_redirect_map &&
2993 func_id != BPF_FUNC_map_lookup_elem)
2994 goto error;
2995 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002996 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002997 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002998 if (func_id != BPF_FUNC_map_lookup_elem)
2999 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07003000 break;
John Fastabend174a79f2017-08-15 22:32:47 -07003001 case BPF_MAP_TYPE_SOCKMAP:
3002 if (func_id != BPF_FUNC_sk_redirect_map &&
3003 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07003004 func_id != BPF_FUNC_map_delete_elem &&
3005 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07003006 goto error;
3007 break;
John Fastabend81110382018-05-14 10:00:17 -07003008 case BPF_MAP_TYPE_SOCKHASH:
3009 if (func_id != BPF_FUNC_sk_redirect_hash &&
3010 func_id != BPF_FUNC_sock_hash_update &&
3011 func_id != BPF_FUNC_map_delete_elem &&
3012 func_id != BPF_FUNC_msg_redirect_hash)
3013 goto error;
3014 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07003015 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
3016 if (func_id != BPF_FUNC_sk_select_reuseport)
3017 goto error;
3018 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003019 case BPF_MAP_TYPE_QUEUE:
3020 case BPF_MAP_TYPE_STACK:
3021 if (func_id != BPF_FUNC_map_peek_elem &&
3022 func_id != BPF_FUNC_map_pop_elem &&
3023 func_id != BPF_FUNC_map_push_elem)
3024 goto error;
3025 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003026 case BPF_MAP_TYPE_SK_STORAGE:
3027 if (func_id != BPF_FUNC_sk_storage_get &&
3028 func_id != BPF_FUNC_sk_storage_delete)
3029 goto error;
3030 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003031 default:
3032 break;
3033 }
3034
3035 /* ... and second from the function itself. */
3036 switch (func_id) {
3037 case BPF_FUNC_tail_call:
3038 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
3039 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04003040 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003041 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
3042 return -EINVAL;
3043 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003044 break;
3045 case BPF_FUNC_perf_event_read:
3046 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07003047 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003048 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
3049 goto error;
3050 break;
3051 case BPF_FUNC_get_stackid:
3052 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
3053 goto error;
3054 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07003055 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02003056 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07003057 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
3058 goto error;
3059 break;
John Fastabend97f91a72017-07-17 09:29:18 -07003060 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02003061 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02003062 map->map_type != BPF_MAP_TYPE_CPUMAP &&
3063 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07003064 goto error;
3065 break;
John Fastabend174a79f2017-08-15 22:32:47 -07003066 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07003067 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07003068 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07003069 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
3070 goto error;
3071 break;
John Fastabend81110382018-05-14 10:00:17 -07003072 case BPF_FUNC_sk_redirect_hash:
3073 case BPF_FUNC_msg_redirect_hash:
3074 case BPF_FUNC_sock_hash_update:
3075 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07003076 goto error;
3077 break;
Roman Gushchincd339432018-08-02 14:27:24 -07003078 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00003079 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
3080 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07003081 goto error;
3082 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07003083 case BPF_FUNC_sk_select_reuseport:
3084 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
3085 goto error;
3086 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003087 case BPF_FUNC_map_peek_elem:
3088 case BPF_FUNC_map_pop_elem:
3089 case BPF_FUNC_map_push_elem:
3090 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
3091 map->map_type != BPF_MAP_TYPE_STACK)
3092 goto error;
3093 break;
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07003094 case BPF_FUNC_sk_storage_get:
3095 case BPF_FUNC_sk_storage_delete:
3096 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
3097 goto error;
3098 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003099 default:
3100 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00003101 }
3102
3103 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003104error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003105 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003106 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07003107 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00003108}
3109
Daniel Borkmann90133412018-01-20 01:24:29 +01003110static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003111{
3112 int count = 0;
3113
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003114 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003115 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003116 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003117 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003118 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003119 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003120 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003121 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08003122 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02003123 count++;
3124
Daniel Borkmann90133412018-01-20 01:24:29 +01003125 /* We only support one arg being in raw mode at the moment,
3126 * which is sufficient for the helper functions we have
3127 * right now.
3128 */
3129 return count <= 1;
3130}
3131
3132static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
3133 enum bpf_arg_type arg_next)
3134{
3135 return (arg_type_is_mem_ptr(arg_curr) &&
3136 !arg_type_is_mem_size(arg_next)) ||
3137 (!arg_type_is_mem_ptr(arg_curr) &&
3138 arg_type_is_mem_size(arg_next));
3139}
3140
3141static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
3142{
3143 /* bpf_xxx(..., buf, len) call will access 'len'
3144 * bytes from memory 'buf'. Both arg types need
3145 * to be paired, so make sure there's no buggy
3146 * helper function specification.
3147 */
3148 if (arg_type_is_mem_size(fn->arg1_type) ||
3149 arg_type_is_mem_ptr(fn->arg5_type) ||
3150 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
3151 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
3152 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
3153 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
3154 return false;
3155
3156 return true;
3157}
3158
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003159static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003160{
3161 int count = 0;
3162
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003163 if (arg_type_may_be_refcounted(fn->arg1_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003164 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003165 if (arg_type_may_be_refcounted(fn->arg2_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003166 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003167 if (arg_type_may_be_refcounted(fn->arg3_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003168 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003169 if (arg_type_may_be_refcounted(fn->arg4_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003170 count++;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003171 if (arg_type_may_be_refcounted(fn->arg5_type))
Joe Stringerfd978bf72018-10-02 13:35:35 -07003172 count++;
3173
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003174 /* A reference acquiring function cannot acquire
3175 * another refcounted ptr.
3176 */
3177 if (is_acquire_function(func_id) && count)
3178 return false;
3179
Joe Stringerfd978bf72018-10-02 13:35:35 -07003180 /* We only support one arg being unreferenced at the moment,
3181 * which is sufficient for the helper functions we have right now.
3182 */
3183 return count <= 1;
3184}
3185
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003186static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
Daniel Borkmann90133412018-01-20 01:24:29 +01003187{
3188 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07003189 check_arg_pair_ok(fn) &&
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003190 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02003191}
3192
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003193/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
3194 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01003195 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003196static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
3197 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003198{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003199 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003200 int i;
3201
3202 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003203 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003204 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003205
Joe Stringerf3709f62018-10-02 13:35:29 -07003206 bpf_for_each_spilled_reg(i, state, reg) {
3207 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003208 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003209 if (reg_is_pkt_pointer_any(reg))
3210 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003211 }
3212}
3213
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003214static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
3215{
3216 struct bpf_verifier_state *vstate = env->cur_state;
3217 int i;
3218
3219 for (i = 0; i <= vstate->curframe; i++)
3220 __clear_all_pkt_pointers(env, vstate->frame[i]);
3221}
3222
Joe Stringerfd978bf72018-10-02 13:35:35 -07003223static void release_reg_references(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003224 struct bpf_func_state *state,
3225 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003226{
3227 struct bpf_reg_state *regs = state->regs, *reg;
3228 int i;
3229
3230 for (i = 0; i < MAX_BPF_REG; i++)
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003231 if (regs[i].ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003232 mark_reg_unknown(env, regs, i);
3233
3234 bpf_for_each_spilled_reg(i, state, reg) {
3235 if (!reg)
3236 continue;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003237 if (reg->ref_obj_id == ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003238 __mark_reg_unknown(reg);
3239 }
3240}
3241
3242/* The pointer with the specified id has released its reference to kernel
3243 * resources. Identify all copies of the same pointer and clear the reference.
3244 */
3245static int release_reference(struct bpf_verifier_env *env,
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003246 int ref_obj_id)
Joe Stringerfd978bf72018-10-02 13:35:35 -07003247{
3248 struct bpf_verifier_state *vstate = env->cur_state;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003249 int err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003250 int i;
3251
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003252 err = release_reference_state(cur_func(env), ref_obj_id);
3253 if (err)
3254 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003255
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003256 for (i = 0; i <= vstate->curframe; i++)
3257 release_reg_references(env, vstate->frame[i], ref_obj_id);
3258
3259 return 0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003260}
3261
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003262static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
3263 int *insn_idx)
3264{
3265 struct bpf_verifier_state *state = env->cur_state;
3266 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003267 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003268
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003269 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003270 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08003271 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003272 return -E2BIG;
3273 }
3274
3275 target_insn = *insn_idx + insn->imm;
3276 subprog = find_subprog(env, target_insn + 1);
3277 if (subprog < 0) {
3278 verbose(env, "verifier bug. No program starts at insn %d\n",
3279 target_insn + 1);
3280 return -EFAULT;
3281 }
3282
3283 caller = state->frame[state->curframe];
3284 if (state->frame[state->curframe + 1]) {
3285 verbose(env, "verifier bug. Frame %d already allocated\n",
3286 state->curframe + 1);
3287 return -EFAULT;
3288 }
3289
3290 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3291 if (!callee)
3292 return -ENOMEM;
3293 state->frame[state->curframe + 1] = callee;
3294
3295 /* callee cannot access r0, r6 - r9 for reading and has to write
3296 * into its own stack before reading from it.
3297 * callee can read/write into caller's stack
3298 */
3299 init_func_state(env, callee,
3300 /* remember the callsite, it will be used by bpf_exit */
3301 *insn_idx /* callsite */,
3302 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04003303 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003304
Joe Stringerfd978bf72018-10-02 13:35:35 -07003305 /* Transfer references to the callee */
3306 err = transfer_reference_state(callee, caller);
3307 if (err)
3308 return err;
3309
Edward Cree679c7822018-08-22 20:02:19 +01003310 /* copy r1 - r5 args that callee can access. The copy includes parent
3311 * pointers, which connects us up to the liveness chain
3312 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003313 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3314 callee->regs[i] = caller->regs[i];
3315
Edward Cree679c7822018-08-22 20:02:19 +01003316 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003317 for (i = 0; i < CALLER_SAVED_REGS; i++) {
3318 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3319 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3320 }
3321
3322 /* only increment it after check_reg_arg() finished */
3323 state->curframe++;
3324
3325 /* and go analyze first insn of the callee */
3326 *insn_idx = target_insn;
3327
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003328 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003329 verbose(env, "caller:\n");
3330 print_verifier_state(env, caller);
3331 verbose(env, "callee:\n");
3332 print_verifier_state(env, callee);
3333 }
3334 return 0;
3335}
3336
3337static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3338{
3339 struct bpf_verifier_state *state = env->cur_state;
3340 struct bpf_func_state *caller, *callee;
3341 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07003342 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003343
3344 callee = state->frame[state->curframe];
3345 r0 = &callee->regs[BPF_REG_0];
3346 if (r0->type == PTR_TO_STACK) {
3347 /* technically it's ok to return caller's stack pointer
3348 * (or caller's caller's pointer) back to the caller,
3349 * since these pointers are valid. Only current stack
3350 * pointer will be invalid as soon as function exits,
3351 * but let's be conservative
3352 */
3353 verbose(env, "cannot return stack pointer to the caller\n");
3354 return -EINVAL;
3355 }
3356
3357 state->curframe--;
3358 caller = state->frame[state->curframe];
3359 /* return to the caller whatever r0 had in the callee */
3360 caller->regs[BPF_REG_0] = *r0;
3361
Joe Stringerfd978bf72018-10-02 13:35:35 -07003362 /* Transfer references to the caller */
3363 err = transfer_reference_state(caller, callee);
3364 if (err)
3365 return err;
3366
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003367 *insn_idx = callee->callsite + 1;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07003368 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003369 verbose(env, "returning from callee:\n");
3370 print_verifier_state(env, callee);
3371 verbose(env, "to caller at %d:\n", *insn_idx);
3372 print_verifier_state(env, caller);
3373 }
3374 /* clear everything in the callee */
3375 free_func_state(callee);
3376 state->frame[state->curframe + 1] = NULL;
3377 return 0;
3378}
3379
Yonghong Song849fa502018-04-28 22:28:09 -07003380static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
3381 int func_id,
3382 struct bpf_call_arg_meta *meta)
3383{
3384 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
3385
3386 if (ret_type != RET_INTEGER ||
3387 (func_id != BPF_FUNC_get_stack &&
3388 func_id != BPF_FUNC_probe_read_str))
3389 return;
3390
3391 ret_reg->smax_value = meta->msize_smax_value;
3392 ret_reg->umax_value = meta->msize_umax_value;
3393 __reg_deduce_bounds(ret_reg);
3394 __reg_bound_offset(ret_reg);
3395}
3396
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003397static int
3398record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3399 int func_id, int insn_idx)
3400{
3401 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
Daniel Borkmann591fe982019-04-09 23:20:05 +02003402 struct bpf_map *map = meta->map_ptr;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003403
3404 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02003405 func_id != BPF_FUNC_map_lookup_elem &&
3406 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02003407 func_id != BPF_FUNC_map_delete_elem &&
3408 func_id != BPF_FUNC_map_push_elem &&
3409 func_id != BPF_FUNC_map_pop_elem &&
3410 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003411 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02003412
Daniel Borkmann591fe982019-04-09 23:20:05 +02003413 if (map == NULL) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003414 verbose(env, "kernel subsystem misconfigured verifier\n");
3415 return -EINVAL;
3416 }
3417
Daniel Borkmann591fe982019-04-09 23:20:05 +02003418 /* In case of read-only, some additional restrictions
3419 * need to be applied in order to prevent altering the
3420 * state of the map from program side.
3421 */
3422 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3423 (func_id == BPF_FUNC_map_delete_elem ||
3424 func_id == BPF_FUNC_map_update_elem ||
3425 func_id == BPF_FUNC_map_push_elem ||
3426 func_id == BPF_FUNC_map_pop_elem)) {
3427 verbose(env, "write into map forbidden\n");
3428 return -EACCES;
3429 }
3430
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003431 if (!BPF_MAP_PTR(aux->map_state))
3432 bpf_map_ptr_store(aux, meta->map_ptr,
3433 meta->map_ptr->unpriv_array);
3434 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3435 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3436 meta->map_ptr->unpriv_array);
3437 return 0;
3438}
3439
Joe Stringerfd978bf72018-10-02 13:35:35 -07003440static int check_reference_leak(struct bpf_verifier_env *env)
3441{
3442 struct bpf_func_state *state = cur_func(env);
3443 int i;
3444
3445 for (i = 0; i < state->acquired_refs; i++) {
3446 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3447 state->refs[i].id, state->refs[i].insn_idx);
3448 }
3449 return state->acquired_refs ? -EINVAL : 0;
3450}
3451
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003452static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003453{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003454 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003455 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003456 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003457 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003458 int i, err;
3459
3460 /* find function prototype */
3461 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003462 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3463 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003464 return -EINVAL;
3465 }
3466
Jakub Kicinski00176a32017-10-16 16:40:54 -07003467 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003468 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003469 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003470 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3471 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003472 return -EINVAL;
3473 }
3474
3475 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01003476 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02003477 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003478 return -EINVAL;
3479 }
3480
Daniel Borkmann04514d12017-12-14 21:07:25 +01003481 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08003482 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01003483 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3484 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3485 func_id_name(func_id), func_id);
3486 return -EINVAL;
3487 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003488
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003489 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02003490 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003491
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003492 err = check_func_proto(fn, func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003493 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003494 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003495 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003496 return err;
3497 }
3498
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08003499 meta.func_id = func_id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003500 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003501 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003502 if (err)
3503 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003504 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003505 if (err)
3506 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003507 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003508 if (err)
3509 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003510 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003511 if (err)
3512 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003513 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003514 if (err)
3515 return err;
3516
Daniel Borkmannc93552c2018-05-24 02:32:53 +02003517 err = record_func_map(env, &meta, func_id, insn_idx);
3518 if (err)
3519 return err;
3520
Daniel Borkmann435faee12016-04-13 00:10:51 +02003521 /* Mark slots with STACK_MISC in case of raw mode, stack offset
3522 * is inferred from register state.
3523 */
3524 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01003525 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
3526 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02003527 if (err)
3528 return err;
3529 }
3530
Joe Stringerfd978bf72018-10-02 13:35:35 -07003531 if (func_id == BPF_FUNC_tail_call) {
3532 err = check_reference_leak(env);
3533 if (err) {
3534 verbose(env, "tail_call would lead to reference leak\n");
3535 return err;
3536 }
3537 } else if (is_release_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003538 err = release_reference(env, meta.ref_obj_id);
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003539 if (err) {
3540 verbose(env, "func %s#%d reference has not been acquired before\n",
3541 func_id_name(func_id), func_id);
Joe Stringerfd978bf72018-10-02 13:35:35 -07003542 return err;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003543 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07003544 }
3545
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003546 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07003547
3548 /* check that flags argument in get_local_storage(map, flags) is 0,
3549 * this is required because get_local_storage() can't return an error.
3550 */
3551 if (func_id == BPF_FUNC_get_local_storage &&
3552 !register_is_null(&regs[BPF_REG_2])) {
3553 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
3554 return -EINVAL;
3555 }
3556
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003557 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01003558 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003559 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01003560 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3561 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003562
Jiong Wang5327ed32019-05-24 23:25:12 +01003563 /* helper call returns 64-bit value. */
3564 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
3565
Edward Creedc503a82017-08-15 20:34:35 +01003566 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003567 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01003568 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003569 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003570 } else if (fn->ret_type == RET_VOID) {
3571 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07003572 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
3573 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01003574 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003575 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003576 /* remember map_ptr, so that check_map_access()
3577 * can check 'value_size' boundary of memory access
3578 * to map element returned from bpf_map_lookup_elem()
3579 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003580 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003581 verbose(env,
3582 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003583 return -EINVAL;
3584 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02003585 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003586 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3587 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08003588 if (map_value_has_spin_lock(meta.map_ptr))
3589 regs[BPF_REG_0].id = ++env->id_gen;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01003590 } else {
3591 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
3592 regs[BPF_REG_0].id = ++env->id_gen;
3593 }
Joe Stringerc64b7982018-10-02 13:35:33 -07003594 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
3595 mark_reg_known_zero(env, regs, BPF_REG_0);
3596 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003597 regs[BPF_REG_0].id = ++env->id_gen;
Lorenz Bauer85a51f82019-03-22 09:54:00 +08003598 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
3599 mark_reg_known_zero(env, regs, BPF_REG_0);
3600 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
3601 regs[BPF_REG_0].id = ++env->id_gen;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003602 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
3603 mark_reg_known_zero(env, regs, BPF_REG_0);
3604 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
3605 regs[BPF_REG_0].id = ++env->id_gen;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003606 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003607 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02003608 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003609 return -EINVAL;
3610 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003611
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003612 if (is_ptr_cast_function(func_id)) {
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003613 /* For release_reference() */
3614 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
Lorenz Bauer0f3adc22019-03-22 09:53:59 +08003615 } else if (is_acquire_function(func_id)) {
3616 int id = acquire_reference_state(env, insn_idx);
3617
3618 if (id < 0)
3619 return id;
3620 /* For mark_ptr_or_null_reg() */
3621 regs[BPF_REG_0].id = id;
3622 /* For release_reference() */
3623 regs[BPF_REG_0].ref_obj_id = id;
3624 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07003625
Yonghong Song849fa502018-04-28 22:28:09 -07003626 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
3627
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003628 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00003629 if (err)
3630 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07003631
Yonghong Songc195651e2018-04-28 22:28:08 -07003632 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
3633 const char *err_str;
3634
3635#ifdef CONFIG_PERF_EVENTS
3636 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3637 err_str = "cannot get callchain buffer for func %s#%d\n";
3638#else
3639 err = -ENOTSUPP;
3640 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3641#endif
3642 if (err) {
3643 verbose(env, err_str, func_id_name(func_id), func_id);
3644 return err;
3645 }
3646
3647 env->prog->has_callchain_buf = true;
3648 }
3649
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003650 if (changes_data)
3651 clear_all_pkt_pointers(env);
3652 return 0;
3653}
3654
Edward Creeb03c9f92017-08-07 15:26:36 +01003655static bool signed_add_overflows(s64 a, s64 b)
3656{
3657 /* Do the add in u64, where overflow is well-defined */
3658 s64 res = (s64)((u64)a + (u64)b);
3659
3660 if (b < 0)
3661 return res > a;
3662 return res < a;
3663}
3664
3665static bool signed_sub_overflows(s64 a, s64 b)
3666{
3667 /* Do the sub in u64, where overflow is well-defined */
3668 s64 res = (s64)((u64)a - (u64)b);
3669
3670 if (b < 0)
3671 return res < a;
3672 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07003673}
3674
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003675static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3676 const struct bpf_reg_state *reg,
3677 enum bpf_reg_type type)
3678{
3679 bool known = tnum_is_const(reg->var_off);
3680 s64 val = reg->var_off.value;
3681 s64 smin = reg->smin_value;
3682
3683 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3684 verbose(env, "math between %s pointer and %lld is not allowed\n",
3685 reg_type_str[type], val);
3686 return false;
3687 }
3688
3689 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3690 verbose(env, "%s pointer offset %d is not allowed\n",
3691 reg_type_str[type], reg->off);
3692 return false;
3693 }
3694
3695 if (smin == S64_MIN) {
3696 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3697 reg_type_str[type]);
3698 return false;
3699 }
3700
3701 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3702 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3703 smin, reg_type_str[type]);
3704 return false;
3705 }
3706
3707 return true;
3708}
3709
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003710static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3711{
3712 return &env->insn_aux_data[env->insn_idx];
3713}
3714
3715static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3716 u32 *ptr_limit, u8 opcode, bool off_is_neg)
3717{
3718 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
3719 (opcode == BPF_SUB && !off_is_neg);
3720 u32 off;
3721
3722 switch (ptr_reg->type) {
3723 case PTR_TO_STACK:
Andrey Ignatov088ec262019-04-03 23:22:39 -07003724 /* Indirect variable offset stack access is prohibited in
3725 * unprivileged mode so it's not handled here.
3726 */
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003727 off = ptr_reg->off + ptr_reg->var_off.value;
3728 if (mask_to_left)
3729 *ptr_limit = MAX_BPF_STACK + off;
3730 else
3731 *ptr_limit = -off;
3732 return 0;
3733 case PTR_TO_MAP_VALUE:
3734 if (mask_to_left) {
3735 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3736 } else {
3737 off = ptr_reg->smin_value + ptr_reg->off;
3738 *ptr_limit = ptr_reg->map_ptr->value_size - off;
3739 }
3740 return 0;
3741 default:
3742 return -EINVAL;
3743 }
3744}
3745
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003746static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3747 const struct bpf_insn *insn)
3748{
3749 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3750}
3751
3752static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3753 u32 alu_state, u32 alu_limit)
3754{
3755 /* If we arrived here from different branches with different
3756 * state or limits to sanitize, then this won't work.
3757 */
3758 if (aux->alu_state &&
3759 (aux->alu_state != alu_state ||
3760 aux->alu_limit != alu_limit))
3761 return -EACCES;
3762
3763 /* Corresponding fixup done in fixup_bpf_calls(). */
3764 aux->alu_state = alu_state;
3765 aux->alu_limit = alu_limit;
3766 return 0;
3767}
3768
3769static int sanitize_val_alu(struct bpf_verifier_env *env,
3770 struct bpf_insn *insn)
3771{
3772 struct bpf_insn_aux_data *aux = cur_aux(env);
3773
3774 if (can_skip_alu_sanitation(env, insn))
3775 return 0;
3776
3777 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3778}
3779
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003780static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3781 struct bpf_insn *insn,
3782 const struct bpf_reg_state *ptr_reg,
3783 struct bpf_reg_state *dst_reg,
3784 bool off_is_neg)
3785{
3786 struct bpf_verifier_state *vstate = env->cur_state;
3787 struct bpf_insn_aux_data *aux = cur_aux(env);
3788 bool ptr_is_dst_reg = ptr_reg == dst_reg;
3789 u8 opcode = BPF_OP(insn->code);
3790 u32 alu_state, alu_limit;
3791 struct bpf_reg_state tmp;
3792 bool ret;
3793
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003794 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003795 return 0;
3796
3797 /* We already marked aux for masking from non-speculative
3798 * paths, thus we got here in the first place. We only care
3799 * to explore bad access from here.
3800 */
3801 if (vstate->speculative)
3802 goto do_sim;
3803
3804 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3805 alu_state |= ptr_is_dst_reg ?
3806 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3807
3808 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3809 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003810 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003811 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003812do_sim:
3813 /* Simulate and find potential out-of-bounds access under
3814 * speculative execution from truncation as a result of
3815 * masking when off was not within expected range. If off
3816 * sits in dst, then we temporarily need to move ptr there
3817 * to simulate dst (== 0) +/-= ptr. Needed, for example,
3818 * for cases where we use K-based arithmetic in one direction
3819 * and truncated reg-based in the other in order to explore
3820 * bad access.
3821 */
3822 if (!ptr_is_dst_reg) {
3823 tmp = *dst_reg;
3824 *dst_reg = *ptr_reg;
3825 }
3826 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
Xu Yu08032782019-03-21 18:00:35 +08003827 if (!ptr_is_dst_reg && ret)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003828 *dst_reg = tmp;
3829 return !ret ? -EFAULT : 0;
3830}
3831
Edward Creef1174f72017-08-07 15:26:19 +01003832/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01003833 * Caller should also handle BPF_MOV case separately.
3834 * If we return -EACCES, caller may want to try again treating pointer as a
3835 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
3836 */
3837static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3838 struct bpf_insn *insn,
3839 const struct bpf_reg_state *ptr_reg,
3840 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04003841{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003842 struct bpf_verifier_state *vstate = env->cur_state;
3843 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3844 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003845 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003846 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3847 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3848 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3849 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003850 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003851 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003852 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04003853
Edward Creef1174f72017-08-07 15:26:19 +01003854 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003855
Daniel Borkmann6f161012018-01-18 01:15:21 +01003856 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3857 smin_val > smax_val || umin_val > umax_val) {
3858 /* Taint dst register if offset had invalid bounds derived from
3859 * e.g. dead branches.
3860 */
3861 __mark_reg_unknown(dst_reg);
3862 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003863 }
3864
Edward Creef1174f72017-08-07 15:26:19 +01003865 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3866 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003867 verbose(env,
3868 "R%d 32-bit pointer arithmetic prohibited\n",
3869 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003870 return -EACCES;
3871 }
David S. Millerd1174412017-05-10 11:22:52 -07003872
Joe Stringeraad2eea2018-10-02 13:35:30 -07003873 switch (ptr_reg->type) {
3874 case PTR_TO_MAP_VALUE_OR_NULL:
3875 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3876 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003877 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003878 case CONST_PTR_TO_MAP:
3879 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003880 case PTR_TO_SOCKET:
3881 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08003882 case PTR_TO_SOCK_COMMON:
3883 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08003884 case PTR_TO_TCP_SOCK:
3885 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07003886 case PTR_TO_XDP_SOCK:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003887 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3888 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003889 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003890 case PTR_TO_MAP_VALUE:
3891 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3892 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3893 off_reg == dst_reg ? dst : src);
3894 return -EACCES;
3895 }
3896 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07003897 default:
3898 break;
Edward Creef1174f72017-08-07 15:26:19 +01003899 }
3900
3901 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3902 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003903 */
Edward Creef1174f72017-08-07 15:26:19 +01003904 dst_reg->type = ptr_reg->type;
3905 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003906
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003907 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3908 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3909 return -EINVAL;
3910
Josef Bacik48461132016-09-28 10:54:32 -04003911 switch (opcode) {
3912 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003913 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3914 if (ret < 0) {
3915 verbose(env, "R%d tried to add from different maps or paths\n", dst);
3916 return ret;
3917 }
Edward Creef1174f72017-08-07 15:26:19 +01003918 /* We can take a fixed offset as long as it doesn't overflow
3919 * the s32 'off' field
3920 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003921 if (known && (ptr_reg->off + smin_val ==
3922 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003923 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003924 dst_reg->smin_value = smin_ptr;
3925 dst_reg->smax_value = smax_ptr;
3926 dst_reg->umin_value = umin_ptr;
3927 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003928 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003929 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003930 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003931 break;
3932 }
Edward Creef1174f72017-08-07 15:26:19 +01003933 /* A new variable offset is created. Note that off_reg->off
3934 * == 0, since it's a scalar.
3935 * dst_reg gets the pointer type and since some positive
3936 * integer value was added to the pointer, give it a new 'id'
3937 * if it's a PTR_TO_PACKET.
3938 * this creates a new 'base' pointer, off_reg (variable) gets
3939 * added into the variable offset, and we copy the fixed offset
3940 * from ptr_reg.
3941 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003942 if (signed_add_overflows(smin_ptr, smin_val) ||
3943 signed_add_overflows(smax_ptr, smax_val)) {
3944 dst_reg->smin_value = S64_MIN;
3945 dst_reg->smax_value = S64_MAX;
3946 } else {
3947 dst_reg->smin_value = smin_ptr + smin_val;
3948 dst_reg->smax_value = smax_ptr + smax_val;
3949 }
3950 if (umin_ptr + umin_val < umin_ptr ||
3951 umax_ptr + umax_val < umax_ptr) {
3952 dst_reg->umin_value = 0;
3953 dst_reg->umax_value = U64_MAX;
3954 } else {
3955 dst_reg->umin_value = umin_ptr + umin_val;
3956 dst_reg->umax_value = umax_ptr + umax_val;
3957 }
Edward Creef1174f72017-08-07 15:26:19 +01003958 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3959 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003960 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003961 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003962 dst_reg->id = ++env->id_gen;
3963 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003964 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003965 }
Josef Bacik48461132016-09-28 10:54:32 -04003966 break;
3967 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003968 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3969 if (ret < 0) {
3970 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3971 return ret;
3972 }
Edward Creef1174f72017-08-07 15:26:19 +01003973 if (dst_reg == off_reg) {
3974 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003975 verbose(env, "R%d tried to subtract pointer from scalar\n",
3976 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003977 return -EACCES;
3978 }
3979 /* We don't allow subtraction from FP, because (according to
3980 * test_verifier.c test "invalid fp arithmetic", JITs might not
3981 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003982 */
Edward Creef1174f72017-08-07 15:26:19 +01003983 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003984 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3985 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003986 return -EACCES;
3987 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003988 if (known && (ptr_reg->off - smin_val ==
3989 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003990 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003991 dst_reg->smin_value = smin_ptr;
3992 dst_reg->smax_value = smax_ptr;
3993 dst_reg->umin_value = umin_ptr;
3994 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003995 dst_reg->var_off = ptr_reg->var_off;
3996 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003997 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003998 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003999 break;
4000 }
Edward Creef1174f72017-08-07 15:26:19 +01004001 /* A new variable offset is created. If the subtrahend is known
4002 * nonnegative, then any reg->range we had before is still good.
4003 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004004 if (signed_sub_overflows(smin_ptr, smax_val) ||
4005 signed_sub_overflows(smax_ptr, smin_val)) {
4006 /* Overflow possible, we know nothing */
4007 dst_reg->smin_value = S64_MIN;
4008 dst_reg->smax_value = S64_MAX;
4009 } else {
4010 dst_reg->smin_value = smin_ptr - smax_val;
4011 dst_reg->smax_value = smax_ptr - smin_val;
4012 }
4013 if (umin_ptr < umax_val) {
4014 /* Overflow possible, we know nothing */
4015 dst_reg->umin_value = 0;
4016 dst_reg->umax_value = U64_MAX;
4017 } else {
4018 /* Cannot overflow (as long as bounds are consistent) */
4019 dst_reg->umin_value = umin_ptr - umax_val;
4020 dst_reg->umax_value = umax_ptr - umin_val;
4021 }
Edward Creef1174f72017-08-07 15:26:19 +01004022 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
4023 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01004024 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004025 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01004026 dst_reg->id = ++env->id_gen;
4027 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01004028 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01004029 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004030 }
4031 break;
4032 case BPF_AND:
4033 case BPF_OR:
4034 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004035 /* bitwise ops on pointers are troublesome, prohibit. */
4036 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
4037 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01004038 return -EACCES;
4039 default:
4040 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004041 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
4042 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01004043 return -EACCES;
4044 }
4045
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004046 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
4047 return -EINVAL;
4048
Edward Creeb03c9f92017-08-07 15:26:36 +01004049 __update_reg_bounds(dst_reg);
4050 __reg_deduce_bounds(dst_reg);
4051 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01004052
4053 /* For unprivileged we require that resulting offset must be in bounds
4054 * in order to be able to sanitize access later on.
4055 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01004056 if (!env->allow_ptr_leaks) {
4057 if (dst_reg->type == PTR_TO_MAP_VALUE &&
4058 check_map_access(env, dst, dst_reg->off, 1, false)) {
4059 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
4060 "prohibited for !root\n", dst);
4061 return -EACCES;
4062 } else if (dst_reg->type == PTR_TO_STACK &&
4063 check_stack_access(env, dst_reg, dst_reg->off +
4064 dst_reg->var_off.value, 1)) {
4065 verbose(env, "R%d stack pointer arithmetic goes out of range, "
4066 "prohibited for !root\n", dst);
4067 return -EACCES;
4068 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01004069 }
4070
Edward Creef1174f72017-08-07 15:26:19 +01004071 return 0;
4072}
4073
Jann Horn468f6ea2017-12-18 20:11:56 -08004074/* WARNING: This function does calculations on 64-bit values, but the actual
4075 * execution may occur on 32-bit values. Therefore, things like bitshifts
4076 * need extra checks in the 32-bit case.
4077 */
Edward Creef1174f72017-08-07 15:26:19 +01004078static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
4079 struct bpf_insn *insn,
4080 struct bpf_reg_state *dst_reg,
4081 struct bpf_reg_state src_reg)
4082{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004083 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01004084 u8 opcode = BPF_OP(insn->code);
4085 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01004086 s64 smin_val, smax_val;
4087 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08004088 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004089 u32 dst = insn->dst_reg;
4090 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01004091
Jann Hornb7992072018-10-05 18:17:59 +02004092 if (insn_bitness == 32) {
4093 /* Relevant for 32-bit RSH: Information can propagate towards
4094 * LSB, so it isn't sufficient to only truncate the output to
4095 * 32 bits.
4096 */
4097 coerce_reg_to_size(dst_reg, 4);
4098 coerce_reg_to_size(&src_reg, 4);
4099 }
4100
Edward Creeb03c9f92017-08-07 15:26:36 +01004101 smin_val = src_reg.smin_value;
4102 smax_val = src_reg.smax_value;
4103 umin_val = src_reg.umin_value;
4104 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004105 src_known = tnum_is_const(src_reg.var_off);
4106 dst_known = tnum_is_const(dst_reg->var_off);
4107
Daniel Borkmann6f161012018-01-18 01:15:21 +01004108 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
4109 smin_val > smax_val || umin_val > umax_val) {
4110 /* Taint dst register if offset had invalid bounds derived from
4111 * e.g. dead branches.
4112 */
4113 __mark_reg_unknown(dst_reg);
4114 return 0;
4115 }
4116
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08004117 if (!src_known &&
4118 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
4119 __mark_reg_unknown(dst_reg);
4120 return 0;
4121 }
4122
Edward Creef1174f72017-08-07 15:26:19 +01004123 switch (opcode) {
4124 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004125 ret = sanitize_val_alu(env, insn);
4126 if (ret < 0) {
4127 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
4128 return ret;
4129 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004130 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
4131 signed_add_overflows(dst_reg->smax_value, smax_val)) {
4132 dst_reg->smin_value = S64_MIN;
4133 dst_reg->smax_value = S64_MAX;
4134 } else {
4135 dst_reg->smin_value += smin_val;
4136 dst_reg->smax_value += smax_val;
4137 }
4138 if (dst_reg->umin_value + umin_val < umin_val ||
4139 dst_reg->umax_value + umax_val < umax_val) {
4140 dst_reg->umin_value = 0;
4141 dst_reg->umax_value = U64_MAX;
4142 } else {
4143 dst_reg->umin_value += umin_val;
4144 dst_reg->umax_value += umax_val;
4145 }
Edward Creef1174f72017-08-07 15:26:19 +01004146 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
4147 break;
4148 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01004149 ret = sanitize_val_alu(env, insn);
4150 if (ret < 0) {
4151 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
4152 return ret;
4153 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004154 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
4155 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
4156 /* Overflow possible, we know nothing */
4157 dst_reg->smin_value = S64_MIN;
4158 dst_reg->smax_value = S64_MAX;
4159 } else {
4160 dst_reg->smin_value -= smax_val;
4161 dst_reg->smax_value -= smin_val;
4162 }
4163 if (dst_reg->umin_value < umax_val) {
4164 /* Overflow possible, we know nothing */
4165 dst_reg->umin_value = 0;
4166 dst_reg->umax_value = U64_MAX;
4167 } else {
4168 /* Cannot overflow (as long as bounds are consistent) */
4169 dst_reg->umin_value -= umax_val;
4170 dst_reg->umax_value -= umin_val;
4171 }
Edward Creef1174f72017-08-07 15:26:19 +01004172 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04004173 break;
4174 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01004175 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
4176 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01004177 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01004178 __mark_reg_unbounded(dst_reg);
4179 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004180 break;
4181 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004182 /* Both values are positive, so we can work with unsigned and
4183 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01004184 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004185 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
4186 /* Potential overflow, we know nothing */
4187 __mark_reg_unbounded(dst_reg);
4188 /* (except what we can learn from the var_off) */
4189 __update_reg_bounds(dst_reg);
4190 break;
4191 }
4192 dst_reg->umin_value *= umin_val;
4193 dst_reg->umax_value *= umax_val;
4194 if (dst_reg->umax_value > S64_MAX) {
4195 /* Overflow possible, we know nothing */
4196 dst_reg->smin_value = S64_MIN;
4197 dst_reg->smax_value = S64_MAX;
4198 } else {
4199 dst_reg->smin_value = dst_reg->umin_value;
4200 dst_reg->smax_value = dst_reg->umax_value;
4201 }
Josef Bacik48461132016-09-28 10:54:32 -04004202 break;
4203 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01004204 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004205 __mark_reg_known(dst_reg, dst_reg->var_off.value &
4206 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01004207 break;
4208 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004209 /* We get our minimum from the var_off, since that's inherently
4210 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05004211 */
Edward Creef1174f72017-08-07 15:26:19 +01004212 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004213 dst_reg->umin_value = dst_reg->var_off.value;
4214 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
4215 if (dst_reg->smin_value < 0 || smin_val < 0) {
4216 /* Lose signed bounds when ANDing negative numbers,
4217 * ain't nobody got time for that.
4218 */
4219 dst_reg->smin_value = S64_MIN;
4220 dst_reg->smax_value = S64_MAX;
4221 } else {
4222 /* ANDing two positives gives a positive, so safe to
4223 * cast result into s64.
4224 */
4225 dst_reg->smin_value = dst_reg->umin_value;
4226 dst_reg->smax_value = dst_reg->umax_value;
4227 }
4228 /* We may learn something more from the var_off */
4229 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004230 break;
4231 case BPF_OR:
4232 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004233 __mark_reg_known(dst_reg, dst_reg->var_off.value |
4234 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01004235 break;
4236 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004237 /* We get our maximum from the var_off, and our minimum is the
4238 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01004239 */
4240 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004241 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4242 dst_reg->umax_value = dst_reg->var_off.value |
4243 dst_reg->var_off.mask;
4244 if (dst_reg->smin_value < 0 || smin_val < 0) {
4245 /* Lose signed bounds when ORing negative numbers,
4246 * ain't nobody got time for that.
4247 */
4248 dst_reg->smin_value = S64_MIN;
4249 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01004250 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004251 /* ORing two positives gives a positive, so safe to
4252 * cast result into s64.
4253 */
4254 dst_reg->smin_value = dst_reg->umin_value;
4255 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01004256 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004257 /* We may learn something more from the var_off */
4258 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004259 break;
4260 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004261 if (umax_val >= insn_bitness) {
4262 /* Shifts greater than 31 or 63 are undefined.
4263 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004264 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004265 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004266 break;
4267 }
Edward Creeb03c9f92017-08-07 15:26:36 +01004268 /* We lose all sign bit information (except what we can pick
4269 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04004270 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004271 dst_reg->smin_value = S64_MIN;
4272 dst_reg->smax_value = S64_MAX;
4273 /* If we might shift our top bit out, then we know nothing */
4274 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
4275 dst_reg->umin_value = 0;
4276 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07004277 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01004278 dst_reg->umin_value <<= umin_val;
4279 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07004280 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07004281 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004282 /* We may learn something more from the var_off */
4283 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004284 break;
4285 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08004286 if (umax_val >= insn_bitness) {
4287 /* Shifts greater than 31 or 63 are undefined.
4288 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01004289 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004290 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004291 break;
4292 }
Edward Cree4374f252017-12-18 20:11:53 -08004293 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
4294 * be negative, then either:
4295 * 1) src_reg might be zero, so the sign bit of the result is
4296 * unknown, so we lose our signed bounds
4297 * 2) it's known negative, thus the unsigned bounds capture the
4298 * signed bounds
4299 * 3) the signed bounds cross zero, so they tell us nothing
4300 * about the result
4301 * If the value in dst_reg is known nonnegative, then again the
4302 * unsigned bounts capture the signed bounds.
4303 * Thus, in all cases it suffices to blow away our signed bounds
4304 * and rely on inferring new ones from the unsigned bounds and
4305 * var_off of the result.
4306 */
4307 dst_reg->smin_value = S64_MIN;
4308 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07004309 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01004310 dst_reg->umin_value >>= umax_val;
4311 dst_reg->umax_value >>= umin_val;
4312 /* We may learn something more from the var_off */
4313 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004314 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07004315 case BPF_ARSH:
4316 if (umax_val >= insn_bitness) {
4317 /* Shifts greater than 31 or 63 are undefined.
4318 * This includes shifts by a negative number.
4319 */
4320 mark_reg_unknown(env, regs, insn->dst_reg);
4321 break;
4322 }
4323
4324 /* Upon reaching here, src_known is true and
4325 * umax_val is equal to umin_val.
4326 */
4327 dst_reg->smin_value >>= umin_val;
4328 dst_reg->smax_value >>= umin_val;
4329 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
4330
4331 /* blow away the dst_reg umin_value/umax_value and rely on
4332 * dst_reg var_off to refine the result.
4333 */
4334 dst_reg->umin_value = 0;
4335 dst_reg->umax_value = U64_MAX;
4336 __update_reg_bounds(dst_reg);
4337 break;
Josef Bacik48461132016-09-28 10:54:32 -04004338 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004339 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004340 break;
4341 }
4342
Jann Horn468f6ea2017-12-18 20:11:56 -08004343 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4344 /* 32-bit ALU ops are (32,32)->32 */
4345 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08004346 }
4347
Edward Creeb03c9f92017-08-07 15:26:36 +01004348 __reg_deduce_bounds(dst_reg);
4349 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004350 return 0;
4351}
4352
4353/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
4354 * and var_off.
4355 */
4356static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4357 struct bpf_insn *insn)
4358{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004359 struct bpf_verifier_state *vstate = env->cur_state;
4360 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4361 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01004362 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4363 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01004364
4365 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004366 src_reg = NULL;
4367 if (dst_reg->type != SCALAR_VALUE)
4368 ptr_reg = dst_reg;
4369 if (BPF_SRC(insn->code) == BPF_X) {
4370 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01004371 if (src_reg->type != SCALAR_VALUE) {
4372 if (dst_reg->type != SCALAR_VALUE) {
4373 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004374 * an arbitrary scalar. Disallow all math except
4375 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01004376 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07004377 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004378 mark_reg_unknown(env, regs, insn->dst_reg);
4379 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01004380 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004381 verbose(env, "R%d pointer %s pointer prohibited\n",
4382 insn->dst_reg,
4383 bpf_alu_string[opcode >> 4]);
4384 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01004385 } else {
4386 /* scalar += pointer
4387 * This is legal, but we have to reverse our
4388 * src/dest handling in computing the range
4389 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004390 return adjust_ptr_min_max_vals(env, insn,
4391 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004392 }
4393 } else if (ptr_reg) {
4394 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004395 return adjust_ptr_min_max_vals(env, insn,
4396 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004397 }
4398 } else {
4399 /* Pretend the src is a reg with a known value, since we only
4400 * need to be able to read from this state.
4401 */
4402 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004403 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01004404 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08004405 if (ptr_reg) /* pointer += K */
4406 return adjust_ptr_min_max_vals(env, insn,
4407 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004408 }
4409
4410 /* Got here implies adding two SCALAR_VALUEs */
4411 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004412 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004413 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004414 return -EINVAL;
4415 }
4416 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004417 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004418 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01004419 return -EINVAL;
4420 }
4421 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004422}
4423
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004424/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004425static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004426{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004427 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004428 u8 opcode = BPF_OP(insn->code);
4429 int err;
4430
4431 if (opcode == BPF_END || opcode == BPF_NEG) {
4432 if (opcode == BPF_NEG) {
4433 if (BPF_SRC(insn->code) != 0 ||
4434 insn->src_reg != BPF_REG_0 ||
4435 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004436 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004437 return -EINVAL;
4438 }
4439 } else {
4440 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01004441 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4442 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004443 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004444 return -EINVAL;
4445 }
4446 }
4447
4448 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004449 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004450 if (err)
4451 return err;
4452
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004453 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004454 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004455 insn->dst_reg);
4456 return -EACCES;
4457 }
4458
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004459 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004460 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004461 if (err)
4462 return err;
4463
4464 } else if (opcode == BPF_MOV) {
4465
4466 if (BPF_SRC(insn->code) == BPF_X) {
4467 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004468 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004469 return -EINVAL;
4470 }
4471
4472 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01004473 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004474 if (err)
4475 return err;
4476 } else {
4477 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004478 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004479 return -EINVAL;
4480 }
4481 }
4482
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004483 /* check dest operand, mark as required later */
4484 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004485 if (err)
4486 return err;
4487
4488 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05004489 struct bpf_reg_state *src_reg = regs + insn->src_reg;
4490 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4491
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004492 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4493 /* case: R1 = R2
4494 * copy register state to dest reg
4495 */
Jiong Wange434b8c2018-12-07 12:16:18 -05004496 *dst_reg = *src_reg;
4497 dst_reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01004498 dst_reg->subreg_def = DEF_NOT_SUBREG;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004499 } else {
Edward Creef1174f72017-08-07 15:26:19 +01004500 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004501 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004502 verbose(env,
4503 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004504 insn->src_reg);
4505 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05004506 } else if (src_reg->type == SCALAR_VALUE) {
4507 *dst_reg = *src_reg;
4508 dst_reg->live |= REG_LIVE_WRITTEN;
Jiong Wang5327ed32019-05-24 23:25:12 +01004509 dst_reg->subreg_def = env->insn_idx + 1;
Jiong Wange434b8c2018-12-07 12:16:18 -05004510 } else {
4511 mark_reg_unknown(env, regs,
4512 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004513 }
Jiong Wange434b8c2018-12-07 12:16:18 -05004514 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004515 }
4516 } else {
4517 /* case: R = imm
4518 * remember the value we stored into this reg
4519 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01004520 /* clear any state __mark_reg_known doesn't set */
4521 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004522 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08004523 if (BPF_CLASS(insn->code) == BPF_ALU64) {
4524 __mark_reg_known(regs + insn->dst_reg,
4525 insn->imm);
4526 } else {
4527 __mark_reg_known(regs + insn->dst_reg,
4528 (u32)insn->imm);
4529 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004530 }
4531
4532 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004533 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004534 return -EINVAL;
4535
4536 } else { /* all other ALU ops: and, sub, xor, add, ... */
4537
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004538 if (BPF_SRC(insn->code) == BPF_X) {
4539 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004540 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004541 return -EINVAL;
4542 }
4543 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004544 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004545 if (err)
4546 return err;
4547 } else {
4548 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004549 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004550 return -EINVAL;
4551 }
4552 }
4553
4554 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004555 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004556 if (err)
4557 return err;
4558
4559 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
4560 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004561 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004562 return -EINVAL;
4563 }
4564
Rabin Vincent229394e82016-01-12 20:17:08 +01004565 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
4566 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
4567 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
4568
4569 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004570 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01004571 return -EINVAL;
4572 }
4573 }
4574
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004575 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01004576 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004577 if (err)
4578 return err;
4579
Edward Creef1174f72017-08-07 15:26:19 +01004580 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004581 }
4582
4583 return 0;
4584}
4585
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004586static void __find_good_pkt_pointers(struct bpf_func_state *state,
4587 struct bpf_reg_state *dst_reg,
4588 enum bpf_reg_type type, u16 new_range)
4589{
4590 struct bpf_reg_state *reg;
4591 int i;
4592
4593 for (i = 0; i < MAX_BPF_REG; i++) {
4594 reg = &state->regs[i];
4595 if (reg->type == type && reg->id == dst_reg->id)
4596 /* keep the maximum range already checked */
4597 reg->range = max(reg->range, new_range);
4598 }
4599
4600 bpf_for_each_spilled_reg(i, state, reg) {
4601 if (!reg)
4602 continue;
4603 if (reg->type == type && reg->id == dst_reg->id)
4604 reg->range = max(reg->range, new_range);
4605 }
4606}
4607
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004608static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004609 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01004610 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004611 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004612{
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004613 u16 new_range;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004614 int i;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004615
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004616 if (dst_reg->off < 0 ||
4617 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01004618 /* This doesn't give us any range */
4619 return;
4620
Edward Creeb03c9f92017-08-07 15:26:36 +01004621 if (dst_reg->umax_value > MAX_PACKET_OFF ||
4622 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01004623 /* Risk of overflow. For instance, ptr + (1<<63) may be less
4624 * than pkt_end, but that's because it's also less than pkt.
4625 */
4626 return;
4627
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004628 new_range = dst_reg->off;
4629 if (range_right_open)
4630 new_range--;
4631
4632 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004633 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004634 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004635 *
4636 * r2 = r3;
4637 * r2 += 8;
4638 * if (r2 > pkt_end) goto <handle exception>
4639 * <access okay>
4640 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004641 * r2 = r3;
4642 * r2 += 8;
4643 * if (r2 < pkt_end) goto <access okay>
4644 * <handle exception>
4645 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004646 * Where:
4647 * r2 == dst_reg, pkt_end == src_reg
4648 * r2=pkt(id=n,off=8,r=0)
4649 * r3=pkt(id=n,off=0,r=0)
4650 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004651 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004652 *
4653 * r2 = r3;
4654 * r2 += 8;
4655 * if (pkt_end >= r2) goto <access okay>
4656 * <handle exception>
4657 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004658 * r2 = r3;
4659 * r2 += 8;
4660 * if (pkt_end <= r2) goto <handle exception>
4661 * <access okay>
4662 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004663 * Where:
4664 * pkt_end == dst_reg, r2 == src_reg
4665 * r2=pkt(id=n,off=8,r=0)
4666 * r3=pkt(id=n,off=0,r=0)
4667 *
4668 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004669 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4670 * and [r3, r3 + 8-1) respectively is safe to access depending on
4671 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004672 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004673
Edward Creef1174f72017-08-07 15:26:19 +01004674 /* If our ids match, then we must have the same max_value. And we
4675 * don't care about the other reg's fixed offset, since if it's too big
4676 * the range won't allow anything.
4677 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4678 */
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02004679 for (i = 0; i <= vstate->curframe; i++)
4680 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
4681 new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004682}
4683
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004684/* compute branch direction of the expression "if (reg opcode val) goto target;"
4685 * and return:
4686 * 1 - branch will be taken and "goto target" will be executed
4687 * 0 - branch will not be taken and fall-through to next insn
4688 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4689 */
Jiong Wang092ed092019-01-26 12:26:01 -05004690static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4691 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004692{
Jiong Wang092ed092019-01-26 12:26:01 -05004693 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004694 s64 sval;
4695
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004696 if (__is_pointer_value(false, reg))
4697 return -1;
4698
Jiong Wang092ed092019-01-26 12:26:01 -05004699 if (is_jmp32) {
4700 reg_lo = *reg;
4701 reg = &reg_lo;
4702 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4703 * could truncate high bits and update umin/umax according to
4704 * information of low bits.
4705 */
4706 coerce_reg_to_size(reg, 4);
4707 /* smin/smax need special handling. For example, after coerce,
4708 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4709 * used as operand to JMP32. It is a negative number from s32's
4710 * point of view, while it is a positive number when seen as
4711 * s64. The smin/smax are kept as s64, therefore, when used with
4712 * JMP32, they need to be transformed into s32, then sign
4713 * extended back to s64.
4714 *
4715 * Also, smin/smax were copied from umin/umax. If umin/umax has
4716 * different sign bit, then min/max relationship doesn't
4717 * maintain after casting into s32, for this case, set smin/smax
4718 * to safest range.
4719 */
4720 if ((reg->umax_value ^ reg->umin_value) &
4721 (1ULL << 31)) {
4722 reg->smin_value = S32_MIN;
4723 reg->smax_value = S32_MAX;
4724 }
4725 reg->smin_value = (s64)(s32)reg->smin_value;
4726 reg->smax_value = (s64)(s32)reg->smax_value;
4727
4728 val = (u32)val;
4729 sval = (s64)(s32)val;
4730 } else {
4731 sval = (s64)val;
4732 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004733
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004734 switch (opcode) {
4735 case BPF_JEQ:
4736 if (tnum_is_const(reg->var_off))
4737 return !!tnum_equals_const(reg->var_off, val);
4738 break;
4739 case BPF_JNE:
4740 if (tnum_is_const(reg->var_off))
4741 return !tnum_equals_const(reg->var_off, val);
4742 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08004743 case BPF_JSET:
4744 if ((~reg->var_off.mask & reg->var_off.value) & val)
4745 return 1;
4746 if (!((reg->var_off.mask | reg->var_off.value) & val))
4747 return 0;
4748 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004749 case BPF_JGT:
4750 if (reg->umin_value > val)
4751 return 1;
4752 else if (reg->umax_value <= val)
4753 return 0;
4754 break;
4755 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004756 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004757 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004758 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004759 return 0;
4760 break;
4761 case BPF_JLT:
4762 if (reg->umax_value < val)
4763 return 1;
4764 else if (reg->umin_value >= val)
4765 return 0;
4766 break;
4767 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004768 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004769 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004770 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004771 return 0;
4772 break;
4773 case BPF_JGE:
4774 if (reg->umin_value >= val)
4775 return 1;
4776 else if (reg->umax_value < val)
4777 return 0;
4778 break;
4779 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004780 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004781 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004782 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004783 return 0;
4784 break;
4785 case BPF_JLE:
4786 if (reg->umax_value <= val)
4787 return 1;
4788 else if (reg->umin_value > val)
4789 return 0;
4790 break;
4791 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004792 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004793 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004794 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004795 return 0;
4796 break;
4797 }
4798
4799 return -1;
4800}
4801
Jiong Wang092ed092019-01-26 12:26:01 -05004802/* Generate min value of the high 32-bit from TNUM info. */
4803static u64 gen_hi_min(struct tnum var)
4804{
4805 return var.value & ~0xffffffffULL;
4806}
4807
4808/* Generate max value of the high 32-bit from TNUM info. */
4809static u64 gen_hi_max(struct tnum var)
4810{
4811 return (var.value | var.mask) & ~0xffffffffULL;
4812}
4813
4814/* Return true if VAL is compared with a s64 sign extended from s32, and they
4815 * are with the same signedness.
4816 */
4817static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4818{
4819 return ((s32)sval >= 0 &&
4820 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4821 ((s32)sval < 0 &&
4822 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4823}
4824
Josef Bacik48461132016-09-28 10:54:32 -04004825/* Adjusts the register min/max values in the case that the dst_reg is the
4826 * variable register that we are working on, and src_reg is a constant or we're
4827 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01004828 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04004829 */
4830static void reg_set_min_max(struct bpf_reg_state *true_reg,
4831 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004832 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004833{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004834 s64 sval;
4835
Edward Creef1174f72017-08-07 15:26:19 +01004836 /* If the dst_reg is a pointer, we can't learn anything about its
4837 * variable offset from the compare (unless src_reg were a pointer into
4838 * the same object, but we don't bother with that.
4839 * Since false_reg and true_reg have the same type by construction, we
4840 * only need to check one of them for pointerness.
4841 */
4842 if (__is_pointer_value(false, false_reg))
4843 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004844
Jiong Wang092ed092019-01-26 12:26:01 -05004845 val = is_jmp32 ? (u32)val : val;
4846 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004847
Josef Bacik48461132016-09-28 10:54:32 -04004848 switch (opcode) {
4849 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004850 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004851 {
4852 struct bpf_reg_state *reg =
4853 opcode == BPF_JEQ ? true_reg : false_reg;
4854
4855 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4856 * if it is true we know the value for sure. Likewise for
4857 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04004858 */
Jiong Wang092ed092019-01-26 12:26:01 -05004859 if (is_jmp32) {
4860 u64 old_v = reg->var_off.value;
4861 u64 hi_mask = ~0xffffffffULL;
4862
4863 reg->var_off.value = (old_v & hi_mask) | val;
4864 reg->var_off.mask &= hi_mask;
4865 } else {
4866 __mark_reg_known(reg, val);
4867 }
Josef Bacik48461132016-09-28 10:54:32 -04004868 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004869 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004870 case BPF_JSET:
4871 false_reg->var_off = tnum_and(false_reg->var_off,
4872 tnum_const(~val));
4873 if (is_power_of_2(val))
4874 true_reg->var_off = tnum_or(true_reg->var_off,
4875 tnum_const(val));
4876 break;
Josef Bacik48461132016-09-28 10:54:32 -04004877 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004878 case BPF_JGT:
4879 {
4880 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
4881 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4882
Jiong Wang092ed092019-01-26 12:26:01 -05004883 if (is_jmp32) {
4884 false_umax += gen_hi_max(false_reg->var_off);
4885 true_umin += gen_hi_min(true_reg->var_off);
4886 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004887 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4888 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01004889 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004890 }
Josef Bacik48461132016-09-28 10:54:32 -04004891 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004892 case BPF_JSGT:
4893 {
4894 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
4895 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4896
Jiong Wang092ed092019-01-26 12:26:01 -05004897 /* If the full s64 was not sign-extended from s32 then don't
4898 * deduct further info.
4899 */
4900 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4901 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004902 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4903 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04004904 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004905 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004906 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004907 case BPF_JLT:
4908 {
4909 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
4910 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4911
Jiong Wang092ed092019-01-26 12:26:01 -05004912 if (is_jmp32) {
4913 false_umin += gen_hi_min(false_reg->var_off);
4914 true_umax += gen_hi_max(true_reg->var_off);
4915 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004916 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4917 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004918 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004919 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004920 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004921 case BPF_JSLT:
4922 {
4923 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
4924 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4925
Jiong Wang092ed092019-01-26 12:26:01 -05004926 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4927 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004928 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4929 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004930 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004931 }
Josef Bacik48461132016-09-28 10:54:32 -04004932 default:
4933 break;
4934 }
4935
Edward Creeb03c9f92017-08-07 15:26:36 +01004936 __reg_deduce_bounds(false_reg);
4937 __reg_deduce_bounds(true_reg);
4938 /* We might have learned some bits from the bounds. */
4939 __reg_bound_offset(false_reg);
4940 __reg_bound_offset(true_reg);
4941 /* Intersecting with the old var_off might have improved our bounds
4942 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4943 * then new var_off is (0; 0x7f...fc) which improves our umax.
4944 */
4945 __update_reg_bounds(false_reg);
4946 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004947}
4948
Edward Creef1174f72017-08-07 15:26:19 +01004949/* Same as above, but for the case that dst_reg holds a constant and src_reg is
4950 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04004951 */
4952static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4953 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004954 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004955{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004956 s64 sval;
4957
Edward Creef1174f72017-08-07 15:26:19 +01004958 if (__is_pointer_value(false, false_reg))
4959 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004960
Jiong Wang092ed092019-01-26 12:26:01 -05004961 val = is_jmp32 ? (u32)val : val;
4962 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004963
Josef Bacik48461132016-09-28 10:54:32 -04004964 switch (opcode) {
4965 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004966 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004967 {
4968 struct bpf_reg_state *reg =
4969 opcode == BPF_JEQ ? true_reg : false_reg;
4970
Jiong Wang092ed092019-01-26 12:26:01 -05004971 if (is_jmp32) {
4972 u64 old_v = reg->var_off.value;
4973 u64 hi_mask = ~0xffffffffULL;
4974
4975 reg->var_off.value = (old_v & hi_mask) | val;
4976 reg->var_off.mask &= hi_mask;
4977 } else {
4978 __mark_reg_known(reg, val);
4979 }
Josef Bacik48461132016-09-28 10:54:32 -04004980 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004981 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004982 case BPF_JSET:
4983 false_reg->var_off = tnum_and(false_reg->var_off,
4984 tnum_const(~val));
4985 if (is_power_of_2(val))
4986 true_reg->var_off = tnum_or(true_reg->var_off,
4987 tnum_const(val));
4988 break;
Josef Bacik48461132016-09-28 10:54:32 -04004989 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004990 case BPF_JGT:
4991 {
4992 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
4993 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4994
Jiong Wang092ed092019-01-26 12:26:01 -05004995 if (is_jmp32) {
4996 false_umin += gen_hi_min(false_reg->var_off);
4997 true_umax += gen_hi_max(true_reg->var_off);
4998 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004999 false_reg->umin_value = max(false_reg->umin_value, false_umin);
5000 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01005001 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005002 }
Josef Bacik48461132016-09-28 10:54:32 -04005003 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005004 case BPF_JSGT:
5005 {
5006 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
5007 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
5008
Jiong Wang092ed092019-01-26 12:26:01 -05005009 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5010 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005011 false_reg->smin_value = max(false_reg->smin_value, false_smin);
5012 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04005013 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005014 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005015 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005016 case BPF_JLT:
5017 {
5018 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
5019 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
5020
Jiong Wang092ed092019-01-26 12:26:01 -05005021 if (is_jmp32) {
5022 false_umax += gen_hi_max(false_reg->var_off);
5023 true_umin += gen_hi_min(true_reg->var_off);
5024 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05005025 false_reg->umax_value = min(false_reg->umax_value, false_umax);
5026 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005027 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005028 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005029 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05005030 case BPF_JSLT:
5031 {
5032 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
5033 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
5034
Jiong Wang092ed092019-01-26 12:26:01 -05005035 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5036 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005037 false_reg->smax_value = min(false_reg->smax_value, false_smax);
5038 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02005039 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05005040 }
Josef Bacik48461132016-09-28 10:54:32 -04005041 default:
5042 break;
5043 }
5044
Edward Creeb03c9f92017-08-07 15:26:36 +01005045 __reg_deduce_bounds(false_reg);
5046 __reg_deduce_bounds(true_reg);
5047 /* We might have learned some bits from the bounds. */
5048 __reg_bound_offset(false_reg);
5049 __reg_bound_offset(true_reg);
5050 /* Intersecting with the old var_off might have improved our bounds
5051 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
5052 * then new var_off is (0; 0x7f...fc) which improves our umax.
5053 */
5054 __update_reg_bounds(false_reg);
5055 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01005056}
5057
5058/* Regs are known to be equal, so intersect their min/max/var_off */
5059static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
5060 struct bpf_reg_state *dst_reg)
5061{
Edward Creeb03c9f92017-08-07 15:26:36 +01005062 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
5063 dst_reg->umin_value);
5064 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
5065 dst_reg->umax_value);
5066 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
5067 dst_reg->smin_value);
5068 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
5069 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01005070 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
5071 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01005072 /* We might have learned new bounds from the var_off. */
5073 __update_reg_bounds(src_reg);
5074 __update_reg_bounds(dst_reg);
5075 /* We might have learned something about the sign bit. */
5076 __reg_deduce_bounds(src_reg);
5077 __reg_deduce_bounds(dst_reg);
5078 /* We might have learned some bits from the bounds. */
5079 __reg_bound_offset(src_reg);
5080 __reg_bound_offset(dst_reg);
5081 /* Intersecting with the old var_off might have improved our bounds
5082 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
5083 * then new var_off is (0; 0x7f...fc) which improves our umax.
5084 */
5085 __update_reg_bounds(src_reg);
5086 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01005087}
5088
5089static void reg_combine_min_max(struct bpf_reg_state *true_src,
5090 struct bpf_reg_state *true_dst,
5091 struct bpf_reg_state *false_src,
5092 struct bpf_reg_state *false_dst,
5093 u8 opcode)
5094{
5095 switch (opcode) {
5096 case BPF_JEQ:
5097 __reg_combine_min_max(true_src, true_dst);
5098 break;
5099 case BPF_JNE:
5100 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01005101 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02005102 }
Josef Bacik48461132016-09-28 10:54:32 -04005103}
5104
Joe Stringerfd978bf72018-10-02 13:35:35 -07005105static void mark_ptr_or_null_reg(struct bpf_func_state *state,
5106 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07005107 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02005108{
Joe Stringer840b9612018-10-02 13:35:32 -07005109 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01005110 /* Old offset (both fixed and variable parts) should
5111 * have been known-zero, because we don't allow pointer
5112 * arithmetic on pointers that might be NULL.
5113 */
Edward Creeb03c9f92017-08-07 15:26:36 +01005114 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
5115 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01005116 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01005117 __mark_reg_known_zero(reg);
5118 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01005119 }
5120 if (is_null) {
5121 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07005122 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
5123 if (reg->map_ptr->inner_map_meta) {
5124 reg->type = CONST_PTR_TO_MAP;
5125 reg->map_ptr = reg->map_ptr->inner_map_meta;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07005126 } else if (reg->map_ptr->map_type ==
5127 BPF_MAP_TYPE_XSKMAP) {
5128 reg->type = PTR_TO_XDP_SOCK;
Joe Stringer840b9612018-10-02 13:35:32 -07005129 } else {
5130 reg->type = PTR_TO_MAP_VALUE;
5131 }
Joe Stringerc64b7982018-10-02 13:35:33 -07005132 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
5133 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08005134 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
5135 reg->type = PTR_TO_SOCK_COMMON;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08005136 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
5137 reg->type = PTR_TO_TCP_SOCK;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07005138 }
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005139 if (is_null) {
5140 /* We don't need id and ref_obj_id from this point
5141 * onwards anymore, thus we should better reset it,
5142 * so that state pruning has chances to take effect.
5143 */
5144 reg->id = 0;
5145 reg->ref_obj_id = 0;
5146 } else if (!reg_may_point_to_spin_lock(reg)) {
5147 /* For not-NULL ptr, reg->ref_obj_id will be reset
5148 * in release_reg_references().
5149 *
5150 * reg->id is still used by spin_lock ptr. Other
5151 * than spin_lock ptr type, reg->id can be reset.
Joe Stringerfd978bf72018-10-02 13:35:35 -07005152 */
5153 reg->id = 0;
5154 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02005155 }
5156}
5157
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005158static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
5159 bool is_null)
5160{
5161 struct bpf_reg_state *reg;
5162 int i;
5163
5164 for (i = 0; i < MAX_BPF_REG; i++)
5165 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
5166
5167 bpf_for_each_spilled_reg(i, state, reg) {
5168 if (!reg)
5169 continue;
5170 mark_ptr_or_null_reg(state, reg, id, is_null);
5171 }
5172}
5173
Thomas Graf57a09bf2016-10-18 19:51:19 +02005174/* The logic is similar to find_good_pkt_pointers(), both could eventually
5175 * be folded together at some point.
5176 */
Joe Stringer840b9612018-10-02 13:35:32 -07005177static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
5178 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02005179{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005180 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005181 struct bpf_reg_state *regs = state->regs;
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005182 u32 ref_obj_id = regs[regno].ref_obj_id;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01005183 u32 id = regs[regno].id;
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005184 int i;
Thomas Graf57a09bf2016-10-18 19:51:19 +02005185
Martin KaFai Lau1b986582019-03-12 10:23:02 -07005186 if (ref_obj_id && ref_obj_id == id && is_null)
5187 /* regs[regno] is in the " == NULL" branch.
5188 * No one could have freed the reference state before
5189 * doing the NULL check.
5190 */
5191 WARN_ON_ONCE(release_reference_state(state, id));
Joe Stringerfd978bf72018-10-02 13:35:35 -07005192
Paul Chaignonc6a9efa2019-04-24 21:50:42 +02005193 for (i = 0; i <= vstate->curframe; i++)
5194 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02005195}
5196
Daniel Borkmann5beca082017-11-01 23:58:10 +01005197static bool try_match_pkt_pointers(const struct bpf_insn *insn,
5198 struct bpf_reg_state *dst_reg,
5199 struct bpf_reg_state *src_reg,
5200 struct bpf_verifier_state *this_branch,
5201 struct bpf_verifier_state *other_branch)
5202{
5203 if (BPF_SRC(insn->code) != BPF_X)
5204 return false;
5205
Jiong Wang092ed092019-01-26 12:26:01 -05005206 /* Pointers are always 64-bit. */
5207 if (BPF_CLASS(insn->code) == BPF_JMP32)
5208 return false;
5209
Daniel Borkmann5beca082017-11-01 23:58:10 +01005210 switch (BPF_OP(insn->code)) {
5211 case BPF_JGT:
5212 if ((dst_reg->type == PTR_TO_PACKET &&
5213 src_reg->type == PTR_TO_PACKET_END) ||
5214 (dst_reg->type == PTR_TO_PACKET_META &&
5215 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5216 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
5217 find_good_pkt_pointers(this_branch, dst_reg,
5218 dst_reg->type, false);
5219 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5220 src_reg->type == PTR_TO_PACKET) ||
5221 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5222 src_reg->type == PTR_TO_PACKET_META)) {
5223 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
5224 find_good_pkt_pointers(other_branch, src_reg,
5225 src_reg->type, true);
5226 } else {
5227 return false;
5228 }
5229 break;
5230 case BPF_JLT:
5231 if ((dst_reg->type == PTR_TO_PACKET &&
5232 src_reg->type == PTR_TO_PACKET_END) ||
5233 (dst_reg->type == PTR_TO_PACKET_META &&
5234 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5235 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
5236 find_good_pkt_pointers(other_branch, dst_reg,
5237 dst_reg->type, true);
5238 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5239 src_reg->type == PTR_TO_PACKET) ||
5240 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5241 src_reg->type == PTR_TO_PACKET_META)) {
5242 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
5243 find_good_pkt_pointers(this_branch, src_reg,
5244 src_reg->type, false);
5245 } else {
5246 return false;
5247 }
5248 break;
5249 case BPF_JGE:
5250 if ((dst_reg->type == PTR_TO_PACKET &&
5251 src_reg->type == PTR_TO_PACKET_END) ||
5252 (dst_reg->type == PTR_TO_PACKET_META &&
5253 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5254 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
5255 find_good_pkt_pointers(this_branch, dst_reg,
5256 dst_reg->type, true);
5257 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5258 src_reg->type == PTR_TO_PACKET) ||
5259 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5260 src_reg->type == PTR_TO_PACKET_META)) {
5261 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
5262 find_good_pkt_pointers(other_branch, src_reg,
5263 src_reg->type, false);
5264 } else {
5265 return false;
5266 }
5267 break;
5268 case BPF_JLE:
5269 if ((dst_reg->type == PTR_TO_PACKET &&
5270 src_reg->type == PTR_TO_PACKET_END) ||
5271 (dst_reg->type == PTR_TO_PACKET_META &&
5272 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5273 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
5274 find_good_pkt_pointers(other_branch, dst_reg,
5275 dst_reg->type, false);
5276 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5277 src_reg->type == PTR_TO_PACKET) ||
5278 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5279 src_reg->type == PTR_TO_PACKET_META)) {
5280 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
5281 find_good_pkt_pointers(this_branch, src_reg,
5282 src_reg->type, true);
5283 } else {
5284 return false;
5285 }
5286 break;
5287 default:
5288 return false;
5289 }
5290
5291 return true;
5292}
5293
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005294static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005295 struct bpf_insn *insn, int *insn_idx)
5296{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005297 struct bpf_verifier_state *this_branch = env->cur_state;
5298 struct bpf_verifier_state *other_branch;
5299 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005300 struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005301 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05005302 bool is_jmp32;
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005303 int pred = -1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005304 int err;
5305
Jiong Wang092ed092019-01-26 12:26:01 -05005306 /* Only conditional jumps are expected to reach here. */
5307 if (opcode == BPF_JA || opcode > BPF_JSLE) {
5308 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005309 return -EINVAL;
5310 }
5311
5312 if (BPF_SRC(insn->code) == BPF_X) {
5313 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005314 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005315 return -EINVAL;
5316 }
5317
5318 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005319 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005320 if (err)
5321 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005322
5323 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005324 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005325 insn->src_reg);
5326 return -EACCES;
5327 }
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005328 src_reg = &regs[insn->src_reg];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005329 } else {
5330 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05005331 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005332 return -EINVAL;
5333 }
5334 }
5335
5336 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01005337 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005338 if (err)
5339 return err;
5340
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005341 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05005342 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005343
Alexei Starovoitovfb8d2512019-06-15 12:12:19 -07005344 if (BPF_SRC(insn->code) == BPF_K)
5345 pred = is_branch_taken(dst_reg, insn->imm,
5346 opcode, is_jmp32);
5347 else if (src_reg->type == SCALAR_VALUE &&
5348 tnum_is_const(src_reg->var_off))
5349 pred = is_branch_taken(dst_reg, src_reg->var_off.value,
5350 opcode, is_jmp32);
5351 if (pred == 1) {
5352 /* only follow the goto, ignore fall-through */
5353 *insn_idx += insn->off;
5354 return 0;
5355 } else if (pred == 0) {
5356 /* only follow fall-through branch, since
5357 * that's where the program will go
5358 */
5359 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005360 }
5361
Daniel Borkmann979d63d2019-01-03 00:58:34 +01005362 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5363 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005364 if (!other_branch)
5365 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005366 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005367
Josef Bacik48461132016-09-28 10:54:32 -04005368 /* detect if we are comparing against a constant value so we can adjust
5369 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01005370 * this is only legit if both are scalars (or pointers to the same
5371 * object, I suppose, but we don't support that right now), because
5372 * otherwise the different base pointers mean the offsets aren't
5373 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04005374 */
5375 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05005376 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
5377 struct bpf_reg_state lo_reg0 = *dst_reg;
5378 struct bpf_reg_state lo_reg1 = *src_reg;
5379 struct bpf_reg_state *src_lo, *dst_lo;
5380
5381 dst_lo = &lo_reg0;
5382 src_lo = &lo_reg1;
5383 coerce_reg_to_size(dst_lo, 4);
5384 coerce_reg_to_size(src_lo, 4);
5385
Edward Creef1174f72017-08-07 15:26:19 +01005386 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05005387 src_reg->type == SCALAR_VALUE) {
5388 if (tnum_is_const(src_reg->var_off) ||
5389 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005390 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005391 dst_reg,
5392 is_jmp32
5393 ? src_lo->var_off.value
5394 : src_reg->var_off.value,
5395 opcode, is_jmp32);
5396 else if (tnum_is_const(dst_reg->var_off) ||
5397 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005398 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005399 src_reg,
5400 is_jmp32
5401 ? dst_lo->var_off.value
5402 : dst_reg->var_off.value,
5403 opcode, is_jmp32);
5404 else if (!is_jmp32 &&
5405 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01005406 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005407 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5408 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005409 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01005410 }
5411 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005412 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05005413 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04005414 }
5415
Jiong Wang092ed092019-01-26 12:26:01 -05005416 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
5417 * NOTE: these optimizations below are related with pointer comparison
5418 * which will never be JMP32.
5419 */
5420 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005421 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07005422 reg_type_may_be_null(dst_reg->type)) {
5423 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02005424 * safe or unknown depending R == 0 or R != 0 conditional.
5425 */
Joe Stringer840b9612018-10-02 13:35:32 -07005426 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
5427 opcode == BPF_JNE);
5428 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
5429 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01005430 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
5431 this_branch, other_branch) &&
5432 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005433 verbose(env, "R%d pointer comparison prohibited\n",
5434 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07005435 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005436 }
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07005437 if (env->log.level & BPF_LOG_LEVEL)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005438 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005439 return 0;
5440}
5441
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005442/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005443static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005444{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005445 struct bpf_insn_aux_data *aux = cur_aux(env);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005446 struct bpf_reg_state *regs = cur_regs(env);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005447 struct bpf_map *map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005448 int err;
5449
5450 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005451 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005452 return -EINVAL;
5453 }
5454 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005455 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005456 return -EINVAL;
5457 }
5458
Edward Creedc503a82017-08-15 20:34:35 +01005459 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005460 if (err)
5461 return err;
5462
Jakub Kicinski6b173872016-09-21 11:43:59 +01005463 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01005464 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5465
Edward Creef1174f72017-08-07 15:26:19 +01005466 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01005467 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005468 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01005469 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005470
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005471 map = env->used_maps[aux->map_index];
5472 mark_reg_known_zero(env, regs, insn->dst_reg);
5473 regs[insn->dst_reg].map_ptr = map;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005474
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02005475 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
5476 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
5477 regs[insn->dst_reg].off = aux->map_off;
5478 if (map_value_has_spin_lock(map))
5479 regs[insn->dst_reg].id = ++env->id_gen;
5480 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
5481 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5482 } else {
5483 verbose(env, "bpf verifier is misconfigured\n");
5484 return -EINVAL;
5485 }
5486
Alexei Starovoitov17a52672014-09-26 00:17:06 -07005487 return 0;
5488}
5489
Daniel Borkmann96be4322015-03-01 12:31:46 +01005490static bool may_access_skb(enum bpf_prog_type type)
5491{
5492 switch (type) {
5493 case BPF_PROG_TYPE_SOCKET_FILTER:
5494 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01005495 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01005496 return true;
5497 default:
5498 return false;
5499 }
5500}
5501
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005502/* verify safety of LD_ABS|LD_IND instructions:
5503 * - they can only appear in the programs where ctx == skb
5504 * - since they are wrappers of function calls, they scratch R1-R5 registers,
5505 * preserve R6-R9, and store return value into R0
5506 *
5507 * Implicit input:
5508 * ctx == skb == R6 == CTX
5509 *
5510 * Explicit input:
5511 * SRC == any register
5512 * IMM == 32-bit immediate
5513 *
5514 * Output:
5515 * R0 - 8/16/32-bit skb data converted to cpu endianness
5516 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005517static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005518{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005519 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005520 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005521 int i, err;
5522
Daniel Borkmann24701ec2015-03-01 12:31:47 +01005523 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005524 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005525 return -EINVAL;
5526 }
5527
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02005528 if (!env->ops->gen_ld_abs) {
5529 verbose(env, "bpf verifier is misconfigured\n");
5530 return -EINVAL;
5531 }
5532
Jiong Wangf910cef2018-05-02 16:17:17 -04005533 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005534 /* when program has LD_ABS insn JITs and interpreter assume
5535 * that r1 == ctx == skb which is not the case for callees
5536 * that can have arbitrary arguments. It's problematic
5537 * for main prog as well since JITs would need to analyze
5538 * all functions in order to make proper register save/restore
5539 * decisions in the main prog. Hence disallow LD_ABS with calls
5540 */
5541 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
5542 return -EINVAL;
5543 }
5544
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005545 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07005546 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005547 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005548 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005549 return -EINVAL;
5550 }
5551
5552 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01005553 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005554 if (err)
5555 return err;
5556
Joe Stringerfd978bf72018-10-02 13:35:35 -07005557 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
5558 * gen_ld_abs() may terminate the program at runtime, leading to
5559 * reference leak.
5560 */
5561 err = check_reference_leak(env);
5562 if (err) {
5563 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
5564 return err;
5565 }
5566
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08005567 if (env->cur_state->active_spin_lock) {
5568 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
5569 return -EINVAL;
5570 }
5571
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005572 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005573 verbose(env,
5574 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005575 return -EINVAL;
5576 }
5577
5578 if (mode == BPF_IND) {
5579 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01005580 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005581 if (err)
5582 return err;
5583 }
5584
5585 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01005586 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005587 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01005588 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
5589 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005590
5591 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01005592 * the value fetched from the packet.
5593 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005594 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005595 mark_reg_unknown(env, regs, BPF_REG_0);
Jiong Wang5327ed32019-05-24 23:25:12 +01005596 /* ld_abs load up to 32-bit skb data. */
5597 regs[BPF_REG_0].subreg_def = env->insn_idx + 1;
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08005598 return 0;
5599}
5600
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005601static int check_return_code(struct bpf_verifier_env *env)
5602{
brakmo5cf1e912019-05-28 16:59:36 -07005603 struct tnum enforce_attach_type_range = tnum_unknown;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005604 struct bpf_reg_state *reg;
5605 struct tnum range = tnum_range(0, 1);
5606
5607 switch (env->prog->type) {
5608 case BPF_PROG_TYPE_CGROUP_SKB:
brakmo5cf1e912019-05-28 16:59:36 -07005609 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) {
5610 range = tnum_range(0, 3);
5611 enforce_attach_type_range = tnum_range(2, 3);
5612 }
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005613 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07005614 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005615 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05005616 case BPF_PROG_TYPE_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08005617 case BPF_PROG_TYPE_CGROUP_SYSCTL:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005618 break;
5619 default:
5620 return 0;
5621 }
5622
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005623 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005624 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005625 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005626 reg_type_str[reg->type]);
5627 return -EINVAL;
5628 }
5629
5630 if (!tnum_in(range, reg->var_off)) {
brakmo5cf1e912019-05-28 16:59:36 -07005631 char tn_buf[48];
5632
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005633 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005634 if (!tnum_is_unknown(reg->var_off)) {
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005635 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005636 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005637 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005638 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005639 }
brakmo5cf1e912019-05-28 16:59:36 -07005640 tnum_strn(tn_buf, sizeof(tn_buf), range);
5641 verbose(env, " should have been %s\n", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005642 return -EINVAL;
5643 }
brakmo5cf1e912019-05-28 16:59:36 -07005644
5645 if (!tnum_is_unknown(enforce_attach_type_range) &&
5646 tnum_in(enforce_attach_type_range, reg->var_off))
5647 env->prog->enforce_expected_attach_type = 1;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07005648 return 0;
5649}
5650
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005651/* non-recursive DFS pseudo code
5652 * 1 procedure DFS-iterative(G,v):
5653 * 2 label v as discovered
5654 * 3 let S be a stack
5655 * 4 S.push(v)
5656 * 5 while S is not empty
5657 * 6 t <- S.pop()
5658 * 7 if t is what we're looking for:
5659 * 8 return t
5660 * 9 for all edges e in G.adjacentEdges(t) do
5661 * 10 if edge e is already labelled
5662 * 11 continue with the next edge
5663 * 12 w <- G.adjacentVertex(t,e)
5664 * 13 if vertex w is not discovered and not explored
5665 * 14 label e as tree-edge
5666 * 15 label w as discovered
5667 * 16 S.push(w)
5668 * 17 continue at 5
5669 * 18 else if vertex w is discovered
5670 * 19 label e as back-edge
5671 * 20 else
5672 * 21 // vertex w is explored
5673 * 22 label e as forward- or cross-edge
5674 * 23 label t as explored
5675 * 24 S.pop()
5676 *
5677 * convention:
5678 * 0x10 - discovered
5679 * 0x11 - discovered and fall-through edge labelled
5680 * 0x12 - discovered and fall-through and branch edges labelled
5681 * 0x20 - explored
5682 */
5683
5684enum {
5685 DISCOVERED = 0x10,
5686 EXPLORED = 0x20,
5687 FALLTHROUGH = 1,
5688 BRANCH = 2,
5689};
5690
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07005691static u32 state_htab_size(struct bpf_verifier_env *env)
5692{
5693 return env->prog->len;
5694}
5695
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005696static struct bpf_verifier_state_list **explored_state(
5697 struct bpf_verifier_env *env,
5698 int idx)
5699{
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07005700 struct bpf_verifier_state *cur = env->cur_state;
5701 struct bpf_func_state *state = cur->frame[cur->curframe];
5702
5703 return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)];
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005704}
5705
5706static void init_explored_state(struct bpf_verifier_env *env, int idx)
5707{
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07005708 env->insn_aux_data[idx].prune_point = true;
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005709}
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005710
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005711/* t, w, e - match pseudo-code above:
5712 * t - index of current instruction
5713 * w - next instruction
5714 * e - edge
5715 */
Alexei Starovoitov25897262019-06-15 12:12:20 -07005716static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
5717 bool loop_ok)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005718{
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005719 int *insn_stack = env->cfg.insn_stack;
5720 int *insn_state = env->cfg.insn_state;
5721
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005722 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
5723 return 0;
5724
5725 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
5726 return 0;
5727
5728 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005729 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005730 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005731 return -EINVAL;
5732 }
5733
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005734 if (e == BRANCH)
5735 /* mark branch target for state pruning */
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005736 init_explored_state(env, w);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005737
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005738 if (insn_state[w] == 0) {
5739 /* tree-edge */
5740 insn_state[t] = DISCOVERED | e;
5741 insn_state[w] = DISCOVERED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005742 if (env->cfg.cur_stack >= env->prog->len)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005743 return -E2BIG;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005744 insn_stack[env->cfg.cur_stack++] = w;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005745 return 1;
5746 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07005747 if (loop_ok && env->allow_ptr_leaks)
5748 return 0;
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005749 verbose_linfo(env, t, "%d: ", t);
5750 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005751 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005752 return -EINVAL;
5753 } else if (insn_state[w] == EXPLORED) {
5754 /* forward- or cross-edge */
5755 insn_state[t] = DISCOVERED | e;
5756 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005757 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005758 return -EFAULT;
5759 }
5760 return 0;
5761}
5762
5763/* non-recursive depth-first-search to detect loops in BPF program
5764 * loop == back-edge in directed graph
5765 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005766static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005767{
5768 struct bpf_insn *insns = env->prog->insnsi;
5769 int insn_cnt = env->prog->len;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005770 int *insn_stack, *insn_state;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005771 int ret = 0;
5772 int i, t;
5773
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005774 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005775 if (!insn_state)
5776 return -ENOMEM;
5777
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005778 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005779 if (!insn_stack) {
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005780 kvfree(insn_state);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005781 return -ENOMEM;
5782 }
5783
5784 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5785 insn_stack[0] = 0; /* 0 is the first instruction */
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005786 env->cfg.cur_stack = 1;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005787
5788peek_stack:
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005789 if (env->cfg.cur_stack == 0)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005790 goto check_state;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005791 t = insn_stack[env->cfg.cur_stack - 1];
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005792
Jiong Wang092ed092019-01-26 12:26:01 -05005793 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5794 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005795 u8 opcode = BPF_OP(insns[t].code);
5796
5797 if (opcode == BPF_EXIT) {
5798 goto mark_explored;
5799 } else if (opcode == BPF_CALL) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07005800 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005801 if (ret == 1)
5802 goto peek_stack;
5803 else if (ret < 0)
5804 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02005805 if (t + 1 < insn_cnt)
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005806 init_explored_state(env, t + 1);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005807 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005808 init_explored_state(env, t);
Alexei Starovoitov25897262019-06-15 12:12:20 -07005809 ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
5810 env, false);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005811 if (ret == 1)
5812 goto peek_stack;
5813 else if (ret < 0)
5814 goto err_free;
5815 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005816 } else if (opcode == BPF_JA) {
5817 if (BPF_SRC(insns[t].code) != BPF_K) {
5818 ret = -EINVAL;
5819 goto err_free;
5820 }
5821 /* unconditional jump with single edge */
5822 ret = push_insn(t, t + insns[t].off + 1,
Alexei Starovoitov25897262019-06-15 12:12:20 -07005823 FALLTHROUGH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005824 if (ret == 1)
5825 goto peek_stack;
5826 else if (ret < 0)
5827 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005828 /* tell verifier to check for equivalent states
5829 * after every call and jump
5830 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07005831 if (t + 1 < insn_cnt)
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005832 init_explored_state(env, t + 1);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005833 } else {
5834 /* conditional jump with two edges */
Alexei Starovoitov5d839022019-05-21 20:17:05 -07005835 init_explored_state(env, t);
Alexei Starovoitov25897262019-06-15 12:12:20 -07005836 ret = push_insn(t, t + 1, FALLTHROUGH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005837 if (ret == 1)
5838 goto peek_stack;
5839 else if (ret < 0)
5840 goto err_free;
5841
Alexei Starovoitov25897262019-06-15 12:12:20 -07005842 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005843 if (ret == 1)
5844 goto peek_stack;
5845 else if (ret < 0)
5846 goto err_free;
5847 }
5848 } else {
5849 /* all other non-branch instructions with single
5850 * fall-through edge
5851 */
Alexei Starovoitov25897262019-06-15 12:12:20 -07005852 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005853 if (ret == 1)
5854 goto peek_stack;
5855 else if (ret < 0)
5856 goto err_free;
5857 }
5858
5859mark_explored:
5860 insn_state[t] = EXPLORED;
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005861 if (env->cfg.cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005862 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005863 ret = -EFAULT;
5864 goto err_free;
5865 }
5866 goto peek_stack;
5867
5868check_state:
5869 for (i = 0; i < insn_cnt; i++) {
5870 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005871 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005872 ret = -EINVAL;
5873 goto err_free;
5874 }
5875 }
5876 ret = 0; /* cfg looks good */
5877
5878err_free:
Alexei Starovoitov71dde682019-04-01 21:27:43 -07005879 kvfree(insn_state);
5880 kvfree(insn_stack);
Alexei Starovoitov7df737e2019-04-19 07:44:54 -07005881 env->cfg.insn_state = env->cfg.insn_stack = NULL;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005882 return ret;
5883}
5884
Yonghong Song838e9692018-11-19 15:29:11 -08005885/* The minimum supported BTF func info size */
5886#define MIN_BPF_FUNCINFO_SIZE 8
5887#define MAX_FUNCINFO_REC_SIZE 252
5888
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005889static int check_btf_func(struct bpf_verifier_env *env,
5890 const union bpf_attr *attr,
5891 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08005892{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005893 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08005894 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005895 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08005896 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005897 struct bpf_prog *prog;
5898 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005899 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005900 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08005901 int ret = 0;
5902
5903 nfuncs = attr->func_info_cnt;
5904 if (!nfuncs)
5905 return 0;
5906
5907 if (nfuncs != env->subprog_cnt) {
5908 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5909 return -EINVAL;
5910 }
5911
5912 urec_size = attr->func_info_rec_size;
5913 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5914 urec_size > MAX_FUNCINFO_REC_SIZE ||
5915 urec_size % sizeof(u32)) {
5916 verbose(env, "invalid func info rec size %u\n", urec_size);
5917 return -EINVAL;
5918 }
5919
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005920 prog = env->prog;
5921 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005922
5923 urecord = u64_to_user_ptr(attr->func_info);
5924 min_size = min_t(u32, krec_size, urec_size);
5925
Yonghong Songba64e7d2018-11-24 23:20:44 -08005926 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005927 if (!krecord)
5928 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005929
Yonghong Song838e9692018-11-19 15:29:11 -08005930 for (i = 0; i < nfuncs; i++) {
5931 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5932 if (ret) {
5933 if (ret == -E2BIG) {
5934 verbose(env, "nonzero tailing record in func info");
5935 /* set the size kernel expects so loader can zero
5936 * out the rest of the record.
5937 */
5938 if (put_user(min_size, &uattr->func_info_rec_size))
5939 ret = -EFAULT;
5940 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005941 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005942 }
5943
Yonghong Songba64e7d2018-11-24 23:20:44 -08005944 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08005945 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005946 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005947 }
5948
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005949 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08005950 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005951 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005952 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005953 "nonzero insn_off %u for the first func info record",
5954 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08005955 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005956 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005957 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005958 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08005959 verbose(env,
5960 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005961 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08005962 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005963 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005964 }
5965
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005966 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005967 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5968 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005969 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005970 }
5971
5972 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08005973 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005974 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5975 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08005976 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005977 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005978 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005979 }
5980
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005981 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08005982 urecord += urec_size;
5983 }
5984
Yonghong Songba64e7d2018-11-24 23:20:44 -08005985 prog->aux->func_info = krecord;
5986 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08005987 return 0;
5988
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005989err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08005990 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08005991 return ret;
5992}
5993
Yonghong Songba64e7d2018-11-24 23:20:44 -08005994static void adjust_btf_func(struct bpf_verifier_env *env)
5995{
5996 int i;
5997
5998 if (!env->prog->aux->func_info)
5999 return;
6000
6001 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08006002 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08006003}
6004
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006005#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
6006 sizeof(((struct bpf_line_info *)(0))->line_col))
6007#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
6008
6009static int check_btf_line(struct bpf_verifier_env *env,
6010 const union bpf_attr *attr,
6011 union bpf_attr __user *uattr)
6012{
6013 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
6014 struct bpf_subprog_info *sub;
6015 struct bpf_line_info *linfo;
6016 struct bpf_prog *prog;
6017 const struct btf *btf;
6018 void __user *ulinfo;
6019 int err;
6020
6021 nr_linfo = attr->line_info_cnt;
6022 if (!nr_linfo)
6023 return 0;
6024
6025 rec_size = attr->line_info_rec_size;
6026 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
6027 rec_size > MAX_LINEINFO_REC_SIZE ||
6028 rec_size & (sizeof(u32) - 1))
6029 return -EINVAL;
6030
6031 /* Need to zero it in case the userspace may
6032 * pass in a smaller bpf_line_info object.
6033 */
6034 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
6035 GFP_KERNEL | __GFP_NOWARN);
6036 if (!linfo)
6037 return -ENOMEM;
6038
6039 prog = env->prog;
6040 btf = prog->aux->btf;
6041
6042 s = 0;
6043 sub = env->subprog_info;
6044 ulinfo = u64_to_user_ptr(attr->line_info);
6045 expected_size = sizeof(struct bpf_line_info);
6046 ncopy = min_t(u32, expected_size, rec_size);
6047 for (i = 0; i < nr_linfo; i++) {
6048 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
6049 if (err) {
6050 if (err == -E2BIG) {
6051 verbose(env, "nonzero tailing record in line_info");
6052 if (put_user(expected_size,
6053 &uattr->line_info_rec_size))
6054 err = -EFAULT;
6055 }
6056 goto err_free;
6057 }
6058
6059 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
6060 err = -EFAULT;
6061 goto err_free;
6062 }
6063
6064 /*
6065 * Check insn_off to ensure
6066 * 1) strictly increasing AND
6067 * 2) bounded by prog->len
6068 *
6069 * The linfo[0].insn_off == 0 check logically falls into
6070 * the later "missing bpf_line_info for func..." case
6071 * because the first linfo[0].insn_off must be the
6072 * first sub also and the first sub must have
6073 * subprog_info[0].start == 0.
6074 */
6075 if ((i && linfo[i].insn_off <= prev_offset) ||
6076 linfo[i].insn_off >= prog->len) {
6077 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
6078 i, linfo[i].insn_off, prev_offset,
6079 prog->len);
6080 err = -EINVAL;
6081 goto err_free;
6082 }
6083
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08006084 if (!prog->insnsi[linfo[i].insn_off].code) {
6085 verbose(env,
6086 "Invalid insn code at line_info[%u].insn_off\n",
6087 i);
6088 err = -EINVAL;
6089 goto err_free;
6090 }
6091
Martin KaFai Lau23127b32018-12-13 10:41:46 -08006092 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
6093 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08006094 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
6095 err = -EINVAL;
6096 goto err_free;
6097 }
6098
6099 if (s != env->subprog_cnt) {
6100 if (linfo[i].insn_off == sub[s].start) {
6101 sub[s].linfo_idx = i;
6102 s++;
6103 } else if (sub[s].start < linfo[i].insn_off) {
6104 verbose(env, "missing bpf_line_info for func#%u\n", s);
6105 err = -EINVAL;
6106 goto err_free;
6107 }
6108 }
6109
6110 prev_offset = linfo[i].insn_off;
6111 ulinfo += rec_size;
6112 }
6113
6114 if (s != env->subprog_cnt) {
6115 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
6116 env->subprog_cnt - s, s);
6117 err = -EINVAL;
6118 goto err_free;
6119 }
6120
6121 prog->aux->linfo = linfo;
6122 prog->aux->nr_linfo = nr_linfo;
6123
6124 return 0;
6125
6126err_free:
6127 kvfree(linfo);
6128 return err;
6129}
6130
6131static int check_btf_info(struct bpf_verifier_env *env,
6132 const union bpf_attr *attr,
6133 union bpf_attr __user *uattr)
6134{
6135 struct btf *btf;
6136 int err;
6137
6138 if (!attr->func_info_cnt && !attr->line_info_cnt)
6139 return 0;
6140
6141 btf = btf_get_by_fd(attr->prog_btf_fd);
6142 if (IS_ERR(btf))
6143 return PTR_ERR(btf);
6144 env->prog->aux->btf = btf;
6145
6146 err = check_btf_func(env, attr, uattr);
6147 if (err)
6148 return err;
6149
6150 err = check_btf_line(env, attr, uattr);
6151 if (err)
6152 return err;
6153
6154 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006155}
6156
Edward Creef1174f72017-08-07 15:26:19 +01006157/* check %cur's range satisfies %old's */
6158static bool range_within(struct bpf_reg_state *old,
6159 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006160{
Edward Creeb03c9f92017-08-07 15:26:36 +01006161 return old->umin_value <= cur->umin_value &&
6162 old->umax_value >= cur->umax_value &&
6163 old->smin_value <= cur->smin_value &&
6164 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01006165}
6166
6167/* Maximum number of register states that can exist at once */
6168#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
6169struct idpair {
6170 u32 old;
6171 u32 cur;
6172};
6173
6174/* If in the old state two registers had the same id, then they need to have
6175 * the same id in the new state as well. But that id could be different from
6176 * the old state, so we need to track the mapping from old to new ids.
6177 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
6178 * regs with old id 5 must also have new id 9 for the new state to be safe. But
6179 * regs with a different old id could still have new id 9, we don't care about
6180 * that.
6181 * So we look through our idmap to see if this old id has been seen before. If
6182 * so, we require the new id to match; otherwise, we add the id pair to the map.
6183 */
6184static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
6185{
6186 unsigned int i;
6187
6188 for (i = 0; i < ID_MAP_SIZE; i++) {
6189 if (!idmap[i].old) {
6190 /* Reached an empty slot; haven't seen this id before */
6191 idmap[i].old = old_id;
6192 idmap[i].cur = cur_id;
6193 return true;
6194 }
6195 if (idmap[i].old == old_id)
6196 return idmap[i].cur == cur_id;
6197 }
6198 /* We ran out of idmap slots, which should be impossible */
6199 WARN_ON_ONCE(1);
6200 return false;
6201}
6202
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006203static void clean_func_state(struct bpf_verifier_env *env,
6204 struct bpf_func_state *st)
6205{
6206 enum bpf_reg_liveness live;
6207 int i, j;
6208
6209 for (i = 0; i < BPF_REG_FP; i++) {
6210 live = st->regs[i].live;
6211 /* liveness must not touch this register anymore */
6212 st->regs[i].live |= REG_LIVE_DONE;
6213 if (!(live & REG_LIVE_READ))
6214 /* since the register is unused, clear its state
6215 * to make further comparison simpler
6216 */
6217 __mark_reg_not_init(&st->regs[i]);
6218 }
6219
6220 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
6221 live = st->stack[i].spilled_ptr.live;
6222 /* liveness must not touch this stack slot anymore */
6223 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
6224 if (!(live & REG_LIVE_READ)) {
6225 __mark_reg_not_init(&st->stack[i].spilled_ptr);
6226 for (j = 0; j < BPF_REG_SIZE; j++)
6227 st->stack[i].slot_type[j] = STACK_INVALID;
6228 }
6229 }
6230}
6231
6232static void clean_verifier_state(struct bpf_verifier_env *env,
6233 struct bpf_verifier_state *st)
6234{
6235 int i;
6236
6237 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
6238 /* all regs in this state in all frames were already marked */
6239 return;
6240
6241 for (i = 0; i <= st->curframe; i++)
6242 clean_func_state(env, st->frame[i]);
6243}
6244
6245/* the parentage chains form a tree.
6246 * the verifier states are added to state lists at given insn and
6247 * pushed into state stack for future exploration.
6248 * when the verifier reaches bpf_exit insn some of the verifer states
6249 * stored in the state lists have their final liveness state already,
6250 * but a lot of states will get revised from liveness point of view when
6251 * the verifier explores other branches.
6252 * Example:
6253 * 1: r0 = 1
6254 * 2: if r1 == 100 goto pc+1
6255 * 3: r0 = 2
6256 * 4: exit
6257 * when the verifier reaches exit insn the register r0 in the state list of
6258 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
6259 * of insn 2 and goes exploring further. At the insn 4 it will walk the
6260 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
6261 *
6262 * Since the verifier pushes the branch states as it sees them while exploring
6263 * the program the condition of walking the branch instruction for the second
6264 * time means that all states below this branch were already explored and
6265 * their final liveness markes are already propagated.
6266 * Hence when the verifier completes the search of state list in is_state_visited()
6267 * we can call this clean_live_states() function to mark all liveness states
6268 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
6269 * will not be used.
6270 * This function also clears the registers and stack for states that !READ
6271 * to simplify state merging.
6272 *
6273 * Important note here that walking the same branch instruction in the callee
6274 * doesn't meant that the states are DONE. The verifier has to compare
6275 * the callsites
6276 */
6277static void clean_live_states(struct bpf_verifier_env *env, int insn,
6278 struct bpf_verifier_state *cur)
6279{
6280 struct bpf_verifier_state_list *sl;
6281 int i;
6282
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006283 sl = *explored_state(env, insn);
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006284 while (sl) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07006285 if (sl->state.branches)
6286 goto next;
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006287 if (sl->state.insn_idx != insn ||
6288 sl->state.curframe != cur->curframe)
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006289 goto next;
6290 for (i = 0; i <= cur->curframe; i++)
6291 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
6292 goto next;
6293 clean_verifier_state(env, &sl->state);
6294next:
6295 sl = sl->next;
6296 }
6297}
6298
Edward Creef1174f72017-08-07 15:26:19 +01006299/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01006300static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
6301 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01006302{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006303 bool equal;
6304
Edward Creedc503a82017-08-15 20:34:35 +01006305 if (!(rold->live & REG_LIVE_READ))
6306 /* explored state didn't use this */
6307 return true;
6308
Edward Cree679c7822018-08-22 20:02:19 +01006309 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006310
6311 if (rold->type == PTR_TO_STACK)
6312 /* two stack pointers are equal only if they're pointing to
6313 * the same stack frame, since fp-8 in foo != fp-8 in bar
6314 */
6315 return equal && rold->frameno == rcur->frameno;
6316
6317 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01006318 return true;
6319
6320 if (rold->type == NOT_INIT)
6321 /* explored state can't have used this */
6322 return true;
6323 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006324 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006325 switch (rold->type) {
6326 case SCALAR_VALUE:
6327 if (rcur->type == SCALAR_VALUE) {
6328 /* new val must satisfy old val knowledge */
6329 return range_within(rold, rcur) &&
6330 tnum_in(rold->var_off, rcur->var_off);
6331 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08006332 /* We're trying to use a pointer in place of a scalar.
6333 * Even if the scalar was unbounded, this could lead to
6334 * pointer leaks because scalars are allowed to leak
6335 * while pointers are not. We could make this safe in
6336 * special cases if root is calling us, but it's
6337 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01006338 */
Jann Horn179d1c52017-12-18 20:11:59 -08006339 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006340 }
6341 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01006342 /* If the new min/max/var_off satisfy the old ones and
6343 * everything else matches, we are OK.
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006344 * 'id' is not compared, since it's only used for maps with
6345 * bpf_spin_lock inside map element and in such cases if
6346 * the rest of the prog is valid for one map element then
6347 * it's valid for all map elements regardless of the key
6348 * used in bpf_map_lookup()
Edward Cree1b688a12017-08-23 15:10:50 +01006349 */
6350 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6351 range_within(rold, rcur) &&
6352 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01006353 case PTR_TO_MAP_VALUE_OR_NULL:
6354 /* a PTR_TO_MAP_VALUE could be safe to use as a
6355 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
6356 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
6357 * checked, doing so could have affected others with the same
6358 * id, and we can't check for that because we lost the id when
6359 * we converted to a PTR_TO_MAP_VALUE.
6360 */
6361 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6362 return false;
6363 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6364 return false;
6365 /* Check our ids match any regs they're supposed to */
6366 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006367 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01006368 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02006369 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01006370 return false;
6371 /* We must have at least as much range as the old ptr
6372 * did, so that any accesses which were safe before are
6373 * still safe. This is true even if old range < old off,
6374 * since someone could have accessed through (ptr - k), or
6375 * even done ptr -= k in a register, to get a safe access.
6376 */
6377 if (rold->range > rcur->range)
6378 return false;
6379 /* If the offsets don't match, we can't trust our alignment;
6380 * nor can we be sure that we won't fall out of range.
6381 */
6382 if (rold->off != rcur->off)
6383 return false;
6384 /* id relations must be preserved */
6385 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6386 return false;
6387 /* new val must satisfy old val knowledge */
6388 return range_within(rold, rcur) &&
6389 tnum_in(rold->var_off, rcur->var_off);
6390 case PTR_TO_CTX:
6391 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01006392 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07006393 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07006394 case PTR_TO_SOCKET:
6395 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006396 case PTR_TO_SOCK_COMMON:
6397 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006398 case PTR_TO_TCP_SOCK:
6399 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07006400 case PTR_TO_XDP_SOCK:
Edward Creef1174f72017-08-07 15:26:19 +01006401 /* Only valid matches are exact, which memcmp() above
6402 * would have accepted
6403 */
6404 default:
6405 /* Don't know what's going on, just say it's not safe */
6406 return false;
6407 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006408
Edward Creef1174f72017-08-07 15:26:19 +01006409 /* Shouldn't get here; if we do, say it's not safe */
6410 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07006411 return false;
6412}
6413
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006414static bool stacksafe(struct bpf_func_state *old,
6415 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006416 struct idpair *idmap)
6417{
6418 int i, spi;
6419
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006420 /* walk slots of the explored stack and ignore any additional
6421 * slots in the current stack, since explored(safe) state
6422 * didn't use them
6423 */
6424 for (i = 0; i < old->allocated_stack; i++) {
6425 spi = i / BPF_REG_SIZE;
6426
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006427 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
6428 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006429 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00006430 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08006431 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006432
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006433 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
6434 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08006435
6436 /* explored stack has more populated slots than current stack
6437 * and these slots were used
6438 */
6439 if (i >= cur->allocated_stack)
6440 return false;
6441
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006442 /* if old state was safe with misc data in the stack
6443 * it will be safe with zero-initialized stack.
6444 * The opposite is not true
6445 */
6446 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
6447 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
6448 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006449 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
6450 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
6451 /* Ex: old explored (safe) state has STACK_SPILL in
6452 * this stack slot, but current has has STACK_MISC ->
6453 * this verifier states are not equivalent,
6454 * return false to continue verification of this path
6455 */
6456 return false;
6457 if (i % BPF_REG_SIZE)
6458 continue;
6459 if (old->stack[spi].slot_type[0] != STACK_SPILL)
6460 continue;
6461 if (!regsafe(&old->stack[spi].spilled_ptr,
6462 &cur->stack[spi].spilled_ptr,
6463 idmap))
6464 /* when explored and current stack slot are both storing
6465 * spilled registers, check that stored pointers types
6466 * are the same as well.
6467 * Ex: explored safe path could have stored
6468 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
6469 * but current path has stored:
6470 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
6471 * such verifier states are not equivalent.
6472 * return false to continue verification of this path
6473 */
6474 return false;
6475 }
6476 return true;
6477}
6478
Joe Stringerfd978bf72018-10-02 13:35:35 -07006479static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6480{
6481 if (old->acquired_refs != cur->acquired_refs)
6482 return false;
6483 return !memcmp(old->refs, cur->refs,
6484 sizeof(*old->refs) * old->acquired_refs);
6485}
6486
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006487/* compare two verifier states
6488 *
6489 * all states stored in state_list are known to be valid, since
6490 * verifier reached 'bpf_exit' instruction through them
6491 *
6492 * this function is called when verifier exploring different branches of
6493 * execution popped from the state stack. If it sees an old state that has
6494 * more strict register state and more strict stack state then this execution
6495 * branch doesn't need to be explored further, since verifier already
6496 * concluded that more strict state leads to valid finish.
6497 *
6498 * Therefore two states are equivalent if register state is more conservative
6499 * and explored stack state is more conservative than the current one.
6500 * Example:
6501 * explored current
6502 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
6503 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
6504 *
6505 * In other words if current stack state (one being explored) has more
6506 * valid slots than old one that already passed validation, it means
6507 * the verifier can stop exploring and conclude that current state is valid too
6508 *
6509 * Similarly with registers. If explored state has register type as invalid
6510 * whereas register type in current state is meaningful, it means that
6511 * the current state will reach 'bpf_exit' instruction safely
6512 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006513static bool func_states_equal(struct bpf_func_state *old,
6514 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006515{
Edward Creef1174f72017-08-07 15:26:19 +01006516 struct idpair *idmap;
6517 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006518 int i;
6519
Edward Creef1174f72017-08-07 15:26:19 +01006520 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
6521 /* If we failed to allocate the idmap, just say it's not safe */
6522 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07006523 return false;
Edward Creef1174f72017-08-07 15:26:19 +01006524
6525 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01006526 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01006527 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006528 }
6529
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006530 if (!stacksafe(old, cur, idmap))
6531 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006532
6533 if (!refsafe(old, cur))
6534 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01006535 ret = true;
6536out_free:
6537 kfree(idmap);
6538 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006539}
6540
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006541static bool states_equal(struct bpf_verifier_env *env,
6542 struct bpf_verifier_state *old,
6543 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01006544{
Edward Creedc503a82017-08-15 20:34:35 +01006545 int i;
6546
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006547 if (old->curframe != cur->curframe)
6548 return false;
6549
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006550 /* Verification state from speculative execution simulation
6551 * must never prune a non-speculative execution one.
6552 */
6553 if (old->speculative && !cur->speculative)
6554 return false;
6555
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08006556 if (old->active_spin_lock != cur->active_spin_lock)
6557 return false;
6558
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006559 /* for states to be equal callsites have to be the same
6560 * and all frame states need to be equivalent
6561 */
6562 for (i = 0; i <= old->curframe; i++) {
6563 if (old->frame[i]->callsite != cur->frame[i]->callsite)
6564 return false;
6565 if (!func_states_equal(old->frame[i], cur->frame[i]))
6566 return false;
6567 }
6568 return true;
6569}
6570
Jiong Wang5327ed32019-05-24 23:25:12 +01006571/* Return 0 if no propagation happened. Return negative error code if error
6572 * happened. Otherwise, return the propagated bit.
6573 */
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006574static int propagate_liveness_reg(struct bpf_verifier_env *env,
6575 struct bpf_reg_state *reg,
6576 struct bpf_reg_state *parent_reg)
6577{
Jiong Wang5327ed32019-05-24 23:25:12 +01006578 u8 parent_flag = parent_reg->live & REG_LIVE_READ;
6579 u8 flag = reg->live & REG_LIVE_READ;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006580 int err;
6581
Jiong Wang5327ed32019-05-24 23:25:12 +01006582 /* When comes here, read flags of PARENT_REG or REG could be any of
6583 * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need
6584 * of propagation if PARENT_REG has strongest REG_LIVE_READ64.
6585 */
6586 if (parent_flag == REG_LIVE_READ64 ||
6587 /* Or if there is no read flag from REG. */
6588 !flag ||
6589 /* Or if the read flag from REG is the same as PARENT_REG. */
6590 parent_flag == flag)
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006591 return 0;
6592
Jiong Wang5327ed32019-05-24 23:25:12 +01006593 err = mark_reg_read(env, reg, parent_reg, flag);
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006594 if (err)
6595 return err;
6596
Jiong Wang5327ed32019-05-24 23:25:12 +01006597 return flag;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006598}
6599
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006600/* A write screens off any subsequent reads; but write marks come from the
6601 * straight-line code between a state and its parent. When we arrive at an
6602 * equivalent state (jump target or such) we didn't arrive by the straight-line
6603 * code, so read marks in the state must propagate to the parent regardless
6604 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01006605 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006606 */
6607static int propagate_liveness(struct bpf_verifier_env *env,
6608 const struct bpf_verifier_state *vstate,
6609 struct bpf_verifier_state *vparent)
6610{
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006611 struct bpf_reg_state *state_reg, *parent_reg;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006612 struct bpf_func_state *state, *parent;
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006613 int i, frame, err = 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006614
6615 if (vparent->curframe != vstate->curframe) {
6616 WARN(1, "propagate_live: parent frame %d current frame %d\n",
6617 vparent->curframe, vstate->curframe);
6618 return -EFAULT;
6619 }
Edward Creedc503a82017-08-15 20:34:35 +01006620 /* Propagate read liveness of registers... */
6621 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
Jakub Kicinski83d16312019-03-21 14:34:36 -07006622 for (frame = 0; frame <= vstate->curframe; frame++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006623 parent = vparent->frame[frame];
6624 state = vstate->frame[frame];
6625 parent_reg = parent->regs;
6626 state_reg = state->regs;
Jakub Kicinski83d16312019-03-21 14:34:36 -07006627 /* We don't need to worry about FP liveness, it's read-only */
6628 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006629 err = propagate_liveness_reg(env, &state_reg[i],
6630 &parent_reg[i]);
Jiong Wang5327ed32019-05-24 23:25:12 +01006631 if (err < 0)
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006632 return err;
Jiong Wang5327ed32019-05-24 23:25:12 +01006633 if (err == REG_LIVE_READ64)
6634 mark_insn_zext(env, &parent_reg[i]);
Edward Creedc503a82017-08-15 20:34:35 +01006635 }
Edward Creedc503a82017-08-15 20:34:35 +01006636
Jiong Wang1b04aee2019-04-12 22:59:34 +01006637 /* Propagate stack slots. */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006638 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
6639 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006640 parent_reg = &parent->stack[i].spilled_ptr;
6641 state_reg = &state->stack[i].spilled_ptr;
Jiong Wang55e7f3b2019-04-12 22:59:36 +01006642 err = propagate_liveness_reg(env, state_reg,
6643 parent_reg);
Jiong Wang5327ed32019-05-24 23:25:12 +01006644 if (err < 0)
Jiong Wang3f8cafa2019-04-12 22:59:35 +01006645 return err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006646 }
Edward Creedc503a82017-08-15 20:34:35 +01006647 }
Jiong Wang5327ed32019-05-24 23:25:12 +01006648 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01006649}
6650
Alexei Starovoitov25897262019-06-15 12:12:20 -07006651static bool states_maybe_looping(struct bpf_verifier_state *old,
6652 struct bpf_verifier_state *cur)
6653{
6654 struct bpf_func_state *fold, *fcur;
6655 int i, fr = cur->curframe;
6656
6657 if (old->curframe != fr)
6658 return false;
6659
6660 fold = old->frame[fr];
6661 fcur = cur->frame[fr];
6662 for (i = 0; i < MAX_BPF_REG; i++)
6663 if (memcmp(&fold->regs[i], &fcur->regs[i],
6664 offsetof(struct bpf_reg_state, parent)))
6665 return false;
6666 return true;
6667}
6668
6669
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006670static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006671{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006672 struct bpf_verifier_state_list *new_sl;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006673 struct bpf_verifier_state_list *sl, **pprev;
Edward Cree679c7822018-08-22 20:02:19 +01006674 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006675 int i, j, err, states_cnt = 0;
Alexei Starovoitov25897262019-06-15 12:12:20 -07006676 bool add_new_state = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006677
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006678 if (!env->insn_aux_data[insn_idx].prune_point)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006679 /* this 'insn_idx' instruction wasn't marked, so we will not
6680 * be doing state search here
6681 */
6682 return 0;
6683
Alexei Starovoitov25897262019-06-15 12:12:20 -07006684 /* bpf progs typically have pruning point every 4 instructions
6685 * http://vger.kernel.org/bpfconf2019.html#session-1
6686 * Do not add new state for future pruning if the verifier hasn't seen
6687 * at least 2 jumps and at least 8 instructions.
6688 * This heuristics helps decrease 'total_states' and 'peak_states' metric.
6689 * In tests that amounts to up to 50% reduction into total verifier
6690 * memory consumption and 20% verifier time speedup.
6691 */
6692 if (env->jmps_processed - env->prev_jmps_processed >= 2 &&
6693 env->insn_processed - env->prev_insn_processed >= 8)
6694 add_new_state = true;
6695
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006696 pprev = explored_state(env, insn_idx);
6697 sl = *pprev;
6698
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08006699 clean_live_states(env, insn_idx, cur);
6700
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07006701 while (sl) {
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006702 states_cnt++;
6703 if (sl->state.insn_idx != insn_idx)
6704 goto next;
Alexei Starovoitov25897262019-06-15 12:12:20 -07006705 if (sl->state.branches) {
6706 if (states_maybe_looping(&sl->state, cur) &&
6707 states_equal(env, &sl->state, cur)) {
6708 verbose_linfo(env, insn_idx, "; ");
6709 verbose(env, "infinite loop detected at insn %d\n", insn_idx);
6710 return -EINVAL;
6711 }
6712 /* if the verifier is processing a loop, avoid adding new state
6713 * too often, since different loop iterations have distinct
6714 * states and may not help future pruning.
6715 * This threshold shouldn't be too low to make sure that
6716 * a loop with large bound will be rejected quickly.
6717 * The most abusive loop will be:
6718 * r1 += 1
6719 * if r1 < 1000000 goto pc-2
6720 * 1M insn_procssed limit / 100 == 10k peak states.
6721 * This threshold shouldn't be too high either, since states
6722 * at the end of the loop are likely to be useful in pruning.
6723 */
6724 if (env->jmps_processed - env->prev_jmps_processed < 20 &&
6725 env->insn_processed - env->prev_insn_processed < 100)
6726 add_new_state = false;
6727 goto miss;
6728 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006729 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006730 sl->hit_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006731 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01006732 * prune the search.
6733 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006734 * If we have any write marks in env->cur_state, they
6735 * will prevent corresponding reads in the continuation
6736 * from reaching our parent (an explored_state). Our
6737 * own state will get the read marks recorded, but
6738 * they'll be immediately forgotten as we're pruning
6739 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006740 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006741 err = propagate_liveness(env, &sl->state, cur);
6742 if (err)
6743 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006744 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01006745 }
Alexei Starovoitov25897262019-06-15 12:12:20 -07006746miss:
6747 /* when new state is not going to be added do not increase miss count.
6748 * Otherwise several loop iterations will remove the state
6749 * recorded earlier. The goal of these heuristics is to have
6750 * states from some iterations of the loop (some in the beginning
6751 * and some at the end) to help pruning.
6752 */
6753 if (add_new_state)
6754 sl->miss_cnt++;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006755 /* heuristic to determine whether this state is beneficial
6756 * to keep checking from state equivalence point of view.
6757 * Higher numbers increase max_states_per_insn and verification time,
6758 * but do not meaningfully decrease insn_processed.
6759 */
6760 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
6761 /* the state is unlikely to be useful. Remove it to
6762 * speed up verification
6763 */
6764 *pprev = sl->next;
6765 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
Alexei Starovoitov25897262019-06-15 12:12:20 -07006766 u32 br = sl->state.branches;
6767
6768 WARN_ONCE(br,
6769 "BUG live_done but branches_to_explore %d\n",
6770 br);
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006771 free_verifier_state(&sl->state, false);
6772 kfree(sl);
6773 env->peak_states--;
6774 } else {
6775 /* cannot free this state, since parentage chain may
6776 * walk it later. Add it for free_list instead to
6777 * be freed at the end of verification
6778 */
6779 sl->next = env->free_list;
6780 env->free_list = sl;
6781 }
6782 sl = *pprev;
6783 continue;
6784 }
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006785next:
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07006786 pprev = &sl->next;
6787 sl = *pprev;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006788 }
6789
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006790 if (env->max_states_per_insn < states_cnt)
6791 env->max_states_per_insn = states_cnt;
6792
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08006793 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
6794 return 0;
6795
Alexei Starovoitov25897262019-06-15 12:12:20 -07006796 if (!add_new_state)
6797 return 0;
6798
6799 /* There were no equivalent states, remember the current one.
6800 * Technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006801 * but it will either reach outer most bpf_exit (which means it's safe)
Alexei Starovoitov25897262019-06-15 12:12:20 -07006802 * or it will be rejected. When there are no loops the verifier won't be
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006803 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
Alexei Starovoitov25897262019-06-15 12:12:20 -07006804 * again on the way to bpf_exit.
6805 * When looping the sl->state.branches will be > 0 and this state
6806 * will not be considered for equivalence until branches == 0.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006807 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006808 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006809 if (!new_sl)
6810 return -ENOMEM;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006811 env->total_states++;
6812 env->peak_states++;
Alexei Starovoitov25897262019-06-15 12:12:20 -07006813 env->prev_jmps_processed = env->jmps_processed;
6814 env->prev_insn_processed = env->insn_processed;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006815
6816 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01006817 new = &new_sl->state;
6818 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006819 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01006820 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07006821 kfree(new_sl);
6822 return err;
6823 }
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07006824 new->insn_idx = insn_idx;
Alexei Starovoitov25897262019-06-15 12:12:20 -07006825 WARN_ONCE(new->branches != 1,
6826 "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx);
6827 cur->parent = new;
Alexei Starovoitov5d839022019-05-21 20:17:05 -07006828 new_sl->next = *explored_state(env, insn_idx);
6829 *explored_state(env, insn_idx) = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08006830 /* connect new state to parentage chain. Current frame needs all
6831 * registers connected. Only r6 - r9 of the callers are alive (pushed
6832 * to the stack implicitly by JITs) so in callers' frames connect just
6833 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
6834 * the state of the call instruction (with WRITTEN set), and r0 comes
6835 * from callee with its full parentage chain, anyway.
6836 */
6837 for (j = 0; j <= cur->curframe; j++)
6838 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
6839 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01006840 /* clear write marks in current state: the writes we did are not writes
6841 * our child did, so they don't screen off its reads from us.
6842 * (There are no read marks in current state, because reads always mark
6843 * their parent and current state never has children yet. Only
6844 * explored_states can get read marks.)
6845 */
Edward Creedc503a82017-08-15 20:34:35 +01006846 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006847 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
6848
6849 /* all stack frames are accessible from callee, clear them all */
6850 for (j = 0; j <= cur->curframe; j++) {
6851 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01006852 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006853
Edward Cree679c7822018-08-22 20:02:19 +01006854 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08006855 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01006856 frame->stack[i].spilled_ptr.parent =
6857 &newframe->stack[i].spilled_ptr;
6858 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006859 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006860 return 0;
6861}
6862
Joe Stringerc64b7982018-10-02 13:35:33 -07006863/* Return true if it's OK to have the same insn return a different type. */
6864static bool reg_type_mismatch_ok(enum bpf_reg_type type)
6865{
6866 switch (type) {
6867 case PTR_TO_CTX:
6868 case PTR_TO_SOCKET:
6869 case PTR_TO_SOCKET_OR_NULL:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08006870 case PTR_TO_SOCK_COMMON:
6871 case PTR_TO_SOCK_COMMON_OR_NULL:
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08006872 case PTR_TO_TCP_SOCK:
6873 case PTR_TO_TCP_SOCK_OR_NULL:
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07006874 case PTR_TO_XDP_SOCK:
Joe Stringerc64b7982018-10-02 13:35:33 -07006875 return false;
6876 default:
6877 return true;
6878 }
6879}
6880
6881/* If an instruction was previously used with particular pointer types, then we
6882 * need to be careful to avoid cases such as the below, where it may be ok
6883 * for one branch accessing the pointer, but not ok for the other branch:
6884 *
6885 * R1 = sock_ptr
6886 * goto X;
6887 * ...
6888 * R1 = some_other_valid_ptr;
6889 * goto X;
6890 * ...
6891 * R2 = *(u32 *)(R1 + 0);
6892 */
6893static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
6894{
6895 return src != prev && (!reg_type_mismatch_ok(src) ||
6896 !reg_type_mismatch_ok(prev));
6897}
6898
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006899static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006900{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006901 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006902 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006903 struct bpf_reg_state *regs;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006904 int insn_cnt = env->prog->len;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006905 bool do_print_state = false;
6906
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006907 env->prev_linfo = NULL;
6908
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006909 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6910 if (!state)
6911 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006912 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006913 state->speculative = false;
Alexei Starovoitov25897262019-06-15 12:12:20 -07006914 state->branches = 1;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006915 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6916 if (!state->frame[0]) {
6917 kfree(state);
6918 return -ENOMEM;
6919 }
6920 env->cur_state = state;
6921 init_func_state(env, state->frame[0],
6922 BPF_MAIN_FUNC /* callsite */,
6923 0 /* frameno */,
6924 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006925
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006926 for (;;) {
6927 struct bpf_insn *insn;
6928 u8 class;
6929 int err;
6930
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006931 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006932 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006933 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006934 return -EFAULT;
6935 }
6936
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006937 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006938 class = BPF_CLASS(insn->code);
6939
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006940 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006941 verbose(env,
6942 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006943 env->insn_processed);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006944 return -E2BIG;
6945 }
6946
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006947 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006948 if (err < 0)
6949 return err;
6950 if (err == 1) {
6951 /* found equivalent state, can prune the search */
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006952 if (env->log.level & BPF_LOG_LEVEL) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006953 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006954 verbose(env, "\nfrom %d to %d%s: safe\n",
6955 env->prev_insn_idx, env->insn_idx,
6956 env->cur_state->speculative ?
6957 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006958 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006959 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006960 }
6961 goto process_bpf_exit;
6962 }
6963
Alexei Starovoitovc3494802018-12-03 22:46:04 -08006964 if (signal_pending(current))
6965 return -EAGAIN;
6966
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02006967 if (need_resched())
6968 cond_resched();
6969
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006970 if (env->log.level & BPF_LOG_LEVEL2 ||
6971 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
6972 if (env->log.level & BPF_LOG_LEVEL2)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006973 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07006974 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006975 verbose(env, "\nfrom %d to %d%s:",
6976 env->prev_insn_idx, env->insn_idx,
6977 env->cur_state->speculative ?
6978 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006979 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006980 do_print_state = false;
6981 }
6982
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07006983 if (env->log.level & BPF_LOG_LEVEL) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006984 const struct bpf_insn_cbs cbs = {
6985 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01006986 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01006987 };
6988
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006989 verbose_linfo(env, env->insn_idx, "; ");
6990 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01006991 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006992 }
6993
Jakub Kicinskicae19272017-12-27 18:39:05 -08006994 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006995 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6996 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08006997 if (err)
6998 return err;
6999 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01007000
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007001 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007002 env->insn_aux_data[env->insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07007003
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007004 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007005 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007006 if (err)
7007 return err;
7008
7009 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007010 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007011
7012 /* check for reserved fields is already done */
7013
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007014 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01007015 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007016 if (err)
7017 return err;
7018
Edward Creedc503a82017-08-15 20:34:35 +01007019 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007020 if (err)
7021 return err;
7022
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07007023 src_reg_type = regs[insn->src_reg].type;
7024
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007025 /* check that memory (src_reg + off) is readable,
7026 * the state of dst_reg will be updated by this func
7027 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007028 err = check_mem_access(env, env->insn_idx, insn->src_reg,
7029 insn->off, BPF_SIZE(insn->code),
7030 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007031 if (err)
7032 return err;
7033
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007034 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007035
7036 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007037 /* saw a valid insn
7038 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007039 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007040 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007041 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007042
Joe Stringerc64b7982018-10-02 13:35:33 -07007043 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007044 /* ABuser program is trying to use the same insn
7045 * dst_reg = *(u32*) (src_reg + off)
7046 * with different pointer types:
7047 * src_reg == ctx in one branch and
7048 * src_reg == stack|map in some other branch.
7049 * Reject it.
7050 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007051 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007052 return -EINVAL;
7053 }
7054
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007055 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007056 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007057
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007058 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007059 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007060 if (err)
7061 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007062 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007063 continue;
7064 }
7065
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007066 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01007067 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007068 if (err)
7069 return err;
7070 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01007071 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007072 if (err)
7073 return err;
7074
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007075 dst_reg_type = regs[insn->dst_reg].type;
7076
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007077 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007078 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7079 insn->off, BPF_SIZE(insn->code),
7080 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007081 if (err)
7082 return err;
7083
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007084 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007085
7086 if (*prev_dst_type == NOT_INIT) {
7087 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07007088 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007089 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007090 return -EINVAL;
7091 }
7092
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007093 } else if (class == BPF_ST) {
7094 if (BPF_MODE(insn->code) != BPF_MEM ||
7095 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007096 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007097 return -EINVAL;
7098 }
7099 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01007100 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007101 if (err)
7102 return err;
7103
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01007104 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07007105 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02007106 insn->dst_reg,
7107 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01007108 return -EACCES;
7109 }
7110
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007111 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007112 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7113 insn->off, BPF_SIZE(insn->code),
7114 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007115 if (err)
7116 return err;
7117
Jiong Wang092ed092019-01-26 12:26:01 -05007118 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007119 u8 opcode = BPF_OP(insn->code);
7120
Alexei Starovoitov25897262019-06-15 12:12:20 -07007121 env->jmps_processed++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007122 if (opcode == BPF_CALL) {
7123 if (BPF_SRC(insn->code) != BPF_K ||
7124 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007125 (insn->src_reg != BPF_REG_0 &&
7126 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05007127 insn->dst_reg != BPF_REG_0 ||
7128 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007129 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007130 return -EINVAL;
7131 }
7132
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007133 if (env->cur_state->active_spin_lock &&
7134 (insn->src_reg == BPF_PSEUDO_CALL ||
7135 insn->imm != BPF_FUNC_spin_unlock)) {
7136 verbose(env, "function calls are not allowed while holding a lock\n");
7137 return -EINVAL;
7138 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007139 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007140 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007141 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007142 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007143 if (err)
7144 return err;
7145
7146 } else if (opcode == BPF_JA) {
7147 if (BPF_SRC(insn->code) != BPF_K ||
7148 insn->imm != 0 ||
7149 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05007150 insn->dst_reg != BPF_REG_0 ||
7151 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007152 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007153 return -EINVAL;
7154 }
7155
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007156 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007157 continue;
7158
7159 } else if (opcode == BPF_EXIT) {
7160 if (BPF_SRC(insn->code) != BPF_K ||
7161 insn->imm != 0 ||
7162 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05007163 insn->dst_reg != BPF_REG_0 ||
7164 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007165 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007166 return -EINVAL;
7167 }
7168
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007169 if (env->cur_state->active_spin_lock) {
7170 verbose(env, "bpf_spin_unlock is missing\n");
7171 return -EINVAL;
7172 }
7173
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007174 if (state->curframe) {
7175 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007176 env->prev_insn_idx = env->insn_idx;
7177 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08007178 if (err)
7179 return err;
7180 do_print_state = true;
7181 continue;
7182 }
7183
Joe Stringerfd978bf72018-10-02 13:35:35 -07007184 err = check_reference_leak(env);
7185 if (err)
7186 return err;
7187
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007188 /* eBPF calling convetion is such that R0 is used
7189 * to return the value from eBPF program.
7190 * Make sure that it's readable at this time
7191 * of bpf_exit, which means that program wrote
7192 * something into it earlier
7193 */
Edward Creedc503a82017-08-15 20:34:35 +01007194 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007195 if (err)
7196 return err;
7197
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007198 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007199 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07007200 return -EACCES;
7201 }
7202
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07007203 err = check_return_code(env);
7204 if (err)
7205 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007206process_bpf_exit:
Alexei Starovoitov25897262019-06-15 12:12:20 -07007207 update_branch_counts(env, env->cur_state);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007208 err = pop_stack(env, &env->prev_insn_idx,
7209 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007210 if (err < 0) {
7211 if (err != -ENOENT)
7212 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007213 break;
7214 } else {
7215 do_print_state = true;
7216 continue;
7217 }
7218 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007219 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007220 if (err)
7221 return err;
7222 }
7223 } else if (class == BPF_LD) {
7224 u8 mode = BPF_MODE(insn->code);
7225
7226 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08007227 err = check_ld_abs(env, insn);
7228 if (err)
7229 return err;
7230
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007231 } else if (mode == BPF_IMM) {
7232 err = check_ld_imm(env, insn);
7233 if (err)
7234 return err;
7235
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007236 env->insn_idx++;
7237 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007238 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007239 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007240 return -EINVAL;
7241 }
7242 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007243 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007244 return -EINVAL;
7245 }
7246
Daniel Borkmannc08435e2019-01-03 00:58:27 +01007247 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007248 }
7249
Jiong Wang9c8105b2018-05-02 16:17:18 -04007250 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007251 return 0;
7252}
7253
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007254static int check_map_prealloc(struct bpf_map *map)
7255{
7256 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07007257 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
7258 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007259 !(map->map_flags & BPF_F_NO_PREALLOC);
7260}
7261
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007262static bool is_tracing_prog_type(enum bpf_prog_type type)
7263{
7264 switch (type) {
7265 case BPF_PROG_TYPE_KPROBE:
7266 case BPF_PROG_TYPE_TRACEPOINT:
7267 case BPF_PROG_TYPE_PERF_EVENT:
7268 case BPF_PROG_TYPE_RAW_TRACEPOINT:
7269 return true;
7270 default:
7271 return false;
7272 }
7273}
7274
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007275static int check_map_prog_compatibility(struct bpf_verifier_env *env,
7276 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007277 struct bpf_prog *prog)
7278
7279{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007280 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
7281 * preallocated hash maps, since doing memory allocation
7282 * in overflow_handler can crash depending on where nmi got
7283 * triggered.
7284 */
7285 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
7286 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007287 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007288 return -EINVAL;
7289 }
7290 if (map->inner_map_meta &&
7291 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007292 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07007293 return -EINVAL;
7294 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007295 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08007296
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08007297 if ((is_tracing_prog_type(prog->type) ||
7298 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
7299 map_value_has_spin_lock(map)) {
7300 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
7301 return -EINVAL;
7302 }
7303
Jakub Kicinskia3884572018-01-11 20:29:09 -08007304 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07007305 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08007306 verbose(env, "offload device mismatch between prog and map\n");
7307 return -EINVAL;
7308 }
7309
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007310 return 0;
7311}
7312
Roman Gushchinb741f162018-09-28 14:45:43 +00007313static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
7314{
7315 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
7316 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
7317}
7318
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007319/* look for pseudo eBPF instructions that access map FDs and
7320 * replace them with actual map pointers
7321 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007322static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007323{
7324 struct bpf_insn *insn = env->prog->insnsi;
7325 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007326 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007327
Daniel Borkmannf1f77142017-01-13 23:38:15 +01007328 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01007329 if (err)
7330 return err;
7331
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007332 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007333 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007334 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007335 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007336 return -EINVAL;
7337 }
7338
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007339 if (BPF_CLASS(insn->code) == BPF_STX &&
7340 ((BPF_MODE(insn->code) != BPF_MEM &&
7341 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007342 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007343 return -EINVAL;
7344 }
7345
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007346 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007347 struct bpf_insn_aux_data *aux;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007348 struct bpf_map *map;
7349 struct fd f;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007350 u64 addr;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007351
7352 if (i == insn_cnt - 1 || insn[1].code != 0 ||
7353 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
7354 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007355 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007356 return -EINVAL;
7357 }
7358
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007359 if (insn[0].src_reg == 0)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007360 /* valid generic load 64-bit imm */
7361 goto next_insn;
7362
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007363 /* In final convert_pseudo_ld_imm64() step, this is
7364 * converted into regular 64-bit imm load insn.
7365 */
7366 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
7367 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
7368 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
7369 insn[1].imm != 0)) {
7370 verbose(env,
7371 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007372 return -EINVAL;
7373 }
7374
Daniel Borkmann20182392019-03-04 21:08:53 +01007375 f = fdget(insn[0].imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01007376 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007377 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007378 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Daniel Borkmann20182392019-03-04 21:08:53 +01007379 insn[0].imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007380 return PTR_ERR(map);
7381 }
7382
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007383 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07007384 if (err) {
7385 fdput(f);
7386 return err;
7387 }
7388
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007389 aux = &env->insn_aux_data[i];
7390 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
7391 addr = (unsigned long)map;
7392 } else {
7393 u32 off = insn[1].imm;
7394
7395 if (off >= BPF_MAX_VAR_OFF) {
7396 verbose(env, "direct value offset of %u is not allowed\n", off);
7397 fdput(f);
7398 return -EINVAL;
7399 }
7400
7401 if (!map->ops->map_direct_value_addr) {
7402 verbose(env, "no direct value access support for this map type\n");
7403 fdput(f);
7404 return -EINVAL;
7405 }
7406
7407 err = map->ops->map_direct_value_addr(map, &addr, off);
7408 if (err) {
7409 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
7410 map->value_size, off);
7411 fdput(f);
7412 return err;
7413 }
7414
7415 aux->map_off = off;
7416 addr += off;
7417 }
7418
7419 insn[0].imm = (u32)addr;
7420 insn[1].imm = addr >> 32;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007421
7422 /* check whether we recorded this map already */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007423 for (j = 0; j < env->used_map_cnt; j++) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007424 if (env->used_maps[j] == map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007425 aux->map_index = j;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007426 fdput(f);
7427 goto next_insn;
7428 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007429 }
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007430
7431 if (env->used_map_cnt >= MAX_USED_MAPS) {
7432 fdput(f);
7433 return -E2BIG;
7434 }
7435
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007436 /* hold the map. If the program is rejected by verifier,
7437 * the map will be released by release_maps() or it
7438 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07007439 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007440 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07007441 map = bpf_map_inc(map, false);
7442 if (IS_ERR(map)) {
7443 fdput(f);
7444 return PTR_ERR(map);
7445 }
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02007446
7447 aux->map_index = env->used_map_cnt;
Alexei Starovoitov92117d82016-04-27 18:56:20 -07007448 env->used_maps[env->used_map_cnt++] = map;
7449
Roman Gushchinb741f162018-09-28 14:45:43 +00007450 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007451 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00007452 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007453 fdput(f);
7454 return -EBUSY;
7455 }
7456
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007457 fdput(f);
7458next_insn:
7459 insn++;
7460 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01007461 continue;
7462 }
7463
7464 /* Basic sanity check before we invest more work here. */
7465 if (!bpf_opcode_in_insntable(insn->code)) {
7466 verbose(env, "unknown opcode %02x\n", insn->code);
7467 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007468 }
7469 }
7470
7471 /* now all pseudo BPF_LD_IMM64 instructions load valid
7472 * 'struct bpf_map *' into a register instead of user map_fd.
7473 * These pointers will be used later by verifier to validate map access.
7474 */
7475 return 0;
7476}
7477
7478/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007479static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007480{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007481 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007482 int i;
7483
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007484 for_each_cgroup_storage_type(stype) {
7485 if (!env->prog->aux->cgroup_storage[stype])
7486 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007487 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00007488 env->prog->aux->cgroup_storage[stype]);
7489 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07007490
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007491 for (i = 0; i < env->used_map_cnt; i++)
7492 bpf_map_put(env->used_maps[i]);
7493}
7494
7495/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007496static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007497{
7498 struct bpf_insn *insn = env->prog->insnsi;
7499 int insn_cnt = env->prog->len;
7500 int i;
7501
7502 for (i = 0; i < insn_cnt; i++, insn++)
7503 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
7504 insn->src_reg = 0;
7505}
7506
Alexei Starovoitov80419022017-03-15 18:26:41 -07007507/* single env->prog->insni[off] instruction was replaced with the range
7508 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
7509 * [0, off) and [off, end) to new locations, so the patched range stays zero
7510 */
Jiong Wangb325fbc2019-05-24 23:25:13 +01007511static int adjust_insn_aux_data(struct bpf_verifier_env *env,
7512 struct bpf_prog *new_prog, u32 off, u32 cnt)
Alexei Starovoitov80419022017-03-15 18:26:41 -07007513{
7514 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Jiong Wangb325fbc2019-05-24 23:25:13 +01007515 struct bpf_insn *insn = new_prog->insnsi;
7516 u32 prog_len;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007517 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007518
Jiong Wangb325fbc2019-05-24 23:25:13 +01007519 /* aux info at OFF always needs adjustment, no matter fast path
7520 * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
7521 * original insn at old prog.
7522 */
7523 old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1);
7524
Alexei Starovoitov80419022017-03-15 18:26:41 -07007525 if (cnt == 1)
7526 return 0;
Jiong Wangb325fbc2019-05-24 23:25:13 +01007527 prog_len = new_prog->len;
Kees Cookfad953c2018-06-12 14:27:37 -07007528 new_data = vzalloc(array_size(prog_len,
7529 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07007530 if (!new_data)
7531 return -ENOMEM;
7532 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
7533 memcpy(new_data + off + cnt - 1, old_data + off,
7534 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Jiong Wangb325fbc2019-05-24 23:25:13 +01007535 for (i = off; i < off + cnt - 1; i++) {
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007536 new_data[i].seen = true;
Jiong Wangb325fbc2019-05-24 23:25:13 +01007537 new_data[i].zext_dst = insn_has_def32(env, insn + i);
7538 }
Alexei Starovoitov80419022017-03-15 18:26:41 -07007539 env->insn_aux_data = new_data;
7540 vfree(old_data);
7541 return 0;
7542}
7543
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007544static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
7545{
7546 int i;
7547
7548 if (len == 1)
7549 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007550 /* NOTE: fake 'exit' subprog should be updated as well. */
7551 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00007552 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007553 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04007554 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007555 }
7556}
7557
Alexei Starovoitov80419022017-03-15 18:26:41 -07007558static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
7559 const struct bpf_insn *patch, u32 len)
7560{
7561 struct bpf_prog *new_prog;
7562
7563 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007564 if (IS_ERR(new_prog)) {
7565 if (PTR_ERR(new_prog) == -ERANGE)
7566 verbose(env,
7567 "insn %d cannot be patched due to 16-bit range\n",
7568 env->insn_aux_data[off].orig_idx);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007569 return NULL;
Alexei Starovoitov4f733792019-04-01 21:27:44 -07007570 }
Jiong Wangb325fbc2019-05-24 23:25:13 +01007571 if (adjust_insn_aux_data(env, new_prog, off, len))
Alexei Starovoitov80419022017-03-15 18:26:41 -07007572 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007573 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07007574 return new_prog;
7575}
7576
Jakub Kicinski52875a02019-01-22 22:45:20 -08007577static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
7578 u32 off, u32 cnt)
7579{
7580 int i, j;
7581
7582 /* find first prog starting at or after off (first to remove) */
7583 for (i = 0; i < env->subprog_cnt; i++)
7584 if (env->subprog_info[i].start >= off)
7585 break;
7586 /* find first prog starting at or after off + cnt (first to stay) */
7587 for (j = i; j < env->subprog_cnt; j++)
7588 if (env->subprog_info[j].start >= off + cnt)
7589 break;
7590 /* if j doesn't start exactly at off + cnt, we are just removing
7591 * the front of previous prog
7592 */
7593 if (env->subprog_info[j].start != off + cnt)
7594 j--;
7595
7596 if (j > i) {
7597 struct bpf_prog_aux *aux = env->prog->aux;
7598 int move;
7599
7600 /* move fake 'exit' subprog as well */
7601 move = env->subprog_cnt + 1 - j;
7602
7603 memmove(env->subprog_info + i,
7604 env->subprog_info + j,
7605 sizeof(*env->subprog_info) * move);
7606 env->subprog_cnt -= j - i;
7607
7608 /* remove func_info */
7609 if (aux->func_info) {
7610 move = aux->func_info_cnt - j;
7611
7612 memmove(aux->func_info + i,
7613 aux->func_info + j,
7614 sizeof(*aux->func_info) * move);
7615 aux->func_info_cnt -= j - i;
7616 /* func_info->insn_off is set after all code rewrites,
7617 * in adjust_btf_func() - no need to adjust
7618 */
7619 }
7620 } else {
7621 /* convert i from "first prog to remove" to "first to adjust" */
7622 if (env->subprog_info[i].start == off)
7623 i++;
7624 }
7625
7626 /* update fake 'exit' subprog as well */
7627 for (; i <= env->subprog_cnt; i++)
7628 env->subprog_info[i].start -= cnt;
7629
7630 return 0;
7631}
7632
7633static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
7634 u32 cnt)
7635{
7636 struct bpf_prog *prog = env->prog;
7637 u32 i, l_off, l_cnt, nr_linfo;
7638 struct bpf_line_info *linfo;
7639
7640 nr_linfo = prog->aux->nr_linfo;
7641 if (!nr_linfo)
7642 return 0;
7643
7644 linfo = prog->aux->linfo;
7645
7646 /* find first line info to remove, count lines to be removed */
7647 for (i = 0; i < nr_linfo; i++)
7648 if (linfo[i].insn_off >= off)
7649 break;
7650
7651 l_off = i;
7652 l_cnt = 0;
7653 for (; i < nr_linfo; i++)
7654 if (linfo[i].insn_off < off + cnt)
7655 l_cnt++;
7656 else
7657 break;
7658
7659 /* First live insn doesn't match first live linfo, it needs to "inherit"
7660 * last removed linfo. prog is already modified, so prog->len == off
7661 * means no live instructions after (tail of the program was removed).
7662 */
7663 if (prog->len != off && l_cnt &&
7664 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
7665 l_cnt--;
7666 linfo[--i].insn_off = off + cnt;
7667 }
7668
7669 /* remove the line info which refer to the removed instructions */
7670 if (l_cnt) {
7671 memmove(linfo + l_off, linfo + i,
7672 sizeof(*linfo) * (nr_linfo - i));
7673
7674 prog->aux->nr_linfo -= l_cnt;
7675 nr_linfo = prog->aux->nr_linfo;
7676 }
7677
7678 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
7679 for (i = l_off; i < nr_linfo; i++)
7680 linfo[i].insn_off -= cnt;
7681
7682 /* fix up all subprogs (incl. 'exit') which start >= off */
7683 for (i = 0; i <= env->subprog_cnt; i++)
7684 if (env->subprog_info[i].linfo_idx > l_off) {
7685 /* program may have started in the removed region but
7686 * may not be fully removed
7687 */
7688 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
7689 env->subprog_info[i].linfo_idx -= l_cnt;
7690 else
7691 env->subprog_info[i].linfo_idx = l_off;
7692 }
7693
7694 return 0;
7695}
7696
7697static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
7698{
7699 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7700 unsigned int orig_prog_len = env->prog->len;
7701 int err;
7702
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007703 if (bpf_prog_is_dev_bound(env->prog->aux))
7704 bpf_prog_offload_remove_insns(env, off, cnt);
7705
Jakub Kicinski52875a02019-01-22 22:45:20 -08007706 err = bpf_remove_insns(env->prog, off, cnt);
7707 if (err)
7708 return err;
7709
7710 err = adjust_subprog_starts_after_remove(env, off, cnt);
7711 if (err)
7712 return err;
7713
7714 err = bpf_adj_linfo_after_remove(env, off, cnt);
7715 if (err)
7716 return err;
7717
7718 memmove(aux_data + off, aux_data + off + cnt,
7719 sizeof(*aux_data) * (orig_prog_len - off - cnt));
7720
7721 return 0;
7722}
7723
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007724/* The verifier does more data flow analysis than llvm and will not
7725 * explore branches that are dead at run time. Malicious programs can
7726 * have dead code too. Therefore replace all dead at-run-time code
7727 * with 'ja -1'.
7728 *
7729 * Just nops are not optimal, e.g. if they would sit at the end of the
7730 * program and through another bug we would manage to jump there, then
7731 * we'd execute beyond program memory otherwise. Returning exception
7732 * code also wouldn't work since we can have subprogs where the dead
7733 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007734 */
7735static void sanitize_dead_code(struct bpf_verifier_env *env)
7736{
7737 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007738 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007739 struct bpf_insn *insn = env->prog->insnsi;
7740 const int insn_cnt = env->prog->len;
7741 int i;
7742
7743 for (i = 0; i < insn_cnt; i++) {
7744 if (aux_data[i].seen)
7745 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01007746 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08007747 }
7748}
7749
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007750static bool insn_is_cond_jump(u8 code)
7751{
7752 u8 op;
7753
Jiong Wang092ed092019-01-26 12:26:01 -05007754 if (BPF_CLASS(code) == BPF_JMP32)
7755 return true;
7756
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007757 if (BPF_CLASS(code) != BPF_JMP)
7758 return false;
7759
7760 op = BPF_OP(code);
7761 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
7762}
7763
7764static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
7765{
7766 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7767 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7768 struct bpf_insn *insn = env->prog->insnsi;
7769 const int insn_cnt = env->prog->len;
7770 int i;
7771
7772 for (i = 0; i < insn_cnt; i++, insn++) {
7773 if (!insn_is_cond_jump(insn->code))
7774 continue;
7775
7776 if (!aux_data[i + 1].seen)
7777 ja.off = insn->off;
7778 else if (!aux_data[i + 1 + insn->off].seen)
7779 ja.off = 0;
7780 else
7781 continue;
7782
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08007783 if (bpf_prog_is_dev_bound(env->prog->aux))
7784 bpf_prog_offload_replace_insn(env, i, &ja);
7785
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007786 memcpy(insn, &ja, sizeof(ja));
7787 }
7788}
7789
Jakub Kicinski52875a02019-01-22 22:45:20 -08007790static int opt_remove_dead_code(struct bpf_verifier_env *env)
7791{
7792 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7793 int insn_cnt = env->prog->len;
7794 int i, err;
7795
7796 for (i = 0; i < insn_cnt; i++) {
7797 int j;
7798
7799 j = 0;
7800 while (i + j < insn_cnt && !aux_data[i + j].seen)
7801 j++;
7802 if (!j)
7803 continue;
7804
7805 err = verifier_remove_insns(env, i, j);
7806 if (err)
7807 return err;
7808 insn_cnt = env->prog->len;
7809 }
7810
7811 return 0;
7812}
7813
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007814static int opt_remove_nops(struct bpf_verifier_env *env)
7815{
7816 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7817 struct bpf_insn *insn = env->prog->insnsi;
7818 int insn_cnt = env->prog->len;
7819 int i, err;
7820
7821 for (i = 0; i < insn_cnt; i++) {
7822 if (memcmp(&insn[i], &ja, sizeof(ja)))
7823 continue;
7824
7825 err = verifier_remove_insns(env, i, 1);
7826 if (err)
7827 return err;
7828 insn_cnt--;
7829 i--;
7830 }
7831
7832 return 0;
7833}
7834
Jiong Wangd6c23082019-05-24 23:25:18 +01007835static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
7836 const union bpf_attr *attr)
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007837{
Jiong Wangd6c23082019-05-24 23:25:18 +01007838 struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4];
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007839 struct bpf_insn_aux_data *aux = env->insn_aux_data;
Jiong Wangd6c23082019-05-24 23:25:18 +01007840 int i, patch_len, delta = 0, len = env->prog->len;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007841 struct bpf_insn *insns = env->prog->insnsi;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007842 struct bpf_prog *new_prog;
Jiong Wangd6c23082019-05-24 23:25:18 +01007843 bool rnd_hi32;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007844
Jiong Wangd6c23082019-05-24 23:25:18 +01007845 rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007846 zext_patch[1] = BPF_ZEXT_REG(0);
Jiong Wangd6c23082019-05-24 23:25:18 +01007847 rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
7848 rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
7849 rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007850 for (i = 0; i < len; i++) {
7851 int adj_idx = i + delta;
7852 struct bpf_insn insn;
7853
Jiong Wangd6c23082019-05-24 23:25:18 +01007854 insn = insns[adj_idx];
7855 if (!aux[adj_idx].zext_dst) {
7856 u8 code, class;
7857 u32 imm_rnd;
7858
7859 if (!rnd_hi32)
7860 continue;
7861
7862 code = insn.code;
7863 class = BPF_CLASS(code);
7864 if (insn_no_def(&insn))
7865 continue;
7866
7867 /* NOTE: arg "reg" (the fourth one) is only used for
7868 * BPF_STX which has been ruled out in above
7869 * check, it is safe to pass NULL here.
7870 */
7871 if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) {
7872 if (class == BPF_LD &&
7873 BPF_MODE(code) == BPF_IMM)
7874 i++;
7875 continue;
7876 }
7877
7878 /* ctx load could be transformed into wider load. */
7879 if (class == BPF_LDX &&
7880 aux[adj_idx].ptr_type == PTR_TO_CTX)
7881 continue;
7882
7883 imm_rnd = get_random_int();
7884 rnd_hi32_patch[0] = insn;
7885 rnd_hi32_patch[1].imm = imm_rnd;
7886 rnd_hi32_patch[3].dst_reg = insn.dst_reg;
7887 patch = rnd_hi32_patch;
7888 patch_len = 4;
7889 goto apply_patch_buffer;
7890 }
7891
7892 if (!bpf_jit_needs_zext())
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007893 continue;
7894
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007895 zext_patch[0] = insn;
7896 zext_patch[1].dst_reg = insn.dst_reg;
7897 zext_patch[1].src_reg = insn.dst_reg;
Jiong Wangd6c23082019-05-24 23:25:18 +01007898 patch = zext_patch;
7899 patch_len = 2;
7900apply_patch_buffer:
7901 new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007902 if (!new_prog)
7903 return -ENOMEM;
7904 env->prog = new_prog;
7905 insns = new_prog->insnsi;
7906 aux = env->insn_aux_data;
Jiong Wangd6c23082019-05-24 23:25:18 +01007907 delta += patch_len - 1;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01007908 }
7909
7910 return 0;
7911}
7912
Joe Stringerc64b7982018-10-02 13:35:33 -07007913/* convert load instructions that access fields of a context type into a
7914 * sequence of instructions that access fields of the underlying structure:
7915 * struct __sk_buff -> struct sk_buff
7916 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007917 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007918static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007919{
Jakub Kicinski00176a32017-10-16 16:40:54 -07007920 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007921 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007922 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007923 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007924 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007925 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007926 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02007927 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007928
Daniel Borkmannb09928b2018-10-24 22:05:49 +02007929 if (ops->gen_prologue || env->seen_direct_write) {
7930 if (!ops->gen_prologue) {
7931 verbose(env, "bpf verifier is misconfigured\n");
7932 return -EINVAL;
7933 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007934 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
7935 env->prog);
7936 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007937 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007938 return -EINVAL;
7939 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07007940 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007941 if (!new_prog)
7942 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07007943
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007944 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007945 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007946 }
7947 }
7948
Joe Stringerc64b7982018-10-02 13:35:33 -07007949 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007950 return 0;
7951
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007952 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02007953
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007954 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07007955 bpf_convert_ctx_access_t convert_ctx_access;
7956
Daniel Borkmann62c79892017-01-12 11:51:33 +01007957 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
7958 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
7959 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007960 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007961 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01007962 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
7963 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
7964 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07007965 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07007966 type = BPF_WRITE;
7967 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007968 continue;
7969
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07007970 if (type == BPF_WRITE &&
7971 env->insn_aux_data[i + delta].sanitize_stack_off) {
7972 struct bpf_insn patch[] = {
7973 /* Sanitize suspicious stack slot with zero.
7974 * There are no memory dependencies for this store,
7975 * since it's only using frame pointer and immediate
7976 * constant of zero
7977 */
7978 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
7979 env->insn_aux_data[i + delta].sanitize_stack_off,
7980 0),
7981 /* the original STX instruction will immediately
7982 * overwrite the same stack slot with appropriate value
7983 */
7984 *insn,
7985 };
7986
7987 cnt = ARRAY_SIZE(patch);
7988 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
7989 if (!new_prog)
7990 return -ENOMEM;
7991
7992 delta += cnt - 1;
7993 env->prog = new_prog;
7994 insn = new_prog->insnsi + i + delta;
7995 continue;
7996 }
7997
Joe Stringerc64b7982018-10-02 13:35:33 -07007998 switch (env->insn_aux_data[i + delta].ptr_type) {
7999 case PTR_TO_CTX:
8000 if (!ops->convert_ctx_access)
8001 continue;
8002 convert_ctx_access = ops->convert_ctx_access;
8003 break;
8004 case PTR_TO_SOCKET:
Martin KaFai Lau46f8bc92019-02-09 23:22:20 -08008005 case PTR_TO_SOCK_COMMON:
Joe Stringerc64b7982018-10-02 13:35:33 -07008006 convert_ctx_access = bpf_sock_convert_ctx_access;
8007 break;
Martin KaFai Lau655a51e2019-02-09 23:22:24 -08008008 case PTR_TO_TCP_SOCK:
8009 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
8010 break;
Jonathan Lemonfada7fd2019-06-06 13:59:40 -07008011 case PTR_TO_XDP_SOCK:
8012 convert_ctx_access = bpf_xdp_sock_convert_ctx_access;
8013 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07008014 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008015 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07008016 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008017
Yonghong Song31fd8582017-06-13 15:52:13 -07008018 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008019 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07008020
8021 /* If the read access is a narrower load of the field,
8022 * convert to a 4/8-byte load, to minimum program type specific
8023 * convert_ctx_access changes. If conversion is successful,
8024 * we will apply proper mask to the result.
8025 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02008026 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008027 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
8028 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07008029 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02008030 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07008031
Daniel Borkmannf96da092017-07-02 02:13:27 +02008032 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008033 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02008034 return -EINVAL;
8035 }
8036
8037 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07008038 if (ctx_field_size == 4)
8039 size_code = BPF_W;
8040 else if (ctx_field_size == 8)
8041 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02008042
Daniel Borkmannbc231052018-06-02 23:06:39 +02008043 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07008044 insn->code = BPF_LDX | BPF_MEM | size_code;
8045 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02008046
8047 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07008048 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
8049 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02008050 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
8051 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008052 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008053 return -EINVAL;
8054 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02008055
8056 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008057 u8 shift = (off & (size_default - 1)) * 8;
8058
8059 if (ctx_field_size <= 4) {
8060 if (shift)
8061 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
8062 insn->dst_reg,
8063 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07008064 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02008065 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008066 } else {
8067 if (shift)
8068 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
8069 insn->dst_reg,
8070 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07008071 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Krzesimir Nowake2f7fc02019-05-08 18:08:58 +02008072 (1ULL << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08008073 }
Yonghong Song31fd8582017-06-13 15:52:13 -07008074 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008075
Alexei Starovoitov80419022017-03-15 18:26:41 -07008076 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008077 if (!new_prog)
8078 return -ENOMEM;
8079
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008080 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008081
8082 /* keep walking new program and skip insns we just inserted */
8083 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008084 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008085 }
8086
8087 return 0;
8088}
8089
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008090static int jit_subprogs(struct bpf_verifier_env *env)
8091{
8092 struct bpf_prog *prog = env->prog, **func, *tmp;
8093 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01008094 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008095 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008096 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008097
Jiong Wangf910cef2018-05-02 16:17:17 -04008098 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008099 return 0;
8100
Daniel Borkmann7105e822017-12-20 13:42:57 +01008101 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008102 if (insn->code != (BPF_JMP | BPF_CALL) ||
8103 insn->src_reg != BPF_PSEUDO_CALL)
8104 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008105 /* Upon error here we cannot fall back to interpreter but
8106 * need a hard reject of the program. Thus -EFAULT is
8107 * propagated in any case.
8108 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008109 subprog = find_subprog(env, i + insn->imm + 1);
8110 if (subprog < 0) {
8111 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
8112 i + insn->imm + 1);
8113 return -EFAULT;
8114 }
8115 /* temporarily remember subprog id inside insn instead of
8116 * aux_data, since next loop will split up all insns into funcs
8117 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008118 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008119 /* remember original imm in case JIT fails and fallback
8120 * to interpreter will be needed
8121 */
8122 env->insn_aux_data[i].call_imm = insn->imm;
8123 /* point imm to __bpf_call_base+1 from JITs point of view */
8124 insn->imm = 1;
8125 }
8126
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008127 err = bpf_prog_alloc_jited_linfo(prog);
8128 if (err)
8129 goto out_undo_insn;
8130
8131 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07008132 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008133 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008134 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008135
Jiong Wangf910cef2018-05-02 16:17:17 -04008136 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008137 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04008138 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008139
8140 len = subprog_end - subprog_start;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08008141 /* BPF_PROG_RUN doesn't call subprogs directly,
8142 * hence main prog stats include the runtime of subprogs.
8143 * subprogs don't have IDs and not reachable via prog_get_next_id
8144 * func[i]->aux->stats will never be accessed and stays NULL
8145 */
8146 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008147 if (!func[i])
8148 goto out_free;
8149 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
8150 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01008151 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008152 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01008153 if (bpf_prog_calc_tag(func[i]))
8154 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008155 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08008156 func[i]->aux->func_idx = i;
8157 /* the btf and func_info will be freed only at prog->aux */
8158 func[i]->aux->btf = prog->aux->btf;
8159 func[i]->aux->func_info = prog->aux->func_info;
8160
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008161 /* Use bpf_prog_F_tag to indicate functions in stack traces.
8162 * Long term would need debug info to populate names
8163 */
8164 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04008165 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008166 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008167 func[i]->aux->linfo = prog->aux->linfo;
8168 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
8169 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
8170 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008171 func[i] = bpf_int_jit_compile(func[i]);
8172 if (!func[i]->jited) {
8173 err = -ENOTSUPP;
8174 goto out_free;
8175 }
8176 cond_resched();
8177 }
8178 /* at this point all bpf functions were successfully JITed
8179 * now populate all bpf_calls with correct addresses and
8180 * run last pass of JIT
8181 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008182 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008183 insn = func[i]->insnsi;
8184 for (j = 0; j < func[i]->len; j++, insn++) {
8185 if (insn->code != (BPF_JMP | BPF_CALL) ||
8186 insn->src_reg != BPF_PSEUDO_CALL)
8187 continue;
8188 subprog = insn->off;
Prashant Bhole0d306c32019-04-16 18:13:01 +09008189 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
8190 __bpf_call_base;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008191 }
Sandipan Das2162fed2018-05-24 12:26:45 +05308192
8193 /* we use the aux data to keep a list of the start addresses
8194 * of the JITed images for each function in the program
8195 *
8196 * for some architectures, such as powerpc64, the imm field
8197 * might not be large enough to hold the offset of the start
8198 * address of the callee's JITed image from __bpf_call_base
8199 *
8200 * in such cases, we can lookup the start address of a callee
8201 * by using its subprog id, available from the off field of
8202 * the call instruction, as an index for this list
8203 */
8204 func[i]->aux->func = func;
8205 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008206 }
Jiong Wangf910cef2018-05-02 16:17:17 -04008207 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008208 old_bpf_func = func[i]->bpf_func;
8209 tmp = bpf_int_jit_compile(func[i]);
8210 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
8211 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008212 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008213 goto out_free;
8214 }
8215 cond_resched();
8216 }
8217
8218 /* finally lock prog and jit images for all functions and
8219 * populate kallsysm
8220 */
Jiong Wangf910cef2018-05-02 16:17:17 -04008221 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008222 bpf_prog_lock_ro(func[i]);
8223 bpf_prog_kallsyms_add(func[i]);
8224 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01008225
8226 /* Last step: make now unused interpreter insns from main
8227 * prog consistent for later dump requests, so they can
8228 * later look the same as if they were interpreted only.
8229 */
8230 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01008231 if (insn->code != (BPF_JMP | BPF_CALL) ||
8232 insn->src_reg != BPF_PSEUDO_CALL)
8233 continue;
8234 insn->off = env->insn_aux_data[i].call_imm;
8235 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05308236 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01008237 }
8238
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008239 prog->jited = 1;
8240 prog->bpf_func = func[0]->bpf_func;
8241 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04008242 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008243 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008244 return 0;
8245out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04008246 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008247 if (func[i])
8248 bpf_jit_free(func[i]);
8249 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008250out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008251 /* cleanup main prog to be interpreted */
8252 prog->jit_requested = 0;
8253 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
8254 if (insn->code != (BPF_JMP | BPF_CALL) ||
8255 insn->src_reg != BPF_PSEUDO_CALL)
8256 continue;
8257 insn->off = 0;
8258 insn->imm = env->insn_aux_data[i].call_imm;
8259 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008260 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008261 return err;
8262}
8263
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008264static int fixup_call_args(struct bpf_verifier_env *env)
8265{
David S. Miller19d28fb2018-01-11 21:27:54 -05008266#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008267 struct bpf_prog *prog = env->prog;
8268 struct bpf_insn *insn = prog->insnsi;
8269 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05008270#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01008271 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008272
Quentin Monnete4052d02018-10-07 12:56:58 +01008273 if (env->prog->jit_requested &&
8274 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05008275 err = jit_subprogs(env);
8276 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08008277 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02008278 if (err == -EFAULT)
8279 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05008280 }
8281#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008282 for (i = 0; i < prog->len; i++, insn++) {
8283 if (insn->code != (BPF_JMP | BPF_CALL) ||
8284 insn->src_reg != BPF_PSEUDO_CALL)
8285 continue;
8286 depth = get_callee_stack_depth(env, insn, i);
8287 if (depth < 0)
8288 return depth;
8289 bpf_patch_call_args(insn, depth);
8290 }
David S. Miller19d28fb2018-01-11 21:27:54 -05008291 err = 0;
8292#endif
8293 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008294}
8295
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008296/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008297 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008298 *
8299 * this function is called after eBPF program passed verification
8300 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008301static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008302{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008303 struct bpf_prog *prog = env->prog;
8304 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008305 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008306 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02008307 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008308 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008309 struct bpf_insn insn_buf[16];
8310 struct bpf_prog *new_prog;
8311 struct bpf_map *map_ptr;
8312 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008313
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008314 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01008315 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
8316 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8317 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08008318 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01008319 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
8320 struct bpf_insn mask_and_div[] = {
8321 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8322 /* Rx div 0 -> 0 */
8323 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
8324 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
8325 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
8326 *insn,
8327 };
8328 struct bpf_insn mask_and_mod[] = {
8329 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8330 /* Rx mod 0 -> Rx */
8331 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
8332 *insn,
8333 };
8334 struct bpf_insn *patchlet;
8335
8336 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8337 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
8338 patchlet = mask_and_div + (is64 ? 1 : 0);
8339 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
8340 } else {
8341 patchlet = mask_and_mod + (is64 ? 1 : 0);
8342 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
8343 }
8344
8345 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08008346 if (!new_prog)
8347 return -ENOMEM;
8348
8349 delta += cnt - 1;
8350 env->prog = prog = new_prog;
8351 insn = new_prog->insnsi + i + delta;
8352 continue;
8353 }
8354
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02008355 if (BPF_CLASS(insn->code) == BPF_LD &&
8356 (BPF_MODE(insn->code) == BPF_ABS ||
8357 BPF_MODE(insn->code) == BPF_IND)) {
8358 cnt = env->ops->gen_ld_abs(insn, insn_buf);
8359 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
8360 verbose(env, "bpf verifier is misconfigured\n");
8361 return -EINVAL;
8362 }
8363
8364 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8365 if (!new_prog)
8366 return -ENOMEM;
8367
8368 delta += cnt - 1;
8369 env->prog = prog = new_prog;
8370 insn = new_prog->insnsi + i + delta;
8371 continue;
8372 }
8373
Daniel Borkmann979d63d2019-01-03 00:58:34 +01008374 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
8375 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
8376 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
8377 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
8378 struct bpf_insn insn_buf[16];
8379 struct bpf_insn *patch = &insn_buf[0];
8380 bool issrc, isneg;
8381 u32 off_reg;
8382
8383 aux = &env->insn_aux_data[i + delta];
Daniel Borkmann3612af72019-03-01 22:05:29 +01008384 if (!aux->alu_state ||
8385 aux->alu_state == BPF_ALU_NON_POINTER)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01008386 continue;
8387
8388 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
8389 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
8390 BPF_ALU_SANITIZE_SRC;
8391
8392 off_reg = issrc ? insn->src_reg : insn->dst_reg;
8393 if (isneg)
8394 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
8395 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
8396 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
8397 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
8398 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
8399 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
8400 if (issrc) {
8401 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
8402 off_reg);
8403 insn->src_reg = BPF_REG_AX;
8404 } else {
8405 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
8406 BPF_REG_AX);
8407 }
8408 if (isneg)
8409 insn->code = insn->code == code_add ?
8410 code_sub : code_add;
8411 *patch++ = *insn;
8412 if (issrc && isneg)
8413 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
8414 cnt = patch - insn_buf;
8415
8416 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8417 if (!new_prog)
8418 return -ENOMEM;
8419
8420 delta += cnt - 1;
8421 env->prog = prog = new_prog;
8422 insn = new_prog->insnsi + i + delta;
8423 continue;
8424 }
8425
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008426 if (insn->code != (BPF_JMP | BPF_CALL))
8427 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08008428 if (insn->src_reg == BPF_PSEUDO_CALL)
8429 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008430
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008431 if (insn->imm == BPF_FUNC_get_route_realm)
8432 prog->dst_needed = 1;
8433 if (insn->imm == BPF_FUNC_get_prandom_u32)
8434 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05008435 if (insn->imm == BPF_FUNC_override_return)
8436 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008437 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04008438 /* If we tail call into other programs, we
8439 * cannot make any assumptions since they can
8440 * be replaced dynamically during runtime in
8441 * the program array.
8442 */
8443 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07008444 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05008445 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04008446
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008447 /* mark bpf_tail_call as different opcode to avoid
8448 * conditional branch in the interpeter for every normal
8449 * call and to prevent accidental JITing by JIT compiler
8450 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008451 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008452 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07008453 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08008454
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008455 aux = &env->insn_aux_data[i + delta];
8456 if (!bpf_map_ptr_unpriv(aux))
8457 continue;
8458
Alexei Starovoitovb2157392018-01-07 17:33:02 -08008459 /* instead of changing every JIT dealing with tail_call
8460 * emit two extra insns:
8461 * if (index >= max_entries) goto out;
8462 * index &= array->index_mask;
8463 * to avoid out-of-bounds cpu speculation
8464 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008465 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00008466 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08008467 return -EINVAL;
8468 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008469
8470 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08008471 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
8472 map_ptr->max_entries, 2);
8473 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
8474 container_of(map_ptr,
8475 struct bpf_array,
8476 map)->index_mask);
8477 insn_buf[2] = *insn;
8478 cnt = 3;
8479 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8480 if (!new_prog)
8481 return -ENOMEM;
8482
8483 delta += cnt - 1;
8484 env->prog = prog = new_prog;
8485 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008486 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008487 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008488
Daniel Borkmann89c63072017-08-19 03:12:45 +02008489 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02008490 * and other inlining handlers are currently limited to 64 bit
8491 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02008492 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08008493 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02008494 (insn->imm == BPF_FUNC_map_lookup_elem ||
8495 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02008496 insn->imm == BPF_FUNC_map_delete_elem ||
8497 insn->imm == BPF_FUNC_map_push_elem ||
8498 insn->imm == BPF_FUNC_map_pop_elem ||
8499 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02008500 aux = &env->insn_aux_data[i + delta];
8501 if (bpf_map_ptr_poisoned(aux))
8502 goto patch_call_imm;
8503
8504 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02008505 ops = map_ptr->ops;
8506 if (insn->imm == BPF_FUNC_map_lookup_elem &&
8507 ops->map_gen_lookup) {
8508 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
8509 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
8510 verbose(env, "bpf verifier is misconfigured\n");
8511 return -EINVAL;
8512 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008513
Daniel Borkmann09772d92018-06-02 23:06:35 +02008514 new_prog = bpf_patch_insn_data(env, i + delta,
8515 insn_buf, cnt);
8516 if (!new_prog)
8517 return -ENOMEM;
8518
8519 delta += cnt - 1;
8520 env->prog = prog = new_prog;
8521 insn = new_prog->insnsi + i + delta;
8522 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008523 }
8524
Daniel Borkmann09772d92018-06-02 23:06:35 +02008525 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
8526 (void *(*)(struct bpf_map *map, void *key))NULL));
8527 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
8528 (int (*)(struct bpf_map *map, void *key))NULL));
8529 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
8530 (int (*)(struct bpf_map *map, void *key, void *value,
8531 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02008532 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
8533 (int (*)(struct bpf_map *map, void *value,
8534 u64 flags))NULL));
8535 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
8536 (int (*)(struct bpf_map *map, void *value))NULL));
8537 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
8538 (int (*)(struct bpf_map *map, void *value))NULL));
8539
Daniel Borkmann09772d92018-06-02 23:06:35 +02008540 switch (insn->imm) {
8541 case BPF_FUNC_map_lookup_elem:
8542 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
8543 __bpf_call_base;
8544 continue;
8545 case BPF_FUNC_map_update_elem:
8546 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
8547 __bpf_call_base;
8548 continue;
8549 case BPF_FUNC_map_delete_elem:
8550 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
8551 __bpf_call_base;
8552 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02008553 case BPF_FUNC_map_push_elem:
8554 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
8555 __bpf_call_base;
8556 continue;
8557 case BPF_FUNC_map_pop_elem:
8558 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
8559 __bpf_call_base;
8560 continue;
8561 case BPF_FUNC_map_peek_elem:
8562 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
8563 __bpf_call_base;
8564 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02008565 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008566
Daniel Borkmann09772d92018-06-02 23:06:35 +02008567 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07008568 }
8569
8570patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07008571 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008572 /* all functions that have prototype and verifier allowed
8573 * programs to call them, must be real in-kernel functions
8574 */
8575 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008576 verbose(env,
8577 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008578 func_id_name(insn->imm), insn->imm);
8579 return -EFAULT;
8580 }
8581 insn->imm = fn->func - __bpf_call_base;
8582 }
8583
8584 return 0;
8585}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008586
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008587static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008588{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008589 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008590 int i;
8591
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -07008592 sl = env->free_list;
8593 while (sl) {
8594 sln = sl->next;
8595 free_verifier_state(&sl->state, false);
8596 kfree(sl);
8597 sl = sln;
8598 }
8599
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008600 if (!env->explored_states)
8601 return;
8602
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07008603 for (i = 0; i < state_htab_size(env); i++) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008604 sl = env->explored_states[i];
8605
Alexei Starovoitova8f500a2019-05-21 20:17:06 -07008606 while (sl) {
8607 sln = sl->next;
8608 free_verifier_state(&sl->state, false);
8609 kfree(sl);
8610 sl = sln;
8611 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008612 }
8613
Alexei Starovoitov71dde682019-04-01 21:27:43 -07008614 kvfree(env->explored_states);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008615}
8616
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008617static void print_verification_stats(struct bpf_verifier_env *env)
8618{
8619 int i;
8620
8621 if (env->log.level & BPF_LOG_STATS) {
8622 verbose(env, "verification time %lld usec\n",
8623 div_u64(env->verification_time, 1000));
8624 verbose(env, "stack depth ");
8625 for (i = 0; i < env->subprog_cnt; i++) {
8626 u32 depth = env->subprog_info[i].stack_depth;
8627
8628 verbose(env, "%d", depth);
8629 if (i + 1 < env->subprog_cnt)
8630 verbose(env, "+");
8631 }
8632 verbose(env, "\n");
8633 }
8634 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
8635 "total_states %d peak_states %d mark_read %d\n",
8636 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
8637 env->max_states_per_insn, env->total_states,
8638 env->peak_states, env->longest_mark_read_walk);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008639}
8640
Yonghong Song838e9692018-11-19 15:29:11 -08008641int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
8642 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008643{
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008644 u64 start_time = ktime_get_ns();
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008645 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07008646 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008647 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008648 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008649
Arnd Bergmanneba0c922017-11-02 12:05:52 +01008650 /* no program is valid */
8651 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
8652 return -EINVAL;
8653
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008654 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008655 * allocate/free it every time bpf_check() is called
8656 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008657 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008658 if (!env)
8659 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07008660 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008661
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008662 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07008663 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008664 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008665 ret = -ENOMEM;
8666 if (!env->insn_aux_data)
8667 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08008668 for (i = 0; i < len; i++)
8669 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008670 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07008671 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008672 is_priv = capable(CAP_SYS_ADMIN);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008673
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008674 /* grab the mutex to protect few globals used by verifier */
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008675 if (!is_priv)
8676 mutex_lock(&bpf_verifier_lock);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008677
8678 if (attr->log_level || attr->log_buf || attr->log_size) {
8679 /* user requested verbose verifier output
8680 * and supplied buffer to store the verification trace
8681 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008682 log->level = attr->log_level;
8683 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
8684 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008685
8686 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07008687 /* log attributes have to be sane */
Alexei Starovoitov7a9f5c62019-04-01 21:27:46 -07008688 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008689 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008690 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008691 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02008692
8693 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
8694 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07008695 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08008696 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
8697 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008698
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008699 env->allow_ptr_leaks = is_priv;
8700
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008701 ret = replace_map_fd_with_map_ptr(env);
8702 if (ret < 0)
8703 goto skip_full_check;
8704
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008705 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00008706 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07008707 if (ret)
8708 goto skip_full_check;
8709 }
8710
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -07008711 env->explored_states = kvcalloc(state_htab_size(env),
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01008712 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008713 GFP_USER);
8714 ret = -ENOMEM;
8715 if (!env->explored_states)
8716 goto skip_full_check;
8717
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008718 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07008719 if (ret < 0)
8720 goto skip_full_check;
8721
Martin KaFai Lauc454a462018-12-07 16:42:25 -08008722 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08008723 if (ret < 0)
8724 goto skip_full_check;
8725
Martin KaFai Laud9762e82018-12-13 10:41:48 -08008726 ret = check_cfg(env);
8727 if (ret < 0)
8728 goto skip_full_check;
8729
Alexei Starovoitov17a52672014-09-26 00:17:06 -07008730 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04008731 if (env->cur_state) {
8732 free_verifier_state(env->cur_state, true);
8733 env->cur_state = NULL;
8734 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008735
Quentin Monnetc941ce92018-10-07 12:56:47 +01008736 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
8737 ret = bpf_prog_offload_finalize(env);
8738
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008739skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07008740 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07008741 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008742
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008743 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08008744 ret = check_max_stack_depth(env);
8745
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008746 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008747 if (is_priv) {
8748 if (ret == 0)
8749 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008750 if (ret == 0)
8751 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08008752 if (ret == 0)
8753 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08008754 } else {
8755 if (ret == 0)
8756 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08008757 }
8758
Jakub Kicinski9b38c402018-12-19 22:13:06 -08008759 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008760 /* program is valid, convert *(u32*)(ctx + off) accesses */
8761 ret = convert_ctx_accesses(env);
8762
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008763 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07008764 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07008765
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008766 /* do 32-bit optimization after insn patching has done so those patched
8767 * insns could be handled correctly.
8768 */
Jiong Wangd6c23082019-05-24 23:25:18 +01008769 if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) {
8770 ret = opt_subreg_zext_lo32_rnd_hi32(env, attr);
8771 env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret
8772 : false;
Jiong Wanga4b1d3c2019-05-24 23:25:15 +01008773 }
8774
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08008775 if (ret == 0)
8776 ret = fixup_call_args(env);
8777
Alexei Starovoitov06ee7112019-04-01 21:27:40 -07008778 env->verification_time = ktime_get_ns() - start_time;
8779 print_verification_stats(env);
8780
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008781 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008782 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008783 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008784 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008785 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008786 }
8787
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008788 if (ret == 0 && env->used_map_cnt) {
8789 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008790 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
8791 sizeof(env->used_maps[0]),
8792 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008793
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008794 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008795 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008796 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008797 }
8798
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008799 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008800 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008801 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008802
8803 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
8804 * bpf_ld_imm64 instructions
8805 */
8806 convert_pseudo_ld_imm64(env);
8807 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07008808
Yonghong Songba64e7d2018-11-24 23:20:44 -08008809 if (ret == 0)
8810 adjust_btf_func(env);
8811
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07008812err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008813 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008814 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07008815 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07008816 */
8817 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07008818 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008819err_unlock:
Alexei Starovoitov45a73c12019-04-19 07:44:55 -07008820 if (!is_priv)
8821 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01008822 vfree(env->insn_aux_data);
8823err_free_env:
8824 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07008825 return ret;
8826}