blob: 8c1c21cd50b4eb446392f1f4a9f479053fc7876c [file] [log] [blame]
Alexei Starovoitov51580e72014-09-26 00:17:02 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002 * Copyright (c) 2016 Facebook
Joe Stringerfd978bf72018-10-02 13:35:35 -07003 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
Alexei Starovoitov51580e72014-09-26 00:17:02 -07004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 */
Yonghong Song838e9692018-11-19 15:29:11 -080014#include <uapi/linux/btf.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070015#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/bpf.h>
Yonghong Song838e9692018-11-19 15:29:11 -080019#include <linux/btf.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010020#include <linux/bpf_verifier.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070021#include <linux/filter.h>
22#include <net/netlink.h>
23#include <linux/file.h>
24#include <linux/vmalloc.h>
Thomas Grafebb676d2016-10-27 11:23:51 +020025#include <linux/stringify.h>
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -080026#include <linux/bsearch.h>
27#include <linux/sort.h>
Yonghong Songc195651e2018-04-28 22:28:08 -070028#include <linux/perf_event.h>
Martin KaFai Laud9762e82018-12-13 10:41:48 -080029#include <linux/ctype.h>
Alexei Starovoitov51580e72014-09-26 00:17:02 -070030
Jakub Kicinskif4ac7e02017-10-09 10:30:12 -070031#include "disasm.h"
32
Jakub Kicinski00176a32017-10-16 16:40:54 -070033static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
34#define BPF_PROG_TYPE(_id, _name) \
35 [_id] = & _name ## _verifier_ops,
36#define BPF_MAP_TYPE(_id, _ops)
37#include <linux/bpf_types.h>
38#undef BPF_PROG_TYPE
39#undef BPF_MAP_TYPE
40};
41
Alexei Starovoitov51580e72014-09-26 00:17:02 -070042/* bpf_check() is a static code analyzer that walks eBPF program
43 * instruction by instruction and updates register/stack state.
44 * All paths of conditional branches are analyzed until 'bpf_exit' insn.
45 *
46 * The first pass is depth-first-search to check that the program is a DAG.
47 * It rejects the following programs:
48 * - larger than BPF_MAXINSNS insns
49 * - if loop is present (detected via back-edge)
50 * - unreachable insns exist (shouldn't be a forest. program = one function)
51 * - out of bounds or malformed jumps
52 * The second pass is all possible path descent from the 1st insn.
53 * Since it's analyzing all pathes through the program, the length of the
Gary Lineba38a92017-03-01 16:25:51 +080054 * analysis is limited to 64k insn, which may be hit even if total number of
Alexei Starovoitov51580e72014-09-26 00:17:02 -070055 * insn is less then 4K, but there are too many branches that change stack/regs.
56 * Number of 'branches to be analyzed' is limited to 1k
57 *
58 * On entry to each instruction, each register has a type, and the instruction
59 * changes the types of the registers depending on instruction semantics.
60 * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
61 * copied to R1.
62 *
63 * All registers are 64-bit.
64 * R0 - return register
65 * R1-R5 argument passing registers
66 * R6-R9 callee saved registers
67 * R10 - frame pointer read-only
68 *
69 * At the start of BPF program the register R1 contains a pointer to bpf_context
70 * and has type PTR_TO_CTX.
71 *
72 * Verifier tracks arithmetic operations on pointers in case:
73 * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
74 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
75 * 1st insn copies R10 (which has FRAME_PTR) type into R1
76 * and 2nd arithmetic instruction is pattern matched to recognize
77 * that it wants to construct a pointer to some element within stack.
78 * So after 2nd insn, the register R1 has type PTR_TO_STACK
79 * (and -20 constant is saved for further stack bounds checking).
80 * Meaning that this reg is a pointer to stack plus known immediate constant.
81 *
Edward Creef1174f72017-08-07 15:26:19 +010082 * Most of the time the registers have SCALAR_VALUE type, which
Alexei Starovoitov51580e72014-09-26 00:17:02 -070083 * means the register has some value, but it's not a valid pointer.
Edward Creef1174f72017-08-07 15:26:19 +010084 * (like pointer plus pointer becomes SCALAR_VALUE type)
Alexei Starovoitov51580e72014-09-26 00:17:02 -070085 *
86 * When verifier sees load or store instructions the type of base register
Joe Stringerc64b7982018-10-02 13:35:33 -070087 * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are
88 * four pointer types recognized by check_mem_access() function.
Alexei Starovoitov51580e72014-09-26 00:17:02 -070089 *
90 * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
91 * and the range of [ptr, ptr + map's value_size) is accessible.
92 *
93 * registers used to pass values to function calls are checked against
94 * function argument constraints.
95 *
96 * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
97 * It means that the register type passed to this function must be
98 * PTR_TO_STACK and it will be used inside the function as
99 * 'pointer to map element key'
100 *
101 * For example the argument constraints for bpf_map_lookup_elem():
102 * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
103 * .arg1_type = ARG_CONST_MAP_PTR,
104 * .arg2_type = ARG_PTR_TO_MAP_KEY,
105 *
106 * ret_type says that this function returns 'pointer to map elem value or null'
107 * function expects 1st argument to be a const pointer to 'struct bpf_map' and
108 * 2nd argument should be a pointer to stack, which will be used inside
109 * the helper function as a pointer to map element key.
110 *
111 * On the kernel side the helper function looks like:
112 * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
113 * {
114 * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
115 * void *key = (void *) (unsigned long) r2;
116 * void *value;
117 *
118 * here kernel can access 'key' and 'map' pointers safely, knowing that
119 * [key, key + map->key_size) bytes are valid and were initialized on
120 * the stack of eBPF program.
121 * }
122 *
123 * Corresponding eBPF program may look like:
124 * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR
125 * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
126 * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP
127 * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
128 * here verifier looks at prototype of map_lookup_elem() and sees:
129 * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
130 * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
131 *
132 * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
133 * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
134 * and were initialized prior to this call.
135 * If it's ok, then verifier allows this BPF_CALL insn and looks at
136 * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
137 * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
138 * returns ether pointer to map value or NULL.
139 *
140 * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
141 * insn, the register holding that pointer in the true branch changes state to
142 * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
143 * branch. See check_cond_jmp_op().
144 *
145 * After the call R0 is set to return type of the function and registers R1-R5
146 * are set to NOT_INIT to indicate that they are no longer readable.
Joe Stringerfd978bf72018-10-02 13:35:35 -0700147 *
148 * The following reference types represent a potential reference to a kernel
149 * resource which, after first being allocated, must be checked and freed by
150 * the BPF program:
151 * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
152 *
153 * When the verifier sees a helper call return a reference type, it allocates a
154 * pointer id for the reference and stores it in the current function state.
155 * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
156 * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
157 * passes through a NULL-check conditional. For the branch wherein the state is
158 * changed to CONST_IMM, the verifier releases the reference.
Joe Stringer6acc9b42018-10-02 13:35:36 -0700159 *
160 * For each helper function that allocates a reference, such as
161 * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
162 * bpf_sk_release(). When a reference type passes into the release function,
163 * the verifier also releases the reference. If any unchecked or unreleased
164 * reference remains at the end of the program, the verifier rejects it.
Alexei Starovoitov51580e72014-09-26 00:17:02 -0700165 */
166
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700167/* verifier_state + insn_idx are pushed to stack when branch is encountered */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100168struct bpf_verifier_stack_elem {
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700169 /* verifer state is 'st'
170 * before processing instruction 'insn_idx'
171 * and after processing instruction 'prev_insn_idx'
172 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100173 struct bpf_verifier_state st;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700174 int insn_idx;
175 int prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100176 struct bpf_verifier_stack_elem *next;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700177};
178
Edward Cree8e17c1b2017-08-07 15:30:30 +0100179#define BPF_COMPLEXITY_LIMIT_INSNS 131072
Daniel Borkmann07016152016-04-05 22:33:17 +0200180#define BPF_COMPLEXITY_LIMIT_STACK 1024
Alexei Starovoitovceefbc92018-12-03 22:46:06 -0800181#define BPF_COMPLEXITY_LIMIT_STATES 64
Daniel Borkmann07016152016-04-05 22:33:17 +0200182
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200183#define BPF_MAP_PTR_UNPRIV 1UL
184#define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
185 POISON_POINTER_DELTA))
186#define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
187
188static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
189{
190 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
191}
192
193static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
194{
195 return aux->map_state & BPF_MAP_PTR_UNPRIV;
196}
197
198static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
199 const struct bpf_map *map, bool unpriv)
200{
201 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
202 unpriv |= bpf_map_ptr_unpriv(aux);
203 aux->map_state = (unsigned long)map |
204 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
205}
Martin KaFai Laufad73a12017-03-22 10:00:32 -0700206
Daniel Borkmann33ff9822016-04-13 00:10:50 +0200207struct bpf_call_arg_meta {
208 struct bpf_map *map_ptr;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200209 bool raw_mode;
Daniel Borkmann36bbef52016-09-20 00:26:13 +0200210 bool pkt_access;
Daniel Borkmann435faee12016-04-13 00:10:51 +0200211 int regno;
212 int access_size;
Yonghong Song849fa502018-04-28 22:28:09 -0700213 s64 msize_smax_value;
214 u64 msize_umax_value;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700215 int ptr_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
Joe Stringer840b9612018-10-02 13:35:32 -0700333static bool reg_type_may_be_null(enum bpf_reg_type type)
334{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700335 return type == PTR_TO_MAP_VALUE_OR_NULL ||
336 type == PTR_TO_SOCKET_OR_NULL;
337}
338
339static bool type_is_refcounted(enum bpf_reg_type type)
340{
341 return type == PTR_TO_SOCKET;
342}
343
344static bool type_is_refcounted_or_null(enum bpf_reg_type type)
345{
346 return type == PTR_TO_SOCKET || type == PTR_TO_SOCKET_OR_NULL;
347}
348
349static bool reg_is_refcounted(const struct bpf_reg_state *reg)
350{
351 return type_is_refcounted(reg->type);
352}
353
354static bool reg_is_refcounted_or_null(const struct bpf_reg_state *reg)
355{
356 return type_is_refcounted_or_null(reg->type);
357}
358
359static bool arg_type_is_refcounted(enum bpf_arg_type type)
360{
361 return type == ARG_PTR_TO_SOCKET;
362}
363
364/* Determine whether the function releases some resources allocated by another
365 * function call. The first reference type argument will be assumed to be
366 * released by release_reference().
367 */
368static bool is_release_function(enum bpf_func_id func_id)
369{
Joe Stringer6acc9b42018-10-02 13:35:36 -0700370 return func_id == BPF_FUNC_sk_release;
Joe Stringer840b9612018-10-02 13:35:32 -0700371}
372
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700373/* string representation of 'enum bpf_reg_type' */
374static const char * const reg_type_str[] = {
375 [NOT_INIT] = "?",
Edward Creef1174f72017-08-07 15:26:19 +0100376 [SCALAR_VALUE] = "inv",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700377 [PTR_TO_CTX] = "ctx",
378 [CONST_PTR_TO_MAP] = "map_ptr",
379 [PTR_TO_MAP_VALUE] = "map_value",
380 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700381 [PTR_TO_STACK] = "fp",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700382 [PTR_TO_PACKET] = "pkt",
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200383 [PTR_TO_PACKET_META] = "pkt_meta",
Alexei Starovoitov969bf052016-05-05 19:49:10 -0700384 [PTR_TO_PACKET_END] = "pkt_end",
Petar Penkovd58e4682018-09-14 07:46:18 -0700385 [PTR_TO_FLOW_KEYS] = "flow_keys",
Joe Stringerc64b7982018-10-02 13:35:33 -0700386 [PTR_TO_SOCKET] = "sock",
387 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700388};
389
Edward Cree8efea212018-08-22 20:02:44 +0100390static char slot_type_char[] = {
391 [STACK_INVALID] = '?',
392 [STACK_SPILL] = 'r',
393 [STACK_MISC] = 'm',
394 [STACK_ZERO] = '0',
395};
396
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800397static void print_liveness(struct bpf_verifier_env *env,
398 enum bpf_reg_liveness live)
399{
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800400 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800401 verbose(env, "_");
402 if (live & REG_LIVE_READ)
403 verbose(env, "r");
404 if (live & REG_LIVE_WRITTEN)
405 verbose(env, "w");
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -0800406 if (live & REG_LIVE_DONE)
407 verbose(env, "D");
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800408}
409
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800410static struct bpf_func_state *func(struct bpf_verifier_env *env,
411 const struct bpf_reg_state *reg)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700412{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800413 struct bpf_verifier_state *cur = env->cur_state;
414
415 return cur->frame[reg->frameno];
416}
417
418static void print_verifier_state(struct bpf_verifier_env *env,
419 const struct bpf_func_state *state)
420{
421 const struct bpf_reg_state *reg;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700422 enum bpf_reg_type t;
423 int i;
424
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800425 if (state->frameno)
426 verbose(env, " frame%d:", state->frameno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700427 for (i = 0; i < MAX_BPF_REG; i++) {
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -0700428 reg = &state->regs[i];
429 t = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700430 if (t == NOT_INIT)
431 continue;
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800432 verbose(env, " R%d", i);
433 print_liveness(env, reg->live);
434 verbose(env, "=%s", reg_type_str[t]);
Edward Creef1174f72017-08-07 15:26:19 +0100435 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
436 tnum_is_const(reg->var_off)) {
437 /* reg->off should be 0 for SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700438 verbose(env, "%lld", reg->var_off.value + reg->off);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800439 if (t == PTR_TO_STACK)
440 verbose(env, ",call_%d", func(env, reg)->callsite);
Edward Creef1174f72017-08-07 15:26:19 +0100441 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700442 verbose(env, "(id=%d", reg->id);
Edward Creef1174f72017-08-07 15:26:19 +0100443 if (t != SCALAR_VALUE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700444 verbose(env, ",off=%d", reg->off);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200445 if (type_is_pkt_pointer(t))
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700446 verbose(env, ",r=%d", reg->range);
Edward Creef1174f72017-08-07 15:26:19 +0100447 else if (t == CONST_PTR_TO_MAP ||
448 t == PTR_TO_MAP_VALUE ||
449 t == PTR_TO_MAP_VALUE_OR_NULL)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700450 verbose(env, ",ks=%d,vs=%d",
Edward Creef1174f72017-08-07 15:26:19 +0100451 reg->map_ptr->key_size,
452 reg->map_ptr->value_size);
Edward Cree7d1238f2017-08-07 15:26:56 +0100453 if (tnum_is_const(reg->var_off)) {
454 /* Typically an immediate SCALAR_VALUE, but
455 * could be a pointer whose offset is too big
456 * for reg->off
457 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700458 verbose(env, ",imm=%llx", reg->var_off.value);
Edward Cree7d1238f2017-08-07 15:26:56 +0100459 } else {
460 if (reg->smin_value != reg->umin_value &&
461 reg->smin_value != S64_MIN)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700462 verbose(env, ",smin_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100463 (long long)reg->smin_value);
464 if (reg->smax_value != reg->umax_value &&
465 reg->smax_value != S64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700466 verbose(env, ",smax_value=%lld",
Edward Cree7d1238f2017-08-07 15:26:56 +0100467 (long long)reg->smax_value);
468 if (reg->umin_value != 0)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700469 verbose(env, ",umin_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100470 (unsigned long long)reg->umin_value);
471 if (reg->umax_value != U64_MAX)
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700472 verbose(env, ",umax_value=%llu",
Edward Cree7d1238f2017-08-07 15:26:56 +0100473 (unsigned long long)reg->umax_value);
474 if (!tnum_is_unknown(reg->var_off)) {
475 char tn_buf[48];
Edward Creef1174f72017-08-07 15:26:19 +0100476
Edward Cree7d1238f2017-08-07 15:26:56 +0100477 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700478 verbose(env, ",var_off=%s", tn_buf);
Edward Cree7d1238f2017-08-07 15:26:56 +0100479 }
Edward Creef1174f72017-08-07 15:26:19 +0100480 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700481 verbose(env, ")");
Edward Creef1174f72017-08-07 15:26:19 +0100482 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700483 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700484 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
Edward Cree8efea212018-08-22 20:02:44 +0100485 char types_buf[BPF_REG_SIZE + 1];
486 bool valid = false;
487 int j;
488
489 for (j = 0; j < BPF_REG_SIZE; j++) {
490 if (state->stack[i].slot_type[j] != STACK_INVALID)
491 valid = true;
492 types_buf[j] = slot_type_char[
493 state->stack[i].slot_type[j]];
494 }
495 types_buf[BPF_REG_SIZE] = 0;
496 if (!valid)
497 continue;
498 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
499 print_liveness(env, state->stack[i].spilled_ptr.live);
500 if (state->stack[i].slot_type[0] == STACK_SPILL)
Alexei Starovoitov4e920242017-11-30 21:31:36 -0800501 verbose(env, "=%s",
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700502 reg_type_str[state->stack[i].spilled_ptr.type]);
Edward Cree8efea212018-08-22 20:02:44 +0100503 else
504 verbose(env, "=%s", types_buf);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700505 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700506 if (state->acquired_refs && state->refs[0].id) {
507 verbose(env, " refs=%d", state->refs[0].id);
508 for (i = 1; i < state->acquired_refs; i++)
509 if (state->refs[i].id)
510 verbose(env, ",%d", state->refs[i].id);
511 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700512 verbose(env, "\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700513}
514
Joe Stringer84dbf352018-10-02 13:35:34 -0700515#define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
516static int copy_##NAME##_state(struct bpf_func_state *dst, \
517 const struct bpf_func_state *src) \
518{ \
519 if (!src->FIELD) \
520 return 0; \
521 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
522 /* internal bug, make state invalid to reject the program */ \
523 memset(dst, 0, sizeof(*dst)); \
524 return -EFAULT; \
525 } \
526 memcpy(dst->FIELD, src->FIELD, \
527 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
528 return 0; \
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700529}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700530/* copy_reference_state() */
531COPY_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700532/* copy_stack_state() */
533COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
534#undef COPY_STATE_FN
535
536#define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
537static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
538 bool copy_old) \
539{ \
540 u32 old_size = state->COUNT; \
541 struct bpf_##NAME##_state *new_##FIELD; \
542 int slot = size / SIZE; \
543 \
544 if (size <= old_size || !size) { \
545 if (copy_old) \
546 return 0; \
547 state->COUNT = slot * SIZE; \
548 if (!size && old_size) { \
549 kfree(state->FIELD); \
550 state->FIELD = NULL; \
551 } \
552 return 0; \
553 } \
554 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
555 GFP_KERNEL); \
556 if (!new_##FIELD) \
557 return -ENOMEM; \
558 if (copy_old) { \
559 if (state->FIELD) \
560 memcpy(new_##FIELD, state->FIELD, \
561 sizeof(*new_##FIELD) * (old_size / SIZE)); \
562 memset(new_##FIELD + old_size / SIZE, 0, \
563 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
564 } \
565 state->COUNT = slot * SIZE; \
566 kfree(state->FIELD); \
567 state->FIELD = new_##FIELD; \
568 return 0; \
569}
Joe Stringerfd978bf72018-10-02 13:35:35 -0700570/* realloc_reference_state() */
571REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
Joe Stringer84dbf352018-10-02 13:35:34 -0700572/* realloc_stack_state() */
573REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
574#undef REALLOC_STATE_FN
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700575
576/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
577 * make it consume minimal amount of memory. check_stack_write() access from
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800578 * the program calls into realloc_func_state() to grow the stack size.
Joe Stringer84dbf352018-10-02 13:35:34 -0700579 * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
580 * which realloc_stack_state() copies over. It points to previous
581 * bpf_verifier_state which is never reallocated.
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700582 */
Joe Stringerfd978bf72018-10-02 13:35:35 -0700583static int realloc_func_state(struct bpf_func_state *state, int stack_size,
584 int refs_size, bool copy_old)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700585{
Joe Stringerfd978bf72018-10-02 13:35:35 -0700586 int err = realloc_reference_state(state, refs_size, copy_old);
587 if (err)
588 return err;
589 return realloc_stack_state(state, stack_size, copy_old);
590}
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700591
Joe Stringerfd978bf72018-10-02 13:35:35 -0700592/* Acquire a pointer id from the env and update the state->refs to include
593 * this new pointer reference.
594 * On success, returns a valid pointer id to associate with the register
595 * On failure, returns a negative errno.
596 */
597static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
598{
599 struct bpf_func_state *state = cur_func(env);
600 int new_ofs = state->acquired_refs;
601 int id, err;
602
603 err = realloc_reference_state(state, state->acquired_refs + 1, true);
604 if (err)
605 return err;
606 id = ++env->id_gen;
607 state->refs[new_ofs].id = id;
608 state->refs[new_ofs].insn_idx = insn_idx;
609
610 return id;
611}
612
613/* release function corresponding to acquire_reference_state(). Idempotent. */
614static int __release_reference_state(struct bpf_func_state *state, int ptr_id)
615{
616 int i, last_idx;
617
618 if (!ptr_id)
619 return -EFAULT;
620
621 last_idx = state->acquired_refs - 1;
622 for (i = 0; i < state->acquired_refs; i++) {
623 if (state->refs[i].id == ptr_id) {
624 if (last_idx && i != last_idx)
625 memcpy(&state->refs[i], &state->refs[last_idx],
626 sizeof(*state->refs));
627 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
628 state->acquired_refs--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700629 return 0;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700630 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700631 }
Joe Stringerfd978bf72018-10-02 13:35:35 -0700632 return -EFAULT;
633}
634
635/* variation on the above for cases where we expect that there must be an
636 * outstanding reference for the specified ptr_id.
637 */
638static int release_reference_state(struct bpf_verifier_env *env, int ptr_id)
639{
640 struct bpf_func_state *state = cur_func(env);
641 int err;
642
643 err = __release_reference_state(state, ptr_id);
644 if (WARN_ON_ONCE(err != 0))
645 verbose(env, "verifier internal error: can't release reference\n");
646 return err;
647}
648
649static int transfer_reference_state(struct bpf_func_state *dst,
650 struct bpf_func_state *src)
651{
652 int err = realloc_reference_state(dst, src->acquired_refs, false);
653 if (err)
654 return err;
655 err = copy_reference_state(dst, src);
656 if (err)
657 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700658 return 0;
659}
660
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800661static void free_func_state(struct bpf_func_state *state)
662{
Alexei Starovoitov58963512018-01-08 07:51:17 -0800663 if (!state)
664 return;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700665 kfree(state->refs);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800666 kfree(state->stack);
667 kfree(state);
668}
669
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700670static void free_verifier_state(struct bpf_verifier_state *state,
671 bool free_self)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700672{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800673 int i;
674
675 for (i = 0; i <= state->curframe; i++) {
676 free_func_state(state->frame[i]);
677 state->frame[i] = NULL;
678 }
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700679 if (free_self)
680 kfree(state);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700681}
682
683/* copy verifier state from src to dst growing dst stack space
684 * when necessary to accommodate larger src stack
685 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800686static int copy_func_state(struct bpf_func_state *dst,
687 const struct bpf_func_state *src)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700688{
689 int err;
690
Joe Stringerfd978bf72018-10-02 13:35:35 -0700691 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
692 false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700693 if (err)
694 return err;
Joe Stringerfd978bf72018-10-02 13:35:35 -0700695 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
696 err = copy_reference_state(dst, src);
697 if (err)
698 return err;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700699 return copy_stack_state(dst, src);
700}
701
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800702static int copy_verifier_state(struct bpf_verifier_state *dst_state,
703 const struct bpf_verifier_state *src)
704{
705 struct bpf_func_state *dst;
706 int i, err;
707
708 /* if dst has more stack frames then src frame, free them */
709 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
710 free_func_state(dst_state->frame[i]);
711 dst_state->frame[i] = NULL;
712 }
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100713 dst_state->speculative = src->speculative;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800714 dst_state->curframe = src->curframe;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800715 for (i = 0; i <= src->curframe; i++) {
716 dst = dst_state->frame[i];
717 if (!dst) {
718 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
719 if (!dst)
720 return -ENOMEM;
721 dst_state->frame[i] = dst;
722 }
723 err = copy_func_state(dst, src->frame[i]);
724 if (err)
725 return err;
726 }
727 return 0;
728}
729
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700730static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
731 int *insn_idx)
732{
733 struct bpf_verifier_state *cur = env->cur_state;
734 struct bpf_verifier_stack_elem *elem, *head = env->head;
735 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700736
737 if (env->head == NULL)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700738 return -ENOENT;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700739
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700740 if (cur) {
741 err = copy_verifier_state(cur, &head->st);
742 if (err)
743 return err;
744 }
745 if (insn_idx)
746 *insn_idx = head->insn_idx;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700747 if (prev_insn_idx)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700748 *prev_insn_idx = head->prev_insn_idx;
749 elem = head->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700750 free_verifier_state(&head->st, false);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700751 kfree(head);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700752 env->head = elem;
753 env->stack_size--;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700754 return 0;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700755}
756
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100757static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100758 int insn_idx, int prev_insn_idx,
759 bool speculative)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700760{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700761 struct bpf_verifier_state *cur = env->cur_state;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100762 struct bpf_verifier_stack_elem *elem;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700763 int err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700764
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700765 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700766 if (!elem)
767 goto err;
768
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700769 elem->insn_idx = insn_idx;
770 elem->prev_insn_idx = prev_insn_idx;
771 elem->next = env->head;
772 env->head = elem;
773 env->stack_size++;
Alexei Starovoitov1969db42017-11-01 00:08:04 -0700774 err = copy_verifier_state(&elem->st, cur);
775 if (err)
776 goto err;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100777 elem->st.speculative |= speculative;
Daniel Borkmann07016152016-04-05 22:33:17 +0200778 if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700779 verbose(env, "BPF program is too complex\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700780 goto err;
781 }
782 return &elem->st;
783err:
Alexei Starovoitov58963512018-01-08 07:51:17 -0800784 free_verifier_state(env->cur_state, true);
785 env->cur_state = NULL;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700786 /* pop all elements and return */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700787 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700788 return NULL;
789}
790
791#define CALLER_SAVED_REGS 6
792static const int caller_saved[CALLER_SAVED_REGS] = {
793 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
794};
795
Edward Creef1174f72017-08-07 15:26:19 +0100796static void __mark_reg_not_init(struct bpf_reg_state *reg);
797
Edward Creeb03c9f92017-08-07 15:26:36 +0100798/* Mark the unknown part of a register (variable offset or scalar value) as
799 * known to have the value @imm.
800 */
801static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
802{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700803 /* Clear id, off, and union(map_ptr, range) */
804 memset(((u8 *)reg) + sizeof(reg->type), 0,
805 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
Edward Creeb03c9f92017-08-07 15:26:36 +0100806 reg->var_off = tnum_const(imm);
807 reg->smin_value = (s64)imm;
808 reg->smax_value = (s64)imm;
809 reg->umin_value = imm;
810 reg->umax_value = imm;
811}
812
Edward Creef1174f72017-08-07 15:26:19 +0100813/* Mark the 'variable offset' part of a register as zero. This should be
814 * used only on registers holding a pointer type.
815 */
816static void __mark_reg_known_zero(struct bpf_reg_state *reg)
817{
Edward Creeb03c9f92017-08-07 15:26:36 +0100818 __mark_reg_known(reg, 0);
Edward Creef1174f72017-08-07 15:26:19 +0100819}
820
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800821static void __mark_reg_const_zero(struct bpf_reg_state *reg)
822{
823 __mark_reg_known(reg, 0);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800824 reg->type = SCALAR_VALUE;
825}
826
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700827static void mark_reg_known_zero(struct bpf_verifier_env *env,
828 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100829{
830 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700831 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +0100832 /* Something bad happened, let's kill all regs */
833 for (regno = 0; regno < MAX_BPF_REG; regno++)
834 __mark_reg_not_init(regs + regno);
835 return;
836 }
837 __mark_reg_known_zero(regs + regno);
838}
839
Daniel Borkmannde8f3a82017-09-25 02:25:51 +0200840static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
841{
842 return type_is_pkt_pointer(reg->type);
843}
844
845static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
846{
847 return reg_is_pkt_pointer(reg) ||
848 reg->type == PTR_TO_PACKET_END;
849}
850
851/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
852static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
853 enum bpf_reg_type which)
854{
855 /* The register can already have a range from prior markings.
856 * This is fine as long as it hasn't been advanced from its
857 * origin.
858 */
859 return reg->type == which &&
860 reg->id == 0 &&
861 reg->off == 0 &&
862 tnum_equals_const(reg->var_off, 0);
863}
864
Edward Creeb03c9f92017-08-07 15:26:36 +0100865/* Attempts to improve min/max values based on var_off information */
866static void __update_reg_bounds(struct bpf_reg_state *reg)
867{
868 /* min signed is max(sign bit) | min(other bits) */
869 reg->smin_value = max_t(s64, reg->smin_value,
870 reg->var_off.value | (reg->var_off.mask & S64_MIN));
871 /* max signed is min(sign bit) | max(other bits) */
872 reg->smax_value = min_t(s64, reg->smax_value,
873 reg->var_off.value | (reg->var_off.mask & S64_MAX));
874 reg->umin_value = max(reg->umin_value, reg->var_off.value);
875 reg->umax_value = min(reg->umax_value,
876 reg->var_off.value | reg->var_off.mask);
877}
878
879/* Uses signed min/max values to inform unsigned, and vice-versa */
880static void __reg_deduce_bounds(struct bpf_reg_state *reg)
881{
882 /* Learn sign from signed bounds.
883 * If we cannot cross the sign boundary, then signed and unsigned bounds
884 * are the same, so combine. This works even in the negative case, e.g.
885 * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
886 */
887 if (reg->smin_value >= 0 || reg->smax_value < 0) {
888 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
889 reg->umin_value);
890 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
891 reg->umax_value);
892 return;
893 }
894 /* Learn sign from unsigned bounds. Signed bounds cross the sign
895 * boundary, so we must be careful.
896 */
897 if ((s64)reg->umax_value >= 0) {
898 /* Positive. We can't learn anything from the smin, but smax
899 * is positive, hence safe.
900 */
901 reg->smin_value = reg->umin_value;
902 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
903 reg->umax_value);
904 } else if ((s64)reg->umin_value < 0) {
905 /* Negative. We can't learn anything from the smax, but smin
906 * is negative, hence safe.
907 */
908 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
909 reg->umin_value);
910 reg->smax_value = reg->umax_value;
911 }
912}
913
914/* Attempts to improve var_off based on unsigned min/max information */
915static void __reg_bound_offset(struct bpf_reg_state *reg)
916{
917 reg->var_off = tnum_intersect(reg->var_off,
918 tnum_range(reg->umin_value,
919 reg->umax_value));
920}
921
922/* Reset the min/max bounds of a register */
923static void __mark_reg_unbounded(struct bpf_reg_state *reg)
924{
925 reg->smin_value = S64_MIN;
926 reg->smax_value = S64_MAX;
927 reg->umin_value = 0;
928 reg->umax_value = U64_MAX;
929}
930
Edward Creef1174f72017-08-07 15:26:19 +0100931/* Mark a register as having a completely unknown (scalar) value. */
932static void __mark_reg_unknown(struct bpf_reg_state *reg)
933{
Alexei Starovoitova9c676b2018-09-04 19:13:44 -0700934 /*
935 * Clear type, id, off, and union(map_ptr, range) and
936 * padding between 'type' and union
937 */
938 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
Edward Creef1174f72017-08-07 15:26:19 +0100939 reg->type = SCALAR_VALUE;
Edward Creef1174f72017-08-07 15:26:19 +0100940 reg->var_off = tnum_unknown;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800941 reg->frameno = 0;
Edward Creeb03c9f92017-08-07 15:26:36 +0100942 __mark_reg_unbounded(reg);
Edward Creef1174f72017-08-07 15:26:19 +0100943}
944
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700945static void mark_reg_unknown(struct bpf_verifier_env *env,
946 struct bpf_reg_state *regs, u32 regno)
Edward Creef1174f72017-08-07 15:26:19 +0100947{
948 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700949 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800950 /* Something bad happened, let's kill all regs except FP */
951 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100952 __mark_reg_not_init(regs + regno);
953 return;
954 }
955 __mark_reg_unknown(regs + regno);
956}
957
958static void __mark_reg_not_init(struct bpf_reg_state *reg)
959{
960 __mark_reg_unknown(reg);
961 reg->type = NOT_INIT;
962}
963
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700964static void mark_reg_not_init(struct bpf_verifier_env *env,
965 struct bpf_reg_state *regs, u32 regno)
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200966{
Edward Creef1174f72017-08-07 15:26:19 +0100967 if (WARN_ON(regno >= MAX_BPF_REG)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700968 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
Alexei Starovoitov19ceb412017-11-30 21:31:37 -0800969 /* Something bad happened, let's kill all regs except FP */
970 for (regno = 0; regno < BPF_REG_FP; regno++)
Edward Creef1174f72017-08-07 15:26:19 +0100971 __mark_reg_not_init(regs + regno);
972 return;
973 }
974 __mark_reg_not_init(regs + regno);
Daniel Borkmanna9789ef2017-05-25 01:05:06 +0200975}
976
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700977static void init_reg_state(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800978 struct bpf_func_state *state)
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700979{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800980 struct bpf_reg_state *regs = state->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700981 int i;
982
Edward Creedc503a82017-08-15 20:34:35 +0100983 for (i = 0; i < MAX_BPF_REG; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700984 mark_reg_not_init(env, regs, i);
Edward Creedc503a82017-08-15 20:34:35 +0100985 regs[i].live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +0100986 regs[i].parent = NULL;
Edward Creedc503a82017-08-15 20:34:35 +0100987 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700988
989 /* frame pointer */
Edward Creef1174f72017-08-07 15:26:19 +0100990 regs[BPF_REG_FP].type = PTR_TO_STACK;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700991 mark_reg_known_zero(env, regs, BPF_REG_FP);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800992 regs[BPF_REG_FP].frameno = state->frameno;
Alexei Starovoitov17a52672014-09-26 00:17:06 -0700993
994 /* 1st arg to a function */
995 regs[BPF_REG_1].type = PTR_TO_CTX;
Jakub Kicinski61bd5212017-10-09 10:30:11 -0700996 mark_reg_known_zero(env, regs, BPF_REG_1);
Daniel Borkmann6760bf22016-12-18 01:52:59 +0100997}
998
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800999#define BPF_MAIN_FUNC (-1)
1000static void init_func_state(struct bpf_verifier_env *env,
1001 struct bpf_func_state *state,
1002 int callsite, int frameno, int subprogno)
1003{
1004 state->callsite = callsite;
1005 state->frameno = frameno;
1006 state->subprogno = subprogno;
1007 init_reg_state(env, state);
1008}
1009
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001010enum reg_arg_type {
1011 SRC_OP, /* register is used as source operand */
1012 DST_OP, /* register is used as destination operand */
1013 DST_OP_NO_MARK /* same as above, check only, don't mark */
1014};
1015
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001016static int cmp_subprogs(const void *a, const void *b)
1017{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001018 return ((struct bpf_subprog_info *)a)->start -
1019 ((struct bpf_subprog_info *)b)->start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001020}
1021
1022static int find_subprog(struct bpf_verifier_env *env, int off)
1023{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001024 struct bpf_subprog_info *p;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001025
Jiong Wang9c8105b2018-05-02 16:17:18 -04001026 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1027 sizeof(env->subprog_info[0]), cmp_subprogs);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001028 if (!p)
1029 return -ENOENT;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001030 return p - env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001031
1032}
1033
1034static int add_subprog(struct bpf_verifier_env *env, int off)
1035{
1036 int insn_cnt = env->prog->len;
1037 int ret;
1038
1039 if (off >= insn_cnt || off < 0) {
1040 verbose(env, "call to invalid destination\n");
1041 return -EINVAL;
1042 }
1043 ret = find_subprog(env, off);
1044 if (ret >= 0)
1045 return 0;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001046 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001047 verbose(env, "too many subprograms\n");
1048 return -E2BIG;
1049 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001050 env->subprog_info[env->subprog_cnt++].start = off;
1051 sort(env->subprog_info, env->subprog_cnt,
1052 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001053 return 0;
1054}
1055
1056static int check_subprogs(struct bpf_verifier_env *env)
1057{
1058 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001059 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001060 struct bpf_insn *insn = env->prog->insnsi;
1061 int insn_cnt = env->prog->len;
1062
Jiong Wangf910cef2018-05-02 16:17:17 -04001063 /* Add entry function. */
1064 ret = add_subprog(env, 0);
1065 if (ret < 0)
1066 return ret;
1067
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001068 /* determine subprog starts. The end is one before the next starts */
1069 for (i = 0; i < insn_cnt; i++) {
1070 if (insn[i].code != (BPF_JMP | BPF_CALL))
1071 continue;
1072 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1073 continue;
1074 if (!env->allow_ptr_leaks) {
1075 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1076 return -EPERM;
1077 }
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001078 ret = add_subprog(env, i + insn[i].imm + 1);
1079 if (ret < 0)
1080 return ret;
1081 }
1082
Jiong Wang4cb3d992018-05-02 16:17:19 -04001083 /* Add a fake 'exit' subprog which could simplify subprog iteration
1084 * logic. 'subprog_cnt' should not be increased.
1085 */
1086 subprog[env->subprog_cnt].start = insn_cnt;
1087
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001088 if (env->log.level > 1)
1089 for (i = 0; i < env->subprog_cnt; i++)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001090 verbose(env, "func#%d @%d\n", i, subprog[i].start);
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001091
1092 /* now check that all jumps are within the same subprog */
Jiong Wang4cb3d992018-05-02 16:17:19 -04001093 subprog_start = subprog[cur_subprog].start;
1094 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001095 for (i = 0; i < insn_cnt; i++) {
1096 u8 code = insn[i].code;
1097
Jiong Wang092ed092019-01-26 12:26:01 -05001098 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001099 goto next;
1100 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1101 goto next;
1102 off = i + insn[i].off + 1;
1103 if (off < subprog_start || off >= subprog_end) {
1104 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1105 return -EINVAL;
1106 }
1107next:
1108 if (i == subprog_end - 1) {
1109 /* to avoid fall-through from one subprog into another
1110 * the last insn of the subprog should be either exit
1111 * or unconditional jump back
1112 */
1113 if (code != (BPF_JMP | BPF_EXIT) &&
1114 code != (BPF_JMP | BPF_JA)) {
1115 verbose(env, "last insn is not an exit or jmp\n");
1116 return -EINVAL;
1117 }
1118 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04001119 cur_subprog++;
1120 if (cur_subprog < env->subprog_cnt)
Jiong Wang9c8105b2018-05-02 16:17:18 -04001121 subprog_end = subprog[cur_subprog + 1].start;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08001122 }
1123 }
1124 return 0;
1125}
1126
Edward Cree679c7822018-08-22 20:02:19 +01001127/* Parentage chain of this register (or stack slot) should take care of all
1128 * issues like callee-saved registers, stack slot allocation time, etc.
1129 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001130static int mark_reg_read(struct bpf_verifier_env *env,
Edward Cree679c7822018-08-22 20:02:19 +01001131 const struct bpf_reg_state *state,
1132 struct bpf_reg_state *parent)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001133{
1134 bool writes = parent == state->parent; /* Observe write marks */
Edward Creedc503a82017-08-15 20:34:35 +01001135
1136 while (parent) {
1137 /* if read wasn't screened by an earlier write ... */
Edward Cree679c7822018-08-22 20:02:19 +01001138 if (writes && state->live & REG_LIVE_WRITTEN)
Edward Creedc503a82017-08-15 20:34:35 +01001139 break;
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08001140 if (parent->live & REG_LIVE_DONE) {
1141 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1142 reg_type_str[parent->type],
1143 parent->var_off.value, parent->off);
1144 return -EFAULT;
1145 }
Edward Creedc503a82017-08-15 20:34:35 +01001146 /* ... then we depend on parent's value */
Edward Cree679c7822018-08-22 20:02:19 +01001147 parent->live |= REG_LIVE_READ;
Edward Creedc503a82017-08-15 20:34:35 +01001148 state = parent;
1149 parent = state->parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001150 writes = true;
Edward Creedc503a82017-08-15 20:34:35 +01001151 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001152 return 0;
Edward Creedc503a82017-08-15 20:34:35 +01001153}
1154
1155static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001156 enum reg_arg_type t)
1157{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001158 struct bpf_verifier_state *vstate = env->cur_state;
1159 struct bpf_func_state *state = vstate->frame[vstate->curframe];
1160 struct bpf_reg_state *regs = state->regs;
Edward Creedc503a82017-08-15 20:34:35 +01001161
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001162 if (regno >= MAX_BPF_REG) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001163 verbose(env, "R%d is invalid\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001164 return -EINVAL;
1165 }
1166
1167 if (t == SRC_OP) {
1168 /* check whether register used as source operand can be read */
1169 if (regs[regno].type == NOT_INIT) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001170 verbose(env, "R%d !read_ok\n", regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001171 return -EACCES;
1172 }
Edward Cree679c7822018-08-22 20:02:19 +01001173 /* We don't need to worry about FP liveness because it's read-only */
1174 if (regno != BPF_REG_FP)
1175 return mark_reg_read(env, &regs[regno],
1176 regs[regno].parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001177 } else {
1178 /* check whether register used as dest operand can be written to */
1179 if (regno == BPF_REG_FP) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001180 verbose(env, "frame pointer is read only\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001181 return -EACCES;
1182 }
Edward Creedc503a82017-08-15 20:34:35 +01001183 regs[regno].live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001184 if (t == DST_OP)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001185 mark_reg_unknown(env, regs, regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001186 }
1187 return 0;
1188}
1189
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001190static bool is_spillable_regtype(enum bpf_reg_type type)
1191{
1192 switch (type) {
1193 case PTR_TO_MAP_VALUE:
1194 case PTR_TO_MAP_VALUE_OR_NULL:
1195 case PTR_TO_STACK:
1196 case PTR_TO_CTX:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001197 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001198 case PTR_TO_PACKET_META:
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001199 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07001200 case PTR_TO_FLOW_KEYS:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001201 case CONST_PTR_TO_MAP:
Joe Stringerc64b7982018-10-02 13:35:33 -07001202 case PTR_TO_SOCKET:
1203 case PTR_TO_SOCKET_OR_NULL:
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001204 return true;
1205 default:
1206 return false;
1207 }
1208}
1209
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001210/* Does this register contain a constant zero? */
1211static bool register_is_null(struct bpf_reg_state *reg)
1212{
1213 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1214}
1215
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001216/* check_stack_read/write functions track spill/fill of registers,
1217 * stack boundary and alignment are checked in check_mem_access()
1218 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001219static int check_stack_write(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001220 struct bpf_func_state *state, /* func where register points to */
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001221 int off, int size, int value_regno, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001222{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001223 struct bpf_func_state *cur; /* state of the current function */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001224 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001225 enum bpf_reg_type type;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001226
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001227 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
Joe Stringerfd978bf72018-10-02 13:35:35 -07001228 state->acquired_refs, true);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001229 if (err)
1230 return err;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001231 /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1232 * so it's aligned access and [off, off + size) are within stack limits
1233 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001234 if (!env->allow_ptr_leaks &&
1235 state->stack[spi].slot_type[0] == STACK_SPILL &&
1236 size != BPF_REG_SIZE) {
1237 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1238 return -EACCES;
1239 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001240
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001241 cur = env->cur_state->frame[env->cur_state->curframe];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001242 if (value_regno >= 0 &&
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001243 is_spillable_regtype((type = cur->regs[value_regno].type))) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001244
1245 /* register containing pointer is being spilled into stack */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001246 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001247 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001248 return -EACCES;
1249 }
1250
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001251 if (state != cur && type == PTR_TO_STACK) {
1252 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1253 return -EINVAL;
1254 }
1255
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001256 /* save register state */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001257 state->stack[spi].spilled_ptr = cur->regs[value_regno];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001258 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001259
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001260 for (i = 0; i < BPF_REG_SIZE; i++) {
1261 if (state->stack[spi].slot_type[i] == STACK_MISC &&
1262 !env->allow_ptr_leaks) {
1263 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1264 int soff = (-spi - 1) * BPF_REG_SIZE;
1265
1266 /* detected reuse of integer stack slot with a pointer
1267 * which means either llvm is reusing stack slot or
1268 * an attacker is trying to exploit CVE-2018-3639
1269 * (speculative store bypass)
1270 * Have to sanitize that slot with preemptive
1271 * store of zero.
1272 */
1273 if (*poff && *poff != soff) {
1274 /* disallow programs where single insn stores
1275 * into two different stack slots, since verifier
1276 * cannot sanitize them
1277 */
1278 verbose(env,
1279 "insn %d cannot access two stack slots fp%d and fp%d",
1280 insn_idx, *poff, soff);
1281 return -EINVAL;
1282 }
1283 *poff = soff;
1284 }
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001285 state->stack[spi].slot_type[i] = STACK_SPILL;
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07001286 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001287 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001288 u8 type = STACK_MISC;
1289
Edward Cree679c7822018-08-22 20:02:19 +01001290 /* regular write of data into stack destroys any spilled ptr */
1291 state->stack[spi].spilled_ptr.type = NOT_INIT;
Jiong Wang0bae2d42018-12-15 03:34:40 -05001292 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1293 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1294 for (i = 0; i < BPF_REG_SIZE; i++)
1295 state->stack[spi].slot_type[i] = STACK_MISC;
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001296
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001297 /* only mark the slot as written if all 8 bytes were written
1298 * otherwise read propagation may incorrectly stop too soon
1299 * when stack slots are partially written.
1300 * This heuristic means that read propagation will be
1301 * conservative, since it will add reg_live_read marks
1302 * to stack slots all the way to first state when programs
1303 * writes+reads less than 8 bytes
1304 */
1305 if (size == BPF_REG_SIZE)
1306 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1307
1308 /* when we zero initialize stack slots mark them as such */
1309 if (value_regno >= 0 &&
1310 register_is_null(&cur->regs[value_regno]))
1311 type = STACK_ZERO;
1312
Jiong Wang0bae2d42018-12-15 03:34:40 -05001313 /* Mark slots affected by this stack write. */
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001314 for (i = 0; i < size; i++)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001315 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001316 type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001317 }
1318 return 0;
1319}
1320
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001321static int check_stack_read(struct bpf_verifier_env *env,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001322 struct bpf_func_state *reg_state /* func where register points to */,
1323 int off, int size, int value_regno)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001324{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001325 struct bpf_verifier_state *vstate = env->cur_state;
1326 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001327 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1328 u8 *stype;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001329
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001330 if (reg_state->allocated_stack <= slot) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001331 verbose(env, "invalid read from stack off %d+0 size %d\n",
1332 off, size);
1333 return -EACCES;
1334 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001335 stype = reg_state->stack[spi].slot_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001336
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001337 if (stype[0] == STACK_SPILL) {
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001338 if (size != BPF_REG_SIZE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001339 verbose(env, "invalid size of register spill\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001340 return -EACCES;
1341 }
Alexei Starovoitov9c3997602014-10-28 15:11:41 -07001342 for (i = 1; i < BPF_REG_SIZE; i++) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001343 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001344 verbose(env, "corrupted spill memory\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001345 return -EACCES;
1346 }
1347 }
1348
Edward Creedc503a82017-08-15 20:34:35 +01001349 if (value_regno >= 0) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001350 /* restore register state from stack */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001351 state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
Alexei Starovoitov2f18f622017-11-30 21:31:38 -08001352 /* mark reg as written since spilled pointer state likely
1353 * has its liveness marks cleared by is_state_visited()
1354 * which resets stack/reg liveness for state transitions
1355 */
1356 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
Edward Creedc503a82017-08-15 20:34:35 +01001357 }
Edward Cree679c7822018-08-22 20:02:19 +01001358 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1359 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001360 return 0;
1361 } else {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001362 int zeros = 0;
1363
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001364 for (i = 0; i < size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001365 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1366 continue;
1367 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1368 zeros++;
1369 continue;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001370 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001371 verbose(env, "invalid read from stack off %d+%d size %d\n",
1372 off, i, size);
1373 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001374 }
Edward Cree679c7822018-08-22 20:02:19 +01001375 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1376 reg_state->stack[spi].spilled_ptr.parent);
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08001377 if (value_regno >= 0) {
1378 if (zeros == size) {
1379 /* any size read into register is zero extended,
1380 * so the whole register == const_zero
1381 */
1382 __mark_reg_const_zero(&state->regs[value_regno]);
1383 } else {
1384 /* have read misc data from the stack */
1385 mark_reg_unknown(env, state->regs, value_regno);
1386 }
1387 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1388 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001389 return 0;
1390 }
1391}
1392
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001393static int check_stack_access(struct bpf_verifier_env *env,
1394 const struct bpf_reg_state *reg,
1395 int off, int size)
1396{
1397 /* Stack accesses must be at a fixed offset, so that we
1398 * can determine what type of data were returned. See
1399 * check_stack_read().
1400 */
1401 if (!tnum_is_const(reg->var_off)) {
1402 char tn_buf[48];
1403
1404 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1405 verbose(env, "variable stack access var_off=%s off=%d size=%d",
1406 tn_buf, off, size);
1407 return -EACCES;
1408 }
1409
1410 if (off >= 0 || off < -MAX_BPF_STACK) {
1411 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1412 return -EACCES;
1413 }
1414
1415 return 0;
1416}
1417
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001418/* check read/write into map element returned by bpf_map_lookup_elem() */
Edward Creef1174f72017-08-07 15:26:19 +01001419static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001420 int size, bool zero_size_allowed)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001421{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001422 struct bpf_reg_state *regs = cur_regs(env);
1423 struct bpf_map *map = regs[regno].map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001424
Yonghong Song9fd29c02017-11-12 14:49:09 -08001425 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1426 off + size > map->value_size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001427 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001428 map->value_size, off, size);
1429 return -EACCES;
1430 }
1431 return 0;
1432}
1433
Edward Creef1174f72017-08-07 15:26:19 +01001434/* check read/write into a map element with possible variable offset */
1435static int check_map_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001436 int off, int size, bool zero_size_allowed)
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001437{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001438 struct bpf_verifier_state *vstate = env->cur_state;
1439 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001440 struct bpf_reg_state *reg = &state->regs[regno];
1441 int err;
1442
Edward Creef1174f72017-08-07 15:26:19 +01001443 /* We may have adjusted the register to this map value, so we
1444 * need to try adding each of min_value and max_value to off
1445 * to make sure our theoretical access will be safe.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001446 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001447 if (env->log.level)
1448 print_verifier_state(env, state);
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001449
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001450 /* The minimum value is only important with signed
1451 * comparisons where we can't assume the floor of a
1452 * value is 0. If we are using signed variables for our
1453 * index'es we need to make sure that whatever we use
1454 * will have a set floor within our range.
1455 */
Daniel Borkmannb7137c42019-01-03 00:58:33 +01001456 if (reg->smin_value < 0 &&
1457 (reg->smin_value == S64_MIN ||
1458 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1459 reg->smin_value + off < 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001460 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 -08001461 regno);
1462 return -EACCES;
1463 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001464 err = __check_map_access(env, regno, reg->smin_value + off, size,
1465 zero_size_allowed);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001466 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001467 verbose(env, "R%d min value is outside of the array range\n",
1468 regno);
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001469 return err;
1470 }
1471
Edward Creeb03c9f92017-08-07 15:26:36 +01001472 /* If we haven't set a max value then we need to bail since we can't be
1473 * sure we won't do bad things.
1474 * If reg->umax_value + off could overflow, treat that as unbounded too.
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001475 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001476 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001477 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 -08001478 regno);
1479 return -EACCES;
1480 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001481 err = __check_map_access(env, regno, reg->umax_value + off, size,
1482 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001483 if (err)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001484 verbose(env, "R%d max value is outside of the array range\n",
1485 regno);
Edward Creef1174f72017-08-07 15:26:19 +01001486 return err;
Gianluca Borellodbcfe5f2017-01-09 10:19:46 -08001487}
1488
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001489#define MAX_PACKET_OFF 0xffff
1490
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001491static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001492 const struct bpf_call_arg_meta *meta,
1493 enum bpf_access_type t)
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001494{
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001495 switch (env->prog->type) {
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001496 /* Program types only with direct read access go here! */
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001497 case BPF_PROG_TYPE_LWT_IN:
1498 case BPF_PROG_TYPE_LWT_OUT:
Mathieu Xhonneux004d4b22018-05-20 14:58:16 +01001499 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07001500 case BPF_PROG_TYPE_SK_REUSEPORT:
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001501 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Daniel Borkmannd5563d32018-10-24 22:05:46 +02001502 case BPF_PROG_TYPE_CGROUP_SKB:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001503 if (t == BPF_WRITE)
1504 return false;
Alexander Alemayhu7e57fbb2017-02-14 00:02:35 +01001505 /* fallthrough */
Daniel Borkmann5d66fa72018-10-24 22:05:45 +02001506
1507 /* Program types with direct read + write access go here! */
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001508 case BPF_PROG_TYPE_SCHED_CLS:
1509 case BPF_PROG_TYPE_SCHED_ACT:
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001510 case BPF_PROG_TYPE_XDP:
Thomas Graf3a0af8f2016-11-30 17:10:10 +01001511 case BPF_PROG_TYPE_LWT_XMIT:
John Fastabend8a31db52017-08-15 22:33:09 -07001512 case BPF_PROG_TYPE_SK_SKB:
John Fastabend4f738ad2018-03-18 12:57:10 -07001513 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann36bbef52016-09-20 00:26:13 +02001514 if (meta)
1515 return meta->pkt_access;
1516
1517 env->seen_direct_write = true;
Brenden Blanco4acf6c02016-07-19 12:16:56 -07001518 return true;
1519 default:
1520 return false;
1521 }
1522}
1523
Edward Creef1174f72017-08-07 15:26:19 +01001524static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001525 int off, int size, bool zero_size_allowed)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001526{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001527 struct bpf_reg_state *regs = cur_regs(env);
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01001528 struct bpf_reg_state *reg = &regs[regno];
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001529
Yonghong Song9fd29c02017-11-12 14:49:09 -08001530 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1531 (u64)off + size > reg->range) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001532 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 -07001533 off, size, regno, reg->id, reg->off, reg->range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001534 return -EACCES;
1535 }
1536 return 0;
1537}
1538
Edward Creef1174f72017-08-07 15:26:19 +01001539static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
Yonghong Song9fd29c02017-11-12 14:49:09 -08001540 int size, bool zero_size_allowed)
Edward Creef1174f72017-08-07 15:26:19 +01001541{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001542 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01001543 struct bpf_reg_state *reg = &regs[regno];
1544 int err;
1545
1546 /* We may have added a variable offset to the packet pointer; but any
1547 * reg->range we have comes after that. We are only checking the fixed
1548 * offset.
1549 */
1550
1551 /* We don't allow negative numbers, because we aren't tracking enough
1552 * detail to prove they're safe.
1553 */
Edward Creeb03c9f92017-08-07 15:26:36 +01001554 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001555 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 +01001556 regno);
1557 return -EACCES;
1558 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08001559 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01001560 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001561 verbose(env, "R%d offset is outside of the packet\n", regno);
Edward Creef1174f72017-08-07 15:26:19 +01001562 return err;
1563 }
Jiong Wange6478152018-11-08 04:08:42 -05001564
1565 /* __check_packet_access has made sure "off + size - 1" is within u16.
1566 * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1567 * otherwise find_good_pkt_pointers would have refused to set range info
1568 * that __check_packet_access would have rejected this pkt access.
1569 * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1570 */
1571 env->prog->aux->max_pkt_offset =
1572 max_t(u32, env->prog->aux->max_pkt_offset,
1573 off + reg->umax_value + size - 1);
1574
Edward Creef1174f72017-08-07 15:26:19 +01001575 return err;
1576}
1577
1578/* check access to 'struct bpf_context' fields. Supports fixed offsets only */
Yonghong Song31fd8582017-06-13 15:52:13 -07001579static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001580 enum bpf_access_type t, enum bpf_reg_type *reg_type)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001581{
Daniel Borkmannf96da092017-07-02 02:13:27 +02001582 struct bpf_insn_access_aux info = {
1583 .reg_type = *reg_type,
1584 };
Yonghong Song31fd8582017-06-13 15:52:13 -07001585
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001586 if (env->ops->is_valid_access &&
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001587 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02001588 /* A non zero info.ctx_field_size indicates that this field is a
1589 * candidate for later verifier transformation to load the whole
1590 * field and then apply a mask when accessed with a narrower
1591 * access than actual ctx access size. A zero info.ctx_field_size
1592 * will only allow for whole field access and rejects any other
1593 * type of narrower access.
Yonghong Song31fd8582017-06-13 15:52:13 -07001594 */
Yonghong Song23994632017-06-22 15:07:39 -07001595 *reg_type = info.reg_type;
Yonghong Song31fd8582017-06-13 15:52:13 -07001596
Jakub Kicinski4f9218a2017-10-16 16:40:55 -07001597 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001598 /* remember the offset of last byte accessed in ctx */
1599 if (env->prog->aux->max_ctx_offset < off + size)
1600 env->prog->aux->max_ctx_offset = off + size;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001601 return 0;
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07001602 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001603
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001604 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001605 return -EACCES;
1606}
1607
Petar Penkovd58e4682018-09-14 07:46:18 -07001608static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1609 int size)
1610{
1611 if (size < 0 || off < 0 ||
1612 (u64)off + size > sizeof(struct bpf_flow_keys)) {
1613 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1614 off, size);
1615 return -EACCES;
1616 }
1617 return 0;
1618}
1619
Joe Stringerc64b7982018-10-02 13:35:33 -07001620static int check_sock_access(struct bpf_verifier_env *env, u32 regno, int off,
1621 int size, enum bpf_access_type t)
1622{
1623 struct bpf_reg_state *regs = cur_regs(env);
1624 struct bpf_reg_state *reg = &regs[regno];
1625 struct bpf_insn_access_aux info;
1626
1627 if (reg->smin_value < 0) {
1628 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1629 regno);
1630 return -EACCES;
1631 }
1632
1633 if (!bpf_sock_is_valid_access(off, size, t, &info)) {
1634 verbose(env, "invalid bpf_sock access off=%d size=%d\n",
1635 off, size);
1636 return -EACCES;
1637 }
1638
1639 return 0;
1640}
1641
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001642static bool __is_pointer_value(bool allow_ptr_leaks,
1643 const struct bpf_reg_state *reg)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001644{
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001645 if (allow_ptr_leaks)
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001646 return false;
1647
Edward Creef1174f72017-08-07 15:26:19 +01001648 return reg->type != SCALAR_VALUE;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001649}
1650
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001651static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1652{
1653 return cur_regs(env) + regno;
1654}
1655
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001656static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1657{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001658 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02001659}
1660
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001661static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1662{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001663 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001664
Joe Stringerfd978bf72018-10-02 13:35:35 -07001665 return reg->type == PTR_TO_CTX ||
1666 reg->type == PTR_TO_SOCKET;
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01001667}
1668
Daniel Borkmannca369602018-02-23 22:29:05 +01001669static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1670{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02001671 const struct bpf_reg_state *reg = reg_state(env, regno);
Daniel Borkmannca369602018-02-23 22:29:05 +01001672
1673 return type_is_pkt_pointer(reg->type);
1674}
1675
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02001676static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1677{
1678 const struct bpf_reg_state *reg = reg_state(env, regno);
1679
1680 /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1681 return reg->type == PTR_TO_FLOW_KEYS;
1682}
1683
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001684static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1685 const struct bpf_reg_state *reg,
David S. Millerd1174412017-05-10 11:22:52 -07001686 int off, int size, bool strict)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001687{
Edward Creef1174f72017-08-07 15:26:19 +01001688 struct tnum reg_off;
David S. Millere07b98d2017-05-10 11:38:07 -07001689 int ip_align;
David S. Millerd1174412017-05-10 11:22:52 -07001690
1691 /* Byte size accesses are always allowed. */
1692 if (!strict || size == 1)
1693 return 0;
1694
David S. Millere4eda882017-05-22 12:27:07 -04001695 /* For platforms that do not have a Kconfig enabling
1696 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1697 * NET_IP_ALIGN is universally set to '2'. And on platforms
1698 * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1699 * to this code only in strict mode where we want to emulate
1700 * the NET_IP_ALIGN==2 checking. Therefore use an
1701 * unconditional IP align value of '2'.
David S. Millere07b98d2017-05-10 11:38:07 -07001702 */
David S. Millere4eda882017-05-22 12:27:07 -04001703 ip_align = 2;
Edward Creef1174f72017-08-07 15:26:19 +01001704
1705 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1706 if (!tnum_is_aligned(reg_off, size)) {
1707 char tn_buf[48];
1708
1709 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001710 verbose(env,
1711 "misaligned packet access off %d+%s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001712 ip_align, tn_buf, reg->off, off, size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001713 return -EACCES;
1714 }
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001715
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001716 return 0;
1717}
1718
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001719static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1720 const struct bpf_reg_state *reg,
Edward Creef1174f72017-08-07 15:26:19 +01001721 const char *pointer_desc,
1722 int off, int size, bool strict)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001723{
Edward Creef1174f72017-08-07 15:26:19 +01001724 struct tnum reg_off;
1725
1726 /* Byte size accesses are always allowed. */
1727 if (!strict || size == 1)
1728 return 0;
1729
1730 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1731 if (!tnum_is_aligned(reg_off, size)) {
1732 char tn_buf[48];
1733
1734 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001735 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
Edward Creef1174f72017-08-07 15:26:19 +01001736 pointer_desc, tn_buf, reg->off, off, size);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001737 return -EACCES;
1738 }
1739
1740 return 0;
1741}
1742
David S. Millere07b98d2017-05-10 11:38:07 -07001743static int check_ptr_alignment(struct bpf_verifier_env *env,
Daniel Borkmannca369602018-02-23 22:29:05 +01001744 const struct bpf_reg_state *reg, int off,
1745 int size, bool strict_alignment_once)
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001746{
Daniel Borkmannca369602018-02-23 22:29:05 +01001747 bool strict = env->strict_alignment || strict_alignment_once;
Edward Creef1174f72017-08-07 15:26:19 +01001748 const char *pointer_desc = "";
David S. Millerd1174412017-05-10 11:22:52 -07001749
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001750 switch (reg->type) {
1751 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001752 case PTR_TO_PACKET_META:
1753 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1754 * right in front, treat it the very same way.
1755 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001756 return check_pkt_ptr_alignment(env, reg, off, size, strict);
Petar Penkovd58e4682018-09-14 07:46:18 -07001757 case PTR_TO_FLOW_KEYS:
1758 pointer_desc = "flow keys ";
1759 break;
Edward Creef1174f72017-08-07 15:26:19 +01001760 case PTR_TO_MAP_VALUE:
1761 pointer_desc = "value ";
1762 break;
1763 case PTR_TO_CTX:
1764 pointer_desc = "context ";
1765 break;
1766 case PTR_TO_STACK:
1767 pointer_desc = "stack ";
Jann Horna5ec6ae2017-12-18 20:11:58 -08001768 /* The stack spill tracking logic in check_stack_write()
1769 * and check_stack_read() relies on stack accesses being
1770 * aligned.
1771 */
1772 strict = true;
Edward Creef1174f72017-08-07 15:26:19 +01001773 break;
Joe Stringerc64b7982018-10-02 13:35:33 -07001774 case PTR_TO_SOCKET:
1775 pointer_desc = "sock ";
1776 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001777 default:
Edward Creef1174f72017-08-07 15:26:19 +01001778 break;
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001779 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001780 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1781 strict);
Daniel Borkmann79adffc2017-03-31 02:24:03 +02001782}
1783
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001784static int update_stack_depth(struct bpf_verifier_env *env,
1785 const struct bpf_func_state *func,
1786 int off)
1787{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001788 u16 stack = env->subprog_info[func->subprogno].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001789
1790 if (stack >= -off)
1791 return 0;
1792
1793 /* update known max for given subprogram */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001794 env->subprog_info[func->subprogno].stack_depth = -off;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001795 return 0;
1796}
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001797
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001798/* starting from main bpf function walk all instructions of the function
1799 * and recursively walk all callees that given function can call.
1800 * Ignore jump and exit insns.
1801 * Since recursion is prevented by check_cfg() this algorithm
1802 * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1803 */
1804static int check_max_stack_depth(struct bpf_verifier_env *env)
1805{
Jiong Wang9c8105b2018-05-02 16:17:18 -04001806 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1807 struct bpf_subprog_info *subprog = env->subprog_info;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001808 struct bpf_insn *insn = env->prog->insnsi;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001809 int ret_insn[MAX_CALL_FRAMES];
1810 int ret_prog[MAX_CALL_FRAMES];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001811
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001812process_func:
1813 /* round up to 32-bytes, since this is granularity
1814 * of interpreter stack size
1815 */
Jiong Wang9c8105b2018-05-02 16:17:18 -04001816 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001817 if (depth > MAX_BPF_STACK) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001818 verbose(env, "combined stack size of %d calls is %d. Too large\n",
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001819 frame + 1, depth);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001820 return -EACCES;
1821 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001822continue_func:
Jiong Wang4cb3d992018-05-02 16:17:19 -04001823 subprog_end = subprog[idx + 1].start;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001824 for (; i < subprog_end; i++) {
1825 if (insn[i].code != (BPF_JMP | BPF_CALL))
1826 continue;
1827 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1828 continue;
1829 /* remember insn and function to return to */
1830 ret_insn[frame] = i + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001831 ret_prog[frame] = idx;
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001832
1833 /* find the callee */
1834 i = i + insn[i].imm + 1;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001835 idx = find_subprog(env, i);
1836 if (idx < 0) {
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001837 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1838 i);
1839 return -EFAULT;
1840 }
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001841 frame++;
1842 if (frame >= MAX_CALL_FRAMES) {
1843 WARN_ONCE(1, "verifier bug. Call stack is too deep\n");
1844 return -EFAULT;
1845 }
1846 goto process_func;
1847 }
1848 /* end of for() loop means the last insn of the 'subprog'
1849 * was reached. Doesn't matter whether it was JA or EXIT
1850 */
1851 if (frame == 0)
1852 return 0;
Jiong Wang9c8105b2018-05-02 16:17:18 -04001853 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001854 frame--;
1855 i = ret_insn[frame];
Jiong Wang9c8105b2018-05-02 16:17:18 -04001856 idx = ret_prog[frame];
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08001857 goto continue_func;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001858}
1859
David S. Miller19d28fb2018-01-11 21:27:54 -05001860#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001861static int get_callee_stack_depth(struct bpf_verifier_env *env,
1862 const struct bpf_insn *insn, int idx)
1863{
1864 int start = idx + insn->imm + 1, subprog;
1865
1866 subprog = find_subprog(env, start);
1867 if (subprog < 0) {
1868 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1869 start);
1870 return -EFAULT;
1871 }
Jiong Wang9c8105b2018-05-02 16:17:18 -04001872 return env->subprog_info[subprog].stack_depth;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001873}
David S. Miller19d28fb2018-01-11 21:27:54 -05001874#endif
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08001875
Daniel Borkmann58990d12018-06-07 17:40:03 +02001876static int check_ctx_reg(struct bpf_verifier_env *env,
1877 const struct bpf_reg_state *reg, int regno)
1878{
1879 /* Access to ctx or passing it to a helper is only allowed in
1880 * its original, unmodified form.
1881 */
1882
1883 if (reg->off) {
1884 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1885 regno, reg->off);
1886 return -EACCES;
1887 }
1888
1889 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1890 char tn_buf[48];
1891
1892 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1893 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1894 return -EACCES;
1895 }
1896
1897 return 0;
1898}
1899
Jann Horn0c17d1d2017-12-18 20:11:55 -08001900/* truncate register to smaller size (in bytes)
1901 * must be called with size < BPF_REG_SIZE
1902 */
1903static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
1904{
1905 u64 mask;
1906
1907 /* clear high bits in bit representation */
1908 reg->var_off = tnum_cast(reg->var_off, size);
1909
1910 /* fix arithmetic bounds */
1911 mask = ((u64)1 << (size * 8)) - 1;
1912 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
1913 reg->umin_value &= mask;
1914 reg->umax_value &= mask;
1915 } else {
1916 reg->umin_value = 0;
1917 reg->umax_value = mask;
1918 }
1919 reg->smin_value = reg->umin_value;
1920 reg->smax_value = reg->umax_value;
1921}
1922
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001923/* check whether memory at (regno + off) is accessible for t = (read | write)
1924 * if t==write, value_regno is a register which value is stored into memory
1925 * if t==read, value_regno is a register which will receive the value from memory
1926 * if t==write && value_regno==-1, some unknown value is stored into memory
1927 * if t==read && value_regno==-1, don't care what we read from memory
1928 */
Daniel Borkmannca369602018-02-23 22:29:05 +01001929static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
1930 int off, int bpf_size, enum bpf_access_type t,
1931 int value_regno, bool strict_alignment_once)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001932{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001933 struct bpf_reg_state *regs = cur_regs(env);
1934 struct bpf_reg_state *reg = regs + regno;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001935 struct bpf_func_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001936 int size, err = 0;
1937
1938 size = bpf_size_to_bytes(bpf_size);
1939 if (size < 0)
1940 return size;
1941
Edward Creef1174f72017-08-07 15:26:19 +01001942 /* alignment checks will add in reg->off themselves */
Daniel Borkmannca369602018-02-23 22:29:05 +01001943 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001944 if (err)
1945 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001946
Edward Creef1174f72017-08-07 15:26:19 +01001947 /* for access checks, reg->off is just part of off */
1948 off += reg->off;
1949
1950 if (reg->type == PTR_TO_MAP_VALUE) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001951 if (t == BPF_WRITE && value_regno >= 0 &&
1952 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001953 verbose(env, "R%d leaks addr into map\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001954 return -EACCES;
1955 }
Josef Bacik48461132016-09-28 10:54:32 -04001956
Yonghong Song9fd29c02017-11-12 14:49:09 -08001957 err = check_map_access(env, regno, off, size, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001958 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001959 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001960
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07001961 } else if (reg->type == PTR_TO_CTX) {
Edward Creef1174f72017-08-07 15:26:19 +01001962 enum bpf_reg_type reg_type = SCALAR_VALUE;
Alexei Starovoitov19de99f2016-06-15 18:25:38 -07001963
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001964 if (t == BPF_WRITE && value_regno >= 0 &&
1965 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001966 verbose(env, "R%d leaks addr into ctx\n", value_regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07001967 return -EACCES;
1968 }
Edward Creef1174f72017-08-07 15:26:19 +01001969
Daniel Borkmann58990d12018-06-07 17:40:03 +02001970 err = check_ctx_reg(env, reg, regno);
1971 if (err < 0)
1972 return err;
1973
Yonghong Song31fd8582017-06-13 15:52:13 -07001974 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001975 if (!err && t == BPF_READ && value_regno >= 0) {
Edward Creef1174f72017-08-07 15:26:19 +01001976 /* ctx access returns either a scalar, or a
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02001977 * PTR_TO_PACKET[_META,_END]. In the latter
1978 * case, we know the offset is zero.
Edward Creef1174f72017-08-07 15:26:19 +01001979 */
1980 if (reg_type == SCALAR_VALUE)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001981 mark_reg_unknown(env, regs, value_regno);
Edward Creef1174f72017-08-07 15:26:19 +01001982 else
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001983 mark_reg_known_zero(env, regs,
Jakub Kicinski61bd5212017-10-09 10:30:11 -07001984 value_regno);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001985 regs[value_regno].type = reg_type;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07001986 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07001987
Edward Creef1174f72017-08-07 15:26:19 +01001988 } else if (reg->type == PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01001989 off += reg->var_off.value;
Daniel Borkmanne4298d22019-01-03 00:58:31 +01001990 err = check_stack_access(env, reg, off, size);
1991 if (err)
1992 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001993
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08001994 state = func(env, reg);
1995 err = update_stack_depth(env, state, off);
1996 if (err)
1997 return err;
Alexei Starovoitov87266792017-05-30 13:31:29 -07001998
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07001999 if (t == BPF_WRITE)
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002000 err = check_stack_write(env, state, off, size,
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07002001 value_regno, insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002002 else
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002003 err = check_stack_read(env, state, off, size,
2004 value_regno);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002005 } else if (reg_is_pkt_pointer(reg)) {
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002006 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002007 verbose(env, "cannot write into packet\n");
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002008 return -EACCES;
2009 }
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002010 if (t == BPF_WRITE && value_regno >= 0 &&
2011 is_pointer_value(env, value_regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002012 verbose(env, "R%d leaks addr into packet\n",
2013 value_regno);
Brenden Blanco4acf6c02016-07-19 12:16:56 -07002014 return -EACCES;
2015 }
Yonghong Song9fd29c02017-11-12 14:49:09 -08002016 err = check_packet_access(env, regno, off, size, false);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002017 if (!err && t == BPF_READ && value_regno >= 0)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002018 mark_reg_unknown(env, regs, value_regno);
Petar Penkovd58e4682018-09-14 07:46:18 -07002019 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2020 if (t == BPF_WRITE && value_regno >= 0 &&
2021 is_pointer_value(env, value_regno)) {
2022 verbose(env, "R%d leaks addr into flow keys\n",
2023 value_regno);
2024 return -EACCES;
2025 }
2026
2027 err = check_flow_keys_access(env, off, size);
2028 if (!err && t == BPF_READ && value_regno >= 0)
2029 mark_reg_unknown(env, regs, value_regno);
Joe Stringerc64b7982018-10-02 13:35:33 -07002030 } else if (reg->type == PTR_TO_SOCKET) {
2031 if (t == BPF_WRITE) {
2032 verbose(env, "cannot write into socket\n");
2033 return -EACCES;
2034 }
2035 err = check_sock_access(env, regno, off, size, t);
2036 if (!err && value_regno >= 0)
2037 mark_reg_unknown(env, regs, value_regno);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002038 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002039 verbose(env, "R%d invalid mem access '%s'\n", regno,
2040 reg_type_str[reg->type]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002041 return -EACCES;
2042 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002043
Edward Creef1174f72017-08-07 15:26:19 +01002044 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002045 regs[value_regno].type == SCALAR_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002046 /* b/h/w load zero-extends, mark upper bits as known 0 */
Jann Horn0c17d1d2017-12-18 20:11:55 -08002047 coerce_reg_to_size(&regs[value_regno], size);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002048 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002049 return err;
2050}
2051
Yonghong Song31fd8582017-06-13 15:52:13 -07002052static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002053{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002054 int err;
2055
2056 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2057 insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002058 verbose(env, "BPF_XADD uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002059 return -EINVAL;
2060 }
2061
2062 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002063 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002064 if (err)
2065 return err;
2066
2067 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01002068 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002069 if (err)
2070 return err;
2071
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002072 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002073 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
Daniel Borkmann6bdf6ab2017-06-29 03:04:59 +02002074 return -EACCES;
2075 }
2076
Daniel Borkmannca369602018-02-23 22:29:05 +01002077 if (is_ctx_reg(env, insn->dst_reg) ||
Daniel Borkmann4b5defd2018-10-21 02:09:25 +02002078 is_pkt_reg(env, insn->dst_reg) ||
2079 is_flow_key_reg(env, insn->dst_reg)) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002080 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002081 insn->dst_reg,
2082 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01002083 return -EACCES;
2084 }
2085
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002086 /* check whether atomic_add can read the memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002087 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002088 BPF_SIZE(insn->code), BPF_READ, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002089 if (err)
2090 return err;
2091
2092 /* check whether atomic_add can write into the same memory */
Yonghong Song31fd8582017-06-13 15:52:13 -07002093 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
Daniel Borkmannca369602018-02-23 22:29:05 +01002094 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002095}
2096
2097/* when register 'regno' is passed into function that will read 'access_size'
2098 * bytes from that pointer, make sure that it's within stack boundary
Edward Creef1174f72017-08-07 15:26:19 +01002099 * and all elements of stack are initialized.
2100 * Unlike most pointer bounds-checking functions, this one doesn't take an
2101 * 'off' argument, so it has to add in reg->off itself.
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002102 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002103static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
Daniel Borkmann435faee12016-04-13 00:10:51 +02002104 int access_size, bool zero_size_allowed,
2105 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002106{
Daniel Borkmann2a159c62018-10-21 02:09:24 +02002107 struct bpf_reg_state *reg = reg_state(env, regno);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002108 struct bpf_func_state *state = func(env, reg);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002109 int off, i, slot, spi;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002110
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002111 if (reg->type != PTR_TO_STACK) {
Edward Creef1174f72017-08-07 15:26:19 +01002112 /* Allow zero-byte read from NULL, regardless of pointer type */
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002113 if (zero_size_allowed && access_size == 0 &&
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002114 register_is_null(reg))
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002115 return 0;
2116
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002117 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002118 reg_type_str[reg->type],
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002119 reg_type_str[PTR_TO_STACK]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002120 return -EACCES;
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002121 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002122
Edward Creef1174f72017-08-07 15:26:19 +01002123 /* Only allow fixed-offset stack reads */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002124 if (!tnum_is_const(reg->var_off)) {
Edward Creef1174f72017-08-07 15:26:19 +01002125 char tn_buf[48];
2126
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002127 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002128 verbose(env, "invalid variable stack read R%d var_off=%s\n",
Edward Creef1174f72017-08-07 15:26:19 +01002129 regno, tn_buf);
Jann Hornea25f912017-12-18 20:11:57 -08002130 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01002131 }
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002132 off = reg->off + reg->var_off.value;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002133 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
Yonghong Song9fd29c02017-11-12 14:49:09 -08002134 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002135 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002136 regno, off, access_size);
2137 return -EACCES;
2138 }
2139
Daniel Borkmann435faee12016-04-13 00:10:51 +02002140 if (meta && meta->raw_mode) {
2141 meta->access_size = access_size;
2142 meta->regno = regno;
2143 return 0;
2144 }
2145
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002146 for (i = 0; i < access_size; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002147 u8 *stype;
2148
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002149 slot = -(off + i) - 1;
2150 spi = slot / BPF_REG_SIZE;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002151 if (state->allocated_stack <= slot)
2152 goto err;
2153 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2154 if (*stype == STACK_MISC)
2155 goto mark;
2156 if (*stype == STACK_ZERO) {
2157 /* helper can write anything into the stack */
2158 *stype = STACK_MISC;
2159 goto mark;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002160 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08002161err:
2162 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2163 off, i, access_size);
2164 return -EACCES;
2165mark:
2166 /* reading any byte out of 8-byte 'spill_slot' will cause
2167 * the whole slot to be marked as 'read'
2168 */
Edward Cree679c7822018-08-22 20:02:19 +01002169 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2170 state->stack[spi].spilled_ptr.parent);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002171 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002172 return update_stack_depth(env, state, off);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002173}
2174
Gianluca Borello06c1c042017-01-09 10:19:49 -08002175static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2176 int access_size, bool zero_size_allowed,
2177 struct bpf_call_arg_meta *meta)
2178{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002179 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Gianluca Borello06c1c042017-01-09 10:19:49 -08002180
Edward Creef1174f72017-08-07 15:26:19 +01002181 switch (reg->type) {
Gianluca Borello06c1c042017-01-09 10:19:49 -08002182 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002183 case PTR_TO_PACKET_META:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002184 return check_packet_access(env, regno, reg->off, access_size,
2185 zero_size_allowed);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002186 case PTR_TO_MAP_VALUE:
Yonghong Song9fd29c02017-11-12 14:49:09 -08002187 return check_map_access(env, regno, reg->off, access_size,
2188 zero_size_allowed);
Edward Creef1174f72017-08-07 15:26:19 +01002189 default: /* scalar_value|ptr_to_stack or invalid ptr */
Gianluca Borello06c1c042017-01-09 10:19:49 -08002190 return check_stack_boundary(env, regno, access_size,
2191 zero_size_allowed, meta);
2192 }
2193}
2194
Daniel Borkmann90133412018-01-20 01:24:29 +01002195static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2196{
2197 return type == ARG_PTR_TO_MEM ||
2198 type == ARG_PTR_TO_MEM_OR_NULL ||
2199 type == ARG_PTR_TO_UNINIT_MEM;
2200}
2201
2202static bool arg_type_is_mem_size(enum bpf_arg_type type)
2203{
2204 return type == ARG_CONST_SIZE ||
2205 type == ARG_CONST_SIZE_OR_ZERO;
2206}
2207
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002208static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002209 enum bpf_arg_type arg_type,
2210 struct bpf_call_arg_meta *meta)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002211{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002212 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002213 enum bpf_reg_type expected_type, type = reg->type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002214 int err = 0;
2215
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002216 if (arg_type == ARG_DONTCARE)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002217 return 0;
2218
Edward Creedc503a82017-08-15 20:34:35 +01002219 err = check_reg_arg(env, regno, SRC_OP);
2220 if (err)
2221 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002222
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002223 if (arg_type == ARG_ANYTHING) {
2224 if (is_pointer_value(env, regno)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002225 verbose(env, "R%d leaks addr into helper function\n",
2226 regno);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002227 return -EACCES;
2228 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002229 return 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002230 }
Daniel Borkmann80f1d682015-03-12 17:21:42 +01002231
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002232 if (type_is_pkt_pointer(type) &&
Thomas Graf3a0af8f2016-11-30 17:10:10 +01002233 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002234 verbose(env, "helper access to the packet is not allowed\n");
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002235 return -EACCES;
2236 }
2237
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002238 if (arg_type == ARG_PTR_TO_MAP_KEY ||
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002239 arg_type == ARG_PTR_TO_MAP_VALUE ||
2240 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002241 expected_type = PTR_TO_STACK;
Paul Chaignond71962f2018-04-24 15:07:54 +02002242 if (!type_is_pkt_pointer(type) && type != PTR_TO_MAP_VALUE &&
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002243 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002244 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002245 } else if (arg_type == ARG_CONST_SIZE ||
2246 arg_type == ARG_CONST_SIZE_OR_ZERO) {
Edward Creef1174f72017-08-07 15:26:19 +01002247 expected_type = SCALAR_VALUE;
2248 if (type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002249 goto err_type;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002250 } else if (arg_type == ARG_CONST_MAP_PTR) {
2251 expected_type = CONST_PTR_TO_MAP;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002252 if (type != expected_type)
2253 goto err_type;
Alexei Starovoitov608cd712015-03-26 19:53:57 -07002254 } else if (arg_type == ARG_PTR_TO_CTX) {
2255 expected_type = PTR_TO_CTX;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002256 if (type != expected_type)
2257 goto err_type;
Daniel Borkmann58990d12018-06-07 17:40:03 +02002258 err = check_ctx_reg(env, reg, regno);
2259 if (err < 0)
2260 return err;
Joe Stringerc64b7982018-10-02 13:35:33 -07002261 } else if (arg_type == ARG_PTR_TO_SOCKET) {
2262 expected_type = PTR_TO_SOCKET;
2263 if (type != expected_type)
2264 goto err_type;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002265 if (meta->ptr_id || !reg->id) {
2266 verbose(env, "verifier internal error: mismatched references meta=%d, reg=%d\n",
2267 meta->ptr_id, reg->id);
2268 return -EFAULT;
2269 }
2270 meta->ptr_id = reg->id;
Daniel Borkmann90133412018-01-20 01:24:29 +01002271 } else if (arg_type_is_mem_ptr(arg_type)) {
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002272 expected_type = PTR_TO_STACK;
2273 /* One exception here. In case function allows for NULL to be
Edward Creef1174f72017-08-07 15:26:19 +01002274 * passed in as argument, it's a SCALAR_VALUE type. Final test
Daniel Borkmann8e2fe1d92016-02-19 23:05:22 +01002275 * happens during stack boundary checking.
2276 */
Alexei Starovoitov914cb782017-11-30 21:31:40 -08002277 if (register_is_null(reg) &&
Gianluca Borellodb1ac492017-11-22 18:32:53 +00002278 arg_type == ARG_PTR_TO_MEM_OR_NULL)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002279 /* final test in check_stack_boundary() */;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002280 else if (!type_is_pkt_pointer(type) &&
2281 type != PTR_TO_MAP_VALUE &&
Edward Creef1174f72017-08-07 15:26:19 +01002282 type != expected_type)
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002283 goto err_type;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002284 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002285 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002286 verbose(env, "unsupported arg_type %d\n", arg_type);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002287 return -EFAULT;
2288 }
2289
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002290 if (arg_type == ARG_CONST_MAP_PTR) {
2291 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002292 meta->map_ptr = reg->map_ptr;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002293 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2294 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2295 * check that [key, key + map->key_size) are within
2296 * stack limits and initialized
2297 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002298 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002299 /* in function declaration map_ptr must come before
2300 * map_key, so that it's verified and known before
2301 * we have to check map_key here. Otherwise it means
2302 * that kernel subsystem misconfigured verifier
2303 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002304 verbose(env, "invalid map_ptr to access map->key\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002305 return -EACCES;
2306 }
Paul Chaignond71962f2018-04-24 15:07:54 +02002307 err = check_helper_mem_access(env, regno,
2308 meta->map_ptr->key_size, false,
2309 NULL);
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002310 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
2311 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002312 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2313 * check [value, value + map->value_size) validity
2314 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002315 if (!meta->map_ptr) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002316 /* kernel subsystem misconfigured verifier */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002317 verbose(env, "invalid map_ptr to access map->value\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002318 return -EACCES;
2319 }
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002320 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
Paul Chaignond71962f2018-04-24 15:07:54 +02002321 err = check_helper_mem_access(env, regno,
2322 meta->map_ptr->value_size, false,
Mauricio Vasquez B2ea864c2018-10-18 15:16:20 +02002323 meta);
Daniel Borkmann90133412018-01-20 01:24:29 +01002324 } else if (arg_type_is_mem_size(arg_type)) {
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002325 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002326
Yonghong Song849fa502018-04-28 22:28:09 -07002327 /* remember the mem_size which may be used later
2328 * to refine return values.
2329 */
2330 meta->msize_smax_value = reg->smax_value;
2331 meta->msize_umax_value = reg->umax_value;
2332
Edward Creef1174f72017-08-07 15:26:19 +01002333 /* The register is SCALAR_VALUE; the access check
2334 * happens using its boundaries.
Gianluca Borello06c1c042017-01-09 10:19:49 -08002335 */
Edward Creef1174f72017-08-07 15:26:19 +01002336 if (!tnum_is_const(reg->var_off))
Gianluca Borello06c1c042017-01-09 10:19:49 -08002337 /* For unprivileged variable accesses, disable raw
2338 * mode so that the program is required to
2339 * initialize all the memory that the helper could
2340 * just partially fill up.
2341 */
2342 meta = NULL;
2343
Edward Creeb03c9f92017-08-07 15:26:36 +01002344 if (reg->smin_value < 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002345 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002346 regno);
2347 return -EACCES;
2348 }
Gianluca Borello06c1c042017-01-09 10:19:49 -08002349
Edward Creeb03c9f92017-08-07 15:26:36 +01002350 if (reg->umin_value == 0) {
Edward Creef1174f72017-08-07 15:26:19 +01002351 err = check_helper_mem_access(env, regno - 1, 0,
2352 zero_size_allowed,
2353 meta);
Gianluca Borello06c1c042017-01-09 10:19:49 -08002354 if (err)
2355 return err;
Gianluca Borello06c1c042017-01-09 10:19:49 -08002356 }
Edward Creef1174f72017-08-07 15:26:19 +01002357
Edward Creeb03c9f92017-08-07 15:26:36 +01002358 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002359 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
Edward Creef1174f72017-08-07 15:26:19 +01002360 regno);
2361 return -EACCES;
2362 }
2363 err = check_helper_mem_access(env, regno - 1,
Edward Creeb03c9f92017-08-07 15:26:36 +01002364 reg->umax_value,
Edward Creef1174f72017-08-07 15:26:19 +01002365 zero_size_allowed, meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002366 }
2367
2368 return err;
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002369err_type:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002370 verbose(env, "R%d type=%s expected=%s\n", regno,
Alexei Starovoitov6841de82016-08-11 18:17:16 -07002371 reg_type_str[type], reg_type_str[expected_type]);
2372 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002373}
2374
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002375static int check_map_func_compatibility(struct bpf_verifier_env *env,
2376 struct bpf_map *map, int func_id)
Kaixu Xia35578d72015-08-06 07:02:35 +00002377{
Kaixu Xia35578d72015-08-06 07:02:35 +00002378 if (!map)
2379 return 0;
2380
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002381 /* We need a two way check, first is from map perspective ... */
2382 switch (map->map_type) {
2383 case BPF_MAP_TYPE_PROG_ARRAY:
2384 if (func_id != BPF_FUNC_tail_call)
2385 goto error;
2386 break;
2387 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2388 if (func_id != BPF_FUNC_perf_event_read &&
Yonghong Song908432c2017-10-05 09:19:20 -07002389 func_id != BPF_FUNC_perf_event_output &&
2390 func_id != BPF_FUNC_perf_event_read_value)
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002391 goto error;
2392 break;
2393 case BPF_MAP_TYPE_STACK_TRACE:
2394 if (func_id != BPF_FUNC_get_stackid)
2395 goto error;
2396 break;
Martin KaFai Lau4ed8ec52016-06-30 10:28:43 -07002397 case BPF_MAP_TYPE_CGROUP_ARRAY:
David S. Miller60747ef2016-08-18 01:17:32 -04002398 if (func_id != BPF_FUNC_skb_under_cgroup &&
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002399 func_id != BPF_FUNC_current_task_under_cgroup)
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002400 goto error;
2401 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002402 case BPF_MAP_TYPE_CGROUP_STORAGE:
Roman Gushchinb741f162018-09-28 14:45:43 +00002403 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
Roman Gushchincd339432018-08-02 14:27:24 -07002404 if (func_id != BPF_FUNC_get_local_storage)
2405 goto error;
2406 break;
John Fastabend546ac1f2017-07-17 09:28:56 -07002407 /* devmap returns a pointer to a live net_device ifindex that we cannot
2408 * allow to be modified from bpf side. So do not allow lookup elements
2409 * for now.
2410 */
2411 case BPF_MAP_TYPE_DEVMAP:
John Fastabend2ddf71e2017-07-17 09:30:02 -07002412 if (func_id != BPF_FUNC_redirect_map)
John Fastabend546ac1f2017-07-17 09:28:56 -07002413 goto error;
2414 break;
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002415 /* Restrict bpf side of cpumap and xskmap, open when use-cases
2416 * appear.
2417 */
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002418 case BPF_MAP_TYPE_CPUMAP:
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002419 case BPF_MAP_TYPE_XSKMAP:
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02002420 if (func_id != BPF_FUNC_redirect_map)
2421 goto error;
2422 break;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002423 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07002424 case BPF_MAP_TYPE_HASH_OF_MAPS:
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07002425 if (func_id != BPF_FUNC_map_lookup_elem)
2426 goto error;
Martin KaFai Lau16a43622017-08-17 18:14:43 -07002427 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002428 case BPF_MAP_TYPE_SOCKMAP:
2429 if (func_id != BPF_FUNC_sk_redirect_map &&
2430 func_id != BPF_FUNC_sock_map_update &&
John Fastabend4f738ad2018-03-18 12:57:10 -07002431 func_id != BPF_FUNC_map_delete_elem &&
2432 func_id != BPF_FUNC_msg_redirect_map)
John Fastabend174a79f2017-08-15 22:32:47 -07002433 goto error;
2434 break;
John Fastabend81110382018-05-14 10:00:17 -07002435 case BPF_MAP_TYPE_SOCKHASH:
2436 if (func_id != BPF_FUNC_sk_redirect_hash &&
2437 func_id != BPF_FUNC_sock_hash_update &&
2438 func_id != BPF_FUNC_map_delete_elem &&
2439 func_id != BPF_FUNC_msg_redirect_hash)
2440 goto error;
2441 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002442 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2443 if (func_id != BPF_FUNC_sk_select_reuseport)
2444 goto error;
2445 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002446 case BPF_MAP_TYPE_QUEUE:
2447 case BPF_MAP_TYPE_STACK:
2448 if (func_id != BPF_FUNC_map_peek_elem &&
2449 func_id != BPF_FUNC_map_pop_elem &&
2450 func_id != BPF_FUNC_map_push_elem)
2451 goto error;
2452 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002453 default:
2454 break;
2455 }
2456
2457 /* ... and second from the function itself. */
2458 switch (func_id) {
2459 case BPF_FUNC_tail_call:
2460 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2461 goto error;
Jiong Wangf910cef2018-05-02 16:17:17 -04002462 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002463 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2464 return -EINVAL;
2465 }
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002466 break;
2467 case BPF_FUNC_perf_event_read:
2468 case BPF_FUNC_perf_event_output:
Yonghong Song908432c2017-10-05 09:19:20 -07002469 case BPF_FUNC_perf_event_read_value:
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002470 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2471 goto error;
2472 break;
2473 case BPF_FUNC_get_stackid:
2474 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2475 goto error;
2476 break;
Sargun Dhillon60d20f92016-08-12 08:56:52 -07002477 case BPF_FUNC_current_task_under_cgroup:
Daniel Borkmann747ea552016-08-12 22:17:17 +02002478 case BPF_FUNC_skb_under_cgroup:
Martin KaFai Lau4a482f32016-06-30 10:28:44 -07002479 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2480 goto error;
2481 break;
John Fastabend97f91a72017-07-17 09:29:18 -07002482 case BPF_FUNC_redirect_map:
Jesper Dangaard Brouer9c270af2017-10-16 12:19:34 +02002483 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
Björn Töpelfbfc504a2018-05-02 13:01:28 +02002484 map->map_type != BPF_MAP_TYPE_CPUMAP &&
2485 map->map_type != BPF_MAP_TYPE_XSKMAP)
John Fastabend97f91a72017-07-17 09:29:18 -07002486 goto error;
2487 break;
John Fastabend174a79f2017-08-15 22:32:47 -07002488 case BPF_FUNC_sk_redirect_map:
John Fastabend4f738ad2018-03-18 12:57:10 -07002489 case BPF_FUNC_msg_redirect_map:
John Fastabend81110382018-05-14 10:00:17 -07002490 case BPF_FUNC_sock_map_update:
John Fastabend174a79f2017-08-15 22:32:47 -07002491 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2492 goto error;
2493 break;
John Fastabend81110382018-05-14 10:00:17 -07002494 case BPF_FUNC_sk_redirect_hash:
2495 case BPF_FUNC_msg_redirect_hash:
2496 case BPF_FUNC_sock_hash_update:
2497 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
John Fastabend174a79f2017-08-15 22:32:47 -07002498 goto error;
2499 break;
Roman Gushchincd339432018-08-02 14:27:24 -07002500 case BPF_FUNC_get_local_storage:
Roman Gushchinb741f162018-09-28 14:45:43 +00002501 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2502 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Roman Gushchincd339432018-08-02 14:27:24 -07002503 goto error;
2504 break;
Martin KaFai Lau2dbb9b92018-08-08 01:01:25 -07002505 case BPF_FUNC_sk_select_reuseport:
2506 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2507 goto error;
2508 break;
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002509 case BPF_FUNC_map_peek_elem:
2510 case BPF_FUNC_map_pop_elem:
2511 case BPF_FUNC_map_push_elem:
2512 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2513 map->map_type != BPF_MAP_TYPE_STACK)
2514 goto error;
2515 break;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002516 default:
2517 break;
Kaixu Xia35578d72015-08-06 07:02:35 +00002518 }
2519
2520 return 0;
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002521error:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002522 verbose(env, "cannot pass map_type %d into func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002523 map->map_type, func_id_name(func_id), func_id);
Alexei Starovoitov6aff67c2016-04-27 18:56:21 -07002524 return -EINVAL;
Kaixu Xia35578d72015-08-06 07:02:35 +00002525}
2526
Daniel Borkmann90133412018-01-20 01:24:29 +01002527static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002528{
2529 int count = 0;
2530
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002531 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002532 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002533 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002534 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002535 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002536 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002537 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002538 count++;
Alexei Starovoitov39f19ebb2017-01-09 10:19:50 -08002539 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
Daniel Borkmann435faee12016-04-13 00:10:51 +02002540 count++;
2541
Daniel Borkmann90133412018-01-20 01:24:29 +01002542 /* We only support one arg being in raw mode at the moment,
2543 * which is sufficient for the helper functions we have
2544 * right now.
2545 */
2546 return count <= 1;
2547}
2548
2549static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2550 enum bpf_arg_type arg_next)
2551{
2552 return (arg_type_is_mem_ptr(arg_curr) &&
2553 !arg_type_is_mem_size(arg_next)) ||
2554 (!arg_type_is_mem_ptr(arg_curr) &&
2555 arg_type_is_mem_size(arg_next));
2556}
2557
2558static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2559{
2560 /* bpf_xxx(..., buf, len) call will access 'len'
2561 * bytes from memory 'buf'. Both arg types need
2562 * to be paired, so make sure there's no buggy
2563 * helper function specification.
2564 */
2565 if (arg_type_is_mem_size(fn->arg1_type) ||
2566 arg_type_is_mem_ptr(fn->arg5_type) ||
2567 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2568 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2569 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2570 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2571 return false;
2572
2573 return true;
2574}
2575
Joe Stringerfd978bf72018-10-02 13:35:35 -07002576static bool check_refcount_ok(const struct bpf_func_proto *fn)
2577{
2578 int count = 0;
2579
2580 if (arg_type_is_refcounted(fn->arg1_type))
2581 count++;
2582 if (arg_type_is_refcounted(fn->arg2_type))
2583 count++;
2584 if (arg_type_is_refcounted(fn->arg3_type))
2585 count++;
2586 if (arg_type_is_refcounted(fn->arg4_type))
2587 count++;
2588 if (arg_type_is_refcounted(fn->arg5_type))
2589 count++;
2590
2591 /* We only support one arg being unreferenced at the moment,
2592 * which is sufficient for the helper functions we have right now.
2593 */
2594 return count <= 1;
2595}
2596
Daniel Borkmann90133412018-01-20 01:24:29 +01002597static int check_func_proto(const struct bpf_func_proto *fn)
2598{
2599 return check_raw_mode_ok(fn) &&
Joe Stringerfd978bf72018-10-02 13:35:35 -07002600 check_arg_pair_ok(fn) &&
2601 check_refcount_ok(fn) ? 0 : -EINVAL;
Daniel Borkmann435faee12016-04-13 00:10:51 +02002602}
2603
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002604/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2605 * are now invalid, so turn them into unknown SCALAR_VALUE.
Edward Creef1174f72017-08-07 15:26:19 +01002606 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002607static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2608 struct bpf_func_state *state)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002609{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002610 struct bpf_reg_state *regs = state->regs, *reg;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002611 int i;
2612
2613 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002614 if (reg_is_pkt_pointer_any(&regs[i]))
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002615 mark_reg_unknown(env, regs, i);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002616
Joe Stringerf3709f62018-10-02 13:35:29 -07002617 bpf_for_each_spilled_reg(i, state, reg) {
2618 if (!reg)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002619 continue;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02002620 if (reg_is_pkt_pointer_any(reg))
2621 __mark_reg_unknown(reg);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002622 }
2623}
2624
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002625static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2626{
2627 struct bpf_verifier_state *vstate = env->cur_state;
2628 int i;
2629
2630 for (i = 0; i <= vstate->curframe; i++)
2631 __clear_all_pkt_pointers(env, vstate->frame[i]);
2632}
2633
Joe Stringerfd978bf72018-10-02 13:35:35 -07002634static void release_reg_references(struct bpf_verifier_env *env,
2635 struct bpf_func_state *state, int id)
2636{
2637 struct bpf_reg_state *regs = state->regs, *reg;
2638 int i;
2639
2640 for (i = 0; i < MAX_BPF_REG; i++)
2641 if (regs[i].id == id)
2642 mark_reg_unknown(env, regs, i);
2643
2644 bpf_for_each_spilled_reg(i, state, reg) {
2645 if (!reg)
2646 continue;
2647 if (reg_is_refcounted(reg) && reg->id == id)
2648 __mark_reg_unknown(reg);
2649 }
2650}
2651
2652/* The pointer with the specified id has released its reference to kernel
2653 * resources. Identify all copies of the same pointer and clear the reference.
2654 */
2655static int release_reference(struct bpf_verifier_env *env,
2656 struct bpf_call_arg_meta *meta)
2657{
2658 struct bpf_verifier_state *vstate = env->cur_state;
2659 int i;
2660
2661 for (i = 0; i <= vstate->curframe; i++)
2662 release_reg_references(env, vstate->frame[i], meta->ptr_id);
2663
2664 return release_reference_state(env, meta->ptr_id);
2665}
2666
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002667static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2668 int *insn_idx)
2669{
2670 struct bpf_verifier_state *state = env->cur_state;
2671 struct bpf_func_state *caller, *callee;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002672 int i, err, subprog, target_insn;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002673
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002674 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002675 verbose(env, "the call stack of %d frames is too deep\n",
Alexei Starovoitovaada9ce2017-12-25 13:15:42 -08002676 state->curframe + 2);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002677 return -E2BIG;
2678 }
2679
2680 target_insn = *insn_idx + insn->imm;
2681 subprog = find_subprog(env, target_insn + 1);
2682 if (subprog < 0) {
2683 verbose(env, "verifier bug. No program starts at insn %d\n",
2684 target_insn + 1);
2685 return -EFAULT;
2686 }
2687
2688 caller = state->frame[state->curframe];
2689 if (state->frame[state->curframe + 1]) {
2690 verbose(env, "verifier bug. Frame %d already allocated\n",
2691 state->curframe + 1);
2692 return -EFAULT;
2693 }
2694
2695 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
2696 if (!callee)
2697 return -ENOMEM;
2698 state->frame[state->curframe + 1] = callee;
2699
2700 /* callee cannot access r0, r6 - r9 for reading and has to write
2701 * into its own stack before reading from it.
2702 * callee can read/write into caller's stack
2703 */
2704 init_func_state(env, callee,
2705 /* remember the callsite, it will be used by bpf_exit */
2706 *insn_idx /* callsite */,
2707 state->curframe + 1 /* frameno within this callchain */,
Jiong Wangf910cef2018-05-02 16:17:17 -04002708 subprog /* subprog number within this prog */);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002709
Joe Stringerfd978bf72018-10-02 13:35:35 -07002710 /* Transfer references to the callee */
2711 err = transfer_reference_state(callee, caller);
2712 if (err)
2713 return err;
2714
Edward Cree679c7822018-08-22 20:02:19 +01002715 /* copy r1 - r5 args that callee can access. The copy includes parent
2716 * pointers, which connects us up to the liveness chain
2717 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002718 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
2719 callee->regs[i] = caller->regs[i];
2720
Edward Cree679c7822018-08-22 20:02:19 +01002721 /* after the call registers r0 - r5 were scratched */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002722 for (i = 0; i < CALLER_SAVED_REGS; i++) {
2723 mark_reg_not_init(env, caller->regs, caller_saved[i]);
2724 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2725 }
2726
2727 /* only increment it after check_reg_arg() finished */
2728 state->curframe++;
2729
2730 /* and go analyze first insn of the callee */
2731 *insn_idx = target_insn;
2732
2733 if (env->log.level) {
2734 verbose(env, "caller:\n");
2735 print_verifier_state(env, caller);
2736 verbose(env, "callee:\n");
2737 print_verifier_state(env, callee);
2738 }
2739 return 0;
2740}
2741
2742static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
2743{
2744 struct bpf_verifier_state *state = env->cur_state;
2745 struct bpf_func_state *caller, *callee;
2746 struct bpf_reg_state *r0;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002747 int err;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002748
2749 callee = state->frame[state->curframe];
2750 r0 = &callee->regs[BPF_REG_0];
2751 if (r0->type == PTR_TO_STACK) {
2752 /* technically it's ok to return caller's stack pointer
2753 * (or caller's caller's pointer) back to the caller,
2754 * since these pointers are valid. Only current stack
2755 * pointer will be invalid as soon as function exits,
2756 * but let's be conservative
2757 */
2758 verbose(env, "cannot return stack pointer to the caller\n");
2759 return -EINVAL;
2760 }
2761
2762 state->curframe--;
2763 caller = state->frame[state->curframe];
2764 /* return to the caller whatever r0 had in the callee */
2765 caller->regs[BPF_REG_0] = *r0;
2766
Joe Stringerfd978bf72018-10-02 13:35:35 -07002767 /* Transfer references to the caller */
2768 err = transfer_reference_state(caller, callee);
2769 if (err)
2770 return err;
2771
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002772 *insn_idx = callee->callsite + 1;
2773 if (env->log.level) {
2774 verbose(env, "returning from callee:\n");
2775 print_verifier_state(env, callee);
2776 verbose(env, "to caller at %d:\n", *insn_idx);
2777 print_verifier_state(env, caller);
2778 }
2779 /* clear everything in the callee */
2780 free_func_state(callee);
2781 state->frame[state->curframe + 1] = NULL;
2782 return 0;
2783}
2784
Yonghong Song849fa502018-04-28 22:28:09 -07002785static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
2786 int func_id,
2787 struct bpf_call_arg_meta *meta)
2788{
2789 struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
2790
2791 if (ret_type != RET_INTEGER ||
2792 (func_id != BPF_FUNC_get_stack &&
2793 func_id != BPF_FUNC_probe_read_str))
2794 return;
2795
2796 ret_reg->smax_value = meta->msize_smax_value;
2797 ret_reg->umax_value = meta->msize_umax_value;
2798 __reg_deduce_bounds(ret_reg);
2799 __reg_bound_offset(ret_reg);
2800}
2801
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002802static int
2803record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
2804 int func_id, int insn_idx)
2805{
2806 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
2807
2808 if (func_id != BPF_FUNC_tail_call &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02002809 func_id != BPF_FUNC_map_lookup_elem &&
2810 func_id != BPF_FUNC_map_update_elem &&
Mauricio Vasquez Bf1a2e442018-10-18 15:16:25 +02002811 func_id != BPF_FUNC_map_delete_elem &&
2812 func_id != BPF_FUNC_map_push_elem &&
2813 func_id != BPF_FUNC_map_pop_elem &&
2814 func_id != BPF_FUNC_map_peek_elem)
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002815 return 0;
Daniel Borkmann09772d92018-06-02 23:06:35 +02002816
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002817 if (meta->map_ptr == NULL) {
2818 verbose(env, "kernel subsystem misconfigured verifier\n");
2819 return -EINVAL;
2820 }
2821
2822 if (!BPF_MAP_PTR(aux->map_state))
2823 bpf_map_ptr_store(aux, meta->map_ptr,
2824 meta->map_ptr->unpriv_array);
2825 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
2826 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
2827 meta->map_ptr->unpriv_array);
2828 return 0;
2829}
2830
Joe Stringerfd978bf72018-10-02 13:35:35 -07002831static int check_reference_leak(struct bpf_verifier_env *env)
2832{
2833 struct bpf_func_state *state = cur_func(env);
2834 int i;
2835
2836 for (i = 0; i < state->acquired_refs; i++) {
2837 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
2838 state->refs[i].id, state->refs[i].insn_idx);
2839 }
2840 return state->acquired_refs ? -EINVAL : 0;
2841}
2842
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08002843static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002844{
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002845 const struct bpf_func_proto *fn = NULL;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002846 struct bpf_reg_state *regs;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002847 struct bpf_call_arg_meta meta;
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002848 bool changes_data;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002849 int i, err;
2850
2851 /* find function prototype */
2852 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002853 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
2854 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002855 return -EINVAL;
2856 }
2857
Jakub Kicinski00176a32017-10-16 16:40:54 -07002858 if (env->ops->get_func_proto)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002859 fn = env->ops->get_func_proto(func_id, env->prog);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002860 if (!fn) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002861 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
2862 func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002863 return -EINVAL;
2864 }
2865
2866 /* eBPF programs must be GPL compatible to use GPL-ed functions */
Daniel Borkmann24701ec2015-03-01 12:31:47 +01002867 if (!env->prog->gpl_compatible && fn->gpl_only) {
Daniel Borkmann3fe28672018-06-02 23:06:33 +02002868 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002869 return -EINVAL;
2870 }
2871
Daniel Borkmann04514d12017-12-14 21:07:25 +01002872 /* With LD_ABS/IND some JITs save/restore skb from r1. */
Martin KaFai Lau17bedab2016-12-07 15:53:11 -08002873 changes_data = bpf_helper_changes_pkt_data(fn->func);
Daniel Borkmann04514d12017-12-14 21:07:25 +01002874 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
2875 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
2876 func_id_name(func_id), func_id);
2877 return -EINVAL;
2878 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07002879
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002880 memset(&meta, 0, sizeof(meta));
Daniel Borkmann36bbef52016-09-20 00:26:13 +02002881 meta.pkt_access = fn->pkt_access;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002882
Daniel Borkmann90133412018-01-20 01:24:29 +01002883 err = check_func_proto(fn);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002884 if (err) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002885 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002886 func_id_name(func_id), func_id);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002887 return err;
2888 }
2889
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002890 /* check args */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002891 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002892 if (err)
2893 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002894 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002895 if (err)
2896 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002897 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002898 if (err)
2899 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002900 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002901 if (err)
2902 return err;
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002903 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002904 if (err)
2905 return err;
2906
Daniel Borkmannc93552c2018-05-24 02:32:53 +02002907 err = record_func_map(env, &meta, func_id, insn_idx);
2908 if (err)
2909 return err;
2910
Daniel Borkmann435faee12016-04-13 00:10:51 +02002911 /* Mark slots with STACK_MISC in case of raw mode, stack offset
2912 * is inferred from register state.
2913 */
2914 for (i = 0; i < meta.access_size; i++) {
Daniel Borkmannca369602018-02-23 22:29:05 +01002915 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
2916 BPF_WRITE, -1, false);
Daniel Borkmann435faee12016-04-13 00:10:51 +02002917 if (err)
2918 return err;
2919 }
2920
Joe Stringerfd978bf72018-10-02 13:35:35 -07002921 if (func_id == BPF_FUNC_tail_call) {
2922 err = check_reference_leak(env);
2923 if (err) {
2924 verbose(env, "tail_call would lead to reference leak\n");
2925 return err;
2926 }
2927 } else if (is_release_function(func_id)) {
2928 err = release_reference(env, &meta);
2929 if (err)
2930 return err;
2931 }
2932
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07002933 regs = cur_regs(env);
Roman Gushchincd339432018-08-02 14:27:24 -07002934
2935 /* check that flags argument in get_local_storage(map, flags) is 0,
2936 * this is required because get_local_storage() can't return an error.
2937 */
2938 if (func_id == BPF_FUNC_get_local_storage &&
2939 !register_is_null(&regs[BPF_REG_2])) {
2940 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
2941 return -EINVAL;
2942 }
2943
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002944 /* reset caller saved regs */
Edward Creedc503a82017-08-15 20:34:35 +01002945 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002946 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01002947 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
2948 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002949
Edward Creedc503a82017-08-15 20:34:35 +01002950 /* update return register (already marked as written above) */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002951 if (fn->ret_type == RET_INTEGER) {
Edward Creef1174f72017-08-07 15:26:19 +01002952 /* sets type to SCALAR_VALUE */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002953 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002954 } else if (fn->ret_type == RET_VOID) {
2955 regs[BPF_REG_0].type = NOT_INIT;
Roman Gushchin3e6a4b32018-08-02 14:27:22 -07002956 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
2957 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
Edward Creef1174f72017-08-07 15:26:19 +01002958 /* There is no offset yet applied, variable or fixed */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002959 mark_reg_known_zero(env, regs, BPF_REG_0);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002960 /* remember map_ptr, so that check_map_access()
2961 * can check 'value_size' boundary of memory access
2962 * to map element returned from bpf_map_lookup_elem()
2963 */
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002964 if (meta.map_ptr == NULL) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002965 verbose(env,
2966 "kernel subsystem misconfigured verifier\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002967 return -EINVAL;
2968 }
Daniel Borkmann33ff9822016-04-13 00:10:50 +02002969 regs[BPF_REG_0].map_ptr = meta.map_ptr;
Daniel Borkmann4d31f302018-11-01 00:05:53 +01002970 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
2971 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
2972 } else {
2973 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
2974 regs[BPF_REG_0].id = ++env->id_gen;
2975 }
Joe Stringerc64b7982018-10-02 13:35:33 -07002976 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
Joe Stringerfd978bf72018-10-02 13:35:35 -07002977 int id = acquire_reference_state(env, insn_idx);
2978 if (id < 0)
2979 return id;
Joe Stringerc64b7982018-10-02 13:35:33 -07002980 mark_reg_known_zero(env, regs, BPF_REG_0);
2981 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
Joe Stringerfd978bf72018-10-02 13:35:35 -07002982 regs[BPF_REG_0].id = id;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002983 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002984 verbose(env, "unknown return type %d of func %s#%d\n",
Thomas Grafebb676d2016-10-27 11:23:51 +02002985 fn->ret_type, func_id_name(func_id), func_id);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07002986 return -EINVAL;
2987 }
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002988
Yonghong Song849fa502018-04-28 22:28:09 -07002989 do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
2990
Jakub Kicinski61bd5212017-10-09 10:30:11 -07002991 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
Kaixu Xia35578d72015-08-06 07:02:35 +00002992 if (err)
2993 return err;
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002994
Yonghong Songc195651e2018-04-28 22:28:08 -07002995 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
2996 const char *err_str;
2997
2998#ifdef CONFIG_PERF_EVENTS
2999 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3000 err_str = "cannot get callchain buffer for func %s#%d\n";
3001#else
3002 err = -ENOTSUPP;
3003 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3004#endif
3005 if (err) {
3006 verbose(env, err_str, func_id_name(func_id), func_id);
3007 return err;
3008 }
3009
3010 env->prog->has_callchain_buf = true;
3011 }
3012
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003013 if (changes_data)
3014 clear_all_pkt_pointers(env);
3015 return 0;
3016}
3017
Edward Creeb03c9f92017-08-07 15:26:36 +01003018static bool signed_add_overflows(s64 a, s64 b)
3019{
3020 /* Do the add in u64, where overflow is well-defined */
3021 s64 res = (s64)((u64)a + (u64)b);
3022
3023 if (b < 0)
3024 return res > a;
3025 return res < a;
3026}
3027
3028static bool signed_sub_overflows(s64 a, s64 b)
3029{
3030 /* Do the sub in u64, where overflow is well-defined */
3031 s64 res = (s64)((u64)a - (u64)b);
3032
3033 if (b < 0)
3034 return res < a;
3035 return res > a;
David S. Millerd1174412017-05-10 11:22:52 -07003036}
3037
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003038static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3039 const struct bpf_reg_state *reg,
3040 enum bpf_reg_type type)
3041{
3042 bool known = tnum_is_const(reg->var_off);
3043 s64 val = reg->var_off.value;
3044 s64 smin = reg->smin_value;
3045
3046 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3047 verbose(env, "math between %s pointer and %lld is not allowed\n",
3048 reg_type_str[type], val);
3049 return false;
3050 }
3051
3052 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3053 verbose(env, "%s pointer offset %d is not allowed\n",
3054 reg_type_str[type], reg->off);
3055 return false;
3056 }
3057
3058 if (smin == S64_MIN) {
3059 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3060 reg_type_str[type]);
3061 return false;
3062 }
3063
3064 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3065 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3066 smin, reg_type_str[type]);
3067 return false;
3068 }
3069
3070 return true;
3071}
3072
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003073static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3074{
3075 return &env->insn_aux_data[env->insn_idx];
3076}
3077
3078static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3079 u32 *ptr_limit, u8 opcode, bool off_is_neg)
3080{
3081 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
3082 (opcode == BPF_SUB && !off_is_neg);
3083 u32 off;
3084
3085 switch (ptr_reg->type) {
3086 case PTR_TO_STACK:
3087 off = ptr_reg->off + ptr_reg->var_off.value;
3088 if (mask_to_left)
3089 *ptr_limit = MAX_BPF_STACK + off;
3090 else
3091 *ptr_limit = -off;
3092 return 0;
3093 case PTR_TO_MAP_VALUE:
3094 if (mask_to_left) {
3095 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3096 } else {
3097 off = ptr_reg->smin_value + ptr_reg->off;
3098 *ptr_limit = ptr_reg->map_ptr->value_size - off;
3099 }
3100 return 0;
3101 default:
3102 return -EINVAL;
3103 }
3104}
3105
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003106static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3107 const struct bpf_insn *insn)
3108{
3109 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3110}
3111
3112static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3113 u32 alu_state, u32 alu_limit)
3114{
3115 /* If we arrived here from different branches with different
3116 * state or limits to sanitize, then this won't work.
3117 */
3118 if (aux->alu_state &&
3119 (aux->alu_state != alu_state ||
3120 aux->alu_limit != alu_limit))
3121 return -EACCES;
3122
3123 /* Corresponding fixup done in fixup_bpf_calls(). */
3124 aux->alu_state = alu_state;
3125 aux->alu_limit = alu_limit;
3126 return 0;
3127}
3128
3129static int sanitize_val_alu(struct bpf_verifier_env *env,
3130 struct bpf_insn *insn)
3131{
3132 struct bpf_insn_aux_data *aux = cur_aux(env);
3133
3134 if (can_skip_alu_sanitation(env, insn))
3135 return 0;
3136
3137 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3138}
3139
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003140static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3141 struct bpf_insn *insn,
3142 const struct bpf_reg_state *ptr_reg,
3143 struct bpf_reg_state *dst_reg,
3144 bool off_is_neg)
3145{
3146 struct bpf_verifier_state *vstate = env->cur_state;
3147 struct bpf_insn_aux_data *aux = cur_aux(env);
3148 bool ptr_is_dst_reg = ptr_reg == dst_reg;
3149 u8 opcode = BPF_OP(insn->code);
3150 u32 alu_state, alu_limit;
3151 struct bpf_reg_state tmp;
3152 bool ret;
3153
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003154 if (can_skip_alu_sanitation(env, insn))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003155 return 0;
3156
3157 /* We already marked aux for masking from non-speculative
3158 * paths, thus we got here in the first place. We only care
3159 * to explore bad access from here.
3160 */
3161 if (vstate->speculative)
3162 goto do_sim;
3163
3164 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3165 alu_state |= ptr_is_dst_reg ?
3166 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3167
3168 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3169 return 0;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003170 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003171 return -EACCES;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003172do_sim:
3173 /* Simulate and find potential out-of-bounds access under
3174 * speculative execution from truncation as a result of
3175 * masking when off was not within expected range. If off
3176 * sits in dst, then we temporarily need to move ptr there
3177 * to simulate dst (== 0) +/-= ptr. Needed, for example,
3178 * for cases where we use K-based arithmetic in one direction
3179 * and truncated reg-based in the other in order to explore
3180 * bad access.
3181 */
3182 if (!ptr_is_dst_reg) {
3183 tmp = *dst_reg;
3184 *dst_reg = *ptr_reg;
3185 }
3186 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
3187 if (!ptr_is_dst_reg)
3188 *dst_reg = tmp;
3189 return !ret ? -EFAULT : 0;
3190}
3191
Edward Creef1174f72017-08-07 15:26:19 +01003192/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
Edward Creef1174f72017-08-07 15:26:19 +01003193 * Caller should also handle BPF_MOV case separately.
3194 * If we return -EACCES, caller may want to try again treating pointer as a
3195 * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks.
3196 */
3197static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3198 struct bpf_insn *insn,
3199 const struct bpf_reg_state *ptr_reg,
3200 const struct bpf_reg_state *off_reg)
Josef Bacik48461132016-09-28 10:54:32 -04003201{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003202 struct bpf_verifier_state *vstate = env->cur_state;
3203 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3204 struct bpf_reg_state *regs = state->regs, *dst_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003205 bool known = tnum_is_const(off_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003206 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3207 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3208 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3209 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003210 u32 dst = insn->dst_reg, src = insn->src_reg;
Josef Bacik48461132016-09-28 10:54:32 -04003211 u8 opcode = BPF_OP(insn->code);
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003212 int ret;
Josef Bacik48461132016-09-28 10:54:32 -04003213
Edward Creef1174f72017-08-07 15:26:19 +01003214 dst_reg = &regs[dst];
Josef Bacik48461132016-09-28 10:54:32 -04003215
Daniel Borkmann6f161012018-01-18 01:15:21 +01003216 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3217 smin_val > smax_val || umin_val > umax_val) {
3218 /* Taint dst register if offset had invalid bounds derived from
3219 * e.g. dead branches.
3220 */
3221 __mark_reg_unknown(dst_reg);
3222 return 0;
Josef Bacik48461132016-09-28 10:54:32 -04003223 }
3224
Edward Creef1174f72017-08-07 15:26:19 +01003225 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3226 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003227 verbose(env,
3228 "R%d 32-bit pointer arithmetic prohibited\n",
3229 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003230 return -EACCES;
3231 }
David S. Millerd1174412017-05-10 11:22:52 -07003232
Joe Stringeraad2eea2018-10-02 13:35:30 -07003233 switch (ptr_reg->type) {
3234 case PTR_TO_MAP_VALUE_OR_NULL:
3235 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3236 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003237 return -EACCES;
Joe Stringeraad2eea2018-10-02 13:35:30 -07003238 case CONST_PTR_TO_MAP:
3239 case PTR_TO_PACKET_END:
Joe Stringerc64b7982018-10-02 13:35:33 -07003240 case PTR_TO_SOCKET:
3241 case PTR_TO_SOCKET_OR_NULL:
Joe Stringeraad2eea2018-10-02 13:35:30 -07003242 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3243 dst, reg_type_str[ptr_reg->type]);
Edward Creef1174f72017-08-07 15:26:19 +01003244 return -EACCES;
Daniel Borkmann9d7ecee2019-01-03 00:58:32 +01003245 case PTR_TO_MAP_VALUE:
3246 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3247 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3248 off_reg == dst_reg ? dst : src);
3249 return -EACCES;
3250 }
3251 /* fall-through */
Joe Stringeraad2eea2018-10-02 13:35:30 -07003252 default:
3253 break;
Edward Creef1174f72017-08-07 15:26:19 +01003254 }
3255
3256 /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3257 * The id may be overwritten later if we create a new variable offset.
Josef Bacik48461132016-09-28 10:54:32 -04003258 */
Edward Creef1174f72017-08-07 15:26:19 +01003259 dst_reg->type = ptr_reg->type;
3260 dst_reg->id = ptr_reg->id;
Josef Bacikf23cc642016-11-14 15:45:36 -05003261
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003262 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3263 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3264 return -EINVAL;
3265
Josef Bacik48461132016-09-28 10:54:32 -04003266 switch (opcode) {
3267 case BPF_ADD:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003268 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3269 if (ret < 0) {
3270 verbose(env, "R%d tried to add from different maps or paths\n", dst);
3271 return ret;
3272 }
Edward Creef1174f72017-08-07 15:26:19 +01003273 /* We can take a fixed offset as long as it doesn't overflow
3274 * the s32 'off' field
3275 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003276 if (known && (ptr_reg->off + smin_val ==
3277 (s64)(s32)(ptr_reg->off + smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003278 /* pointer += K. Accumulate it into fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003279 dst_reg->smin_value = smin_ptr;
3280 dst_reg->smax_value = smax_ptr;
3281 dst_reg->umin_value = umin_ptr;
3282 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003283 dst_reg->var_off = ptr_reg->var_off;
Edward Creeb03c9f92017-08-07 15:26:36 +01003284 dst_reg->off = ptr_reg->off + smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003285 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003286 break;
3287 }
Edward Creef1174f72017-08-07 15:26:19 +01003288 /* A new variable offset is created. Note that off_reg->off
3289 * == 0, since it's a scalar.
3290 * dst_reg gets the pointer type and since some positive
3291 * integer value was added to the pointer, give it a new 'id'
3292 * if it's a PTR_TO_PACKET.
3293 * this creates a new 'base' pointer, off_reg (variable) gets
3294 * added into the variable offset, and we copy the fixed offset
3295 * from ptr_reg.
3296 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003297 if (signed_add_overflows(smin_ptr, smin_val) ||
3298 signed_add_overflows(smax_ptr, smax_val)) {
3299 dst_reg->smin_value = S64_MIN;
3300 dst_reg->smax_value = S64_MAX;
3301 } else {
3302 dst_reg->smin_value = smin_ptr + smin_val;
3303 dst_reg->smax_value = smax_ptr + smax_val;
3304 }
3305 if (umin_ptr + umin_val < umin_ptr ||
3306 umax_ptr + umax_val < umax_ptr) {
3307 dst_reg->umin_value = 0;
3308 dst_reg->umax_value = U64_MAX;
3309 } else {
3310 dst_reg->umin_value = umin_ptr + umin_val;
3311 dst_reg->umax_value = umax_ptr + umax_val;
3312 }
Edward Creef1174f72017-08-07 15:26:19 +01003313 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3314 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003315 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003316 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003317 dst_reg->id = ++env->id_gen;
3318 /* something was added to pkt_ptr, set range to zero */
Daniel Borkmann09625902018-11-01 00:05:52 +01003319 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003320 }
Josef Bacik48461132016-09-28 10:54:32 -04003321 break;
3322 case BPF_SUB:
Daniel Borkmann979d63d2019-01-03 00:58:34 +01003323 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3324 if (ret < 0) {
3325 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3326 return ret;
3327 }
Edward Creef1174f72017-08-07 15:26:19 +01003328 if (dst_reg == off_reg) {
3329 /* scalar -= pointer. Creates an unknown scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003330 verbose(env, "R%d tried to subtract pointer from scalar\n",
3331 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003332 return -EACCES;
3333 }
3334 /* We don't allow subtraction from FP, because (according to
3335 * test_verifier.c test "invalid fp arithmetic", JITs might not
3336 * be able to deal with it.
Edward Cree93057062017-07-21 14:37:34 +01003337 */
Edward Creef1174f72017-08-07 15:26:19 +01003338 if (ptr_reg->type == PTR_TO_STACK) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003339 verbose(env, "R%d subtraction from stack pointer prohibited\n",
3340 dst);
Edward Creef1174f72017-08-07 15:26:19 +01003341 return -EACCES;
3342 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003343 if (known && (ptr_reg->off - smin_val ==
3344 (s64)(s32)(ptr_reg->off - smin_val))) {
Edward Creef1174f72017-08-07 15:26:19 +01003345 /* pointer -= K. Subtract it from fixed offset */
Edward Creeb03c9f92017-08-07 15:26:36 +01003346 dst_reg->smin_value = smin_ptr;
3347 dst_reg->smax_value = smax_ptr;
3348 dst_reg->umin_value = umin_ptr;
3349 dst_reg->umax_value = umax_ptr;
Edward Creef1174f72017-08-07 15:26:19 +01003350 dst_reg->var_off = ptr_reg->var_off;
3351 dst_reg->id = ptr_reg->id;
Edward Creeb03c9f92017-08-07 15:26:36 +01003352 dst_reg->off = ptr_reg->off - smin_val;
Daniel Borkmann09625902018-11-01 00:05:52 +01003353 dst_reg->raw = ptr_reg->raw;
Edward Creef1174f72017-08-07 15:26:19 +01003354 break;
3355 }
Edward Creef1174f72017-08-07 15:26:19 +01003356 /* A new variable offset is created. If the subtrahend is known
3357 * nonnegative, then any reg->range we had before is still good.
3358 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003359 if (signed_sub_overflows(smin_ptr, smax_val) ||
3360 signed_sub_overflows(smax_ptr, smin_val)) {
3361 /* Overflow possible, we know nothing */
3362 dst_reg->smin_value = S64_MIN;
3363 dst_reg->smax_value = S64_MAX;
3364 } else {
3365 dst_reg->smin_value = smin_ptr - smax_val;
3366 dst_reg->smax_value = smax_ptr - smin_val;
3367 }
3368 if (umin_ptr < umax_val) {
3369 /* Overflow possible, we know nothing */
3370 dst_reg->umin_value = 0;
3371 dst_reg->umax_value = U64_MAX;
3372 } else {
3373 /* Cannot overflow (as long as bounds are consistent) */
3374 dst_reg->umin_value = umin_ptr - umax_val;
3375 dst_reg->umax_value = umax_ptr - umin_val;
3376 }
Edward Creef1174f72017-08-07 15:26:19 +01003377 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3378 dst_reg->off = ptr_reg->off;
Daniel Borkmann09625902018-11-01 00:05:52 +01003379 dst_reg->raw = ptr_reg->raw;
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003380 if (reg_is_pkt_pointer(ptr_reg)) {
Edward Creef1174f72017-08-07 15:26:19 +01003381 dst_reg->id = ++env->id_gen;
3382 /* something was added to pkt_ptr, set range to zero */
Edward Creeb03c9f92017-08-07 15:26:36 +01003383 if (smin_val < 0)
Daniel Borkmann09625902018-11-01 00:05:52 +01003384 dst_reg->raw = 0;
Edward Creef1174f72017-08-07 15:26:19 +01003385 }
3386 break;
3387 case BPF_AND:
3388 case BPF_OR:
3389 case BPF_XOR:
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003390 /* bitwise ops on pointers are troublesome, prohibit. */
3391 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3392 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003393 return -EACCES;
3394 default:
3395 /* other operators (e.g. MUL,LSH) produce non-pointer results */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003396 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3397 dst, bpf_alu_string[opcode >> 4]);
Edward Creef1174f72017-08-07 15:26:19 +01003398 return -EACCES;
3399 }
3400
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003401 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3402 return -EINVAL;
3403
Edward Creeb03c9f92017-08-07 15:26:36 +01003404 __update_reg_bounds(dst_reg);
3405 __reg_deduce_bounds(dst_reg);
3406 __reg_bound_offset(dst_reg);
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003407
3408 /* For unprivileged we require that resulting offset must be in bounds
3409 * in order to be able to sanitize access later on.
3410 */
Daniel Borkmanne4298d22019-01-03 00:58:31 +01003411 if (!env->allow_ptr_leaks) {
3412 if (dst_reg->type == PTR_TO_MAP_VALUE &&
3413 check_map_access(env, dst, dst_reg->off, 1, false)) {
3414 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
3415 "prohibited for !root\n", dst);
3416 return -EACCES;
3417 } else if (dst_reg->type == PTR_TO_STACK &&
3418 check_stack_access(env, dst_reg, dst_reg->off +
3419 dst_reg->var_off.value, 1)) {
3420 verbose(env, "R%d stack pointer arithmetic goes out of range, "
3421 "prohibited for !root\n", dst);
3422 return -EACCES;
3423 }
Daniel Borkmann0d6303d2019-01-03 00:58:30 +01003424 }
3425
Edward Creef1174f72017-08-07 15:26:19 +01003426 return 0;
3427}
3428
Jann Horn468f6ea2017-12-18 20:11:56 -08003429/* WARNING: This function does calculations on 64-bit values, but the actual
3430 * execution may occur on 32-bit values. Therefore, things like bitshifts
3431 * need extra checks in the 32-bit case.
3432 */
Edward Creef1174f72017-08-07 15:26:19 +01003433static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3434 struct bpf_insn *insn,
3435 struct bpf_reg_state *dst_reg,
3436 struct bpf_reg_state src_reg)
3437{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003438 struct bpf_reg_state *regs = cur_regs(env);
Edward Creef1174f72017-08-07 15:26:19 +01003439 u8 opcode = BPF_OP(insn->code);
3440 bool src_known, dst_known;
Edward Creeb03c9f92017-08-07 15:26:36 +01003441 s64 smin_val, smax_val;
3442 u64 umin_val, umax_val;
Jann Horn468f6ea2017-12-18 20:11:56 -08003443 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003444 u32 dst = insn->dst_reg;
3445 int ret;
Edward Creef1174f72017-08-07 15:26:19 +01003446
Jann Hornb7992072018-10-05 18:17:59 +02003447 if (insn_bitness == 32) {
3448 /* Relevant for 32-bit RSH: Information can propagate towards
3449 * LSB, so it isn't sufficient to only truncate the output to
3450 * 32 bits.
3451 */
3452 coerce_reg_to_size(dst_reg, 4);
3453 coerce_reg_to_size(&src_reg, 4);
3454 }
3455
Edward Creeb03c9f92017-08-07 15:26:36 +01003456 smin_val = src_reg.smin_value;
3457 smax_val = src_reg.smax_value;
3458 umin_val = src_reg.umin_value;
3459 umax_val = src_reg.umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003460 src_known = tnum_is_const(src_reg.var_off);
3461 dst_known = tnum_is_const(dst_reg->var_off);
3462
Daniel Borkmann6f161012018-01-18 01:15:21 +01003463 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3464 smin_val > smax_val || umin_val > umax_val) {
3465 /* Taint dst register if offset had invalid bounds derived from
3466 * e.g. dead branches.
3467 */
3468 __mark_reg_unknown(dst_reg);
3469 return 0;
3470 }
3471
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -08003472 if (!src_known &&
3473 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3474 __mark_reg_unknown(dst_reg);
3475 return 0;
3476 }
3477
Edward Creef1174f72017-08-07 15:26:19 +01003478 switch (opcode) {
3479 case BPF_ADD:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003480 ret = sanitize_val_alu(env, insn);
3481 if (ret < 0) {
3482 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3483 return ret;
3484 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003485 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3486 signed_add_overflows(dst_reg->smax_value, smax_val)) {
3487 dst_reg->smin_value = S64_MIN;
3488 dst_reg->smax_value = S64_MAX;
3489 } else {
3490 dst_reg->smin_value += smin_val;
3491 dst_reg->smax_value += smax_val;
3492 }
3493 if (dst_reg->umin_value + umin_val < umin_val ||
3494 dst_reg->umax_value + umax_val < umax_val) {
3495 dst_reg->umin_value = 0;
3496 dst_reg->umax_value = U64_MAX;
3497 } else {
3498 dst_reg->umin_value += umin_val;
3499 dst_reg->umax_value += umax_val;
3500 }
Edward Creef1174f72017-08-07 15:26:19 +01003501 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3502 break;
3503 case BPF_SUB:
Daniel Borkmannd3bd7412019-01-06 00:54:37 +01003504 ret = sanitize_val_alu(env, insn);
3505 if (ret < 0) {
3506 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3507 return ret;
3508 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003509 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3510 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3511 /* Overflow possible, we know nothing */
3512 dst_reg->smin_value = S64_MIN;
3513 dst_reg->smax_value = S64_MAX;
3514 } else {
3515 dst_reg->smin_value -= smax_val;
3516 dst_reg->smax_value -= smin_val;
3517 }
3518 if (dst_reg->umin_value < umax_val) {
3519 /* Overflow possible, we know nothing */
3520 dst_reg->umin_value = 0;
3521 dst_reg->umax_value = U64_MAX;
3522 } else {
3523 /* Cannot overflow (as long as bounds are consistent) */
3524 dst_reg->umin_value -= umax_val;
3525 dst_reg->umax_value -= umin_val;
3526 }
Edward Creef1174f72017-08-07 15:26:19 +01003527 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
Josef Bacik48461132016-09-28 10:54:32 -04003528 break;
3529 case BPF_MUL:
Edward Creeb03c9f92017-08-07 15:26:36 +01003530 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3531 if (smin_val < 0 || dst_reg->smin_value < 0) {
Edward Creef1174f72017-08-07 15:26:19 +01003532 /* Ain't nobody got time to multiply that sign */
Edward Creeb03c9f92017-08-07 15:26:36 +01003533 __mark_reg_unbounded(dst_reg);
3534 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003535 break;
3536 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003537 /* Both values are positive, so we can work with unsigned and
3538 * copy the result to signed (unless it exceeds S64_MAX).
Edward Creef1174f72017-08-07 15:26:19 +01003539 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003540 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3541 /* Potential overflow, we know nothing */
3542 __mark_reg_unbounded(dst_reg);
3543 /* (except what we can learn from the var_off) */
3544 __update_reg_bounds(dst_reg);
3545 break;
3546 }
3547 dst_reg->umin_value *= umin_val;
3548 dst_reg->umax_value *= umax_val;
3549 if (dst_reg->umax_value > S64_MAX) {
3550 /* Overflow possible, we know nothing */
3551 dst_reg->smin_value = S64_MIN;
3552 dst_reg->smax_value = S64_MAX;
3553 } else {
3554 dst_reg->smin_value = dst_reg->umin_value;
3555 dst_reg->smax_value = dst_reg->umax_value;
3556 }
Josef Bacik48461132016-09-28 10:54:32 -04003557 break;
3558 case BPF_AND:
Edward Creef1174f72017-08-07 15:26:19 +01003559 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003560 __mark_reg_known(dst_reg, dst_reg->var_off.value &
3561 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003562 break;
3563 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003564 /* We get our minimum from the var_off, since that's inherently
3565 * bitwise. Our maximum is the minimum of the operands' maxima.
Josef Bacikf23cc642016-11-14 15:45:36 -05003566 */
Edward Creef1174f72017-08-07 15:26:19 +01003567 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003568 dst_reg->umin_value = dst_reg->var_off.value;
3569 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3570 if (dst_reg->smin_value < 0 || smin_val < 0) {
3571 /* Lose signed bounds when ANDing negative numbers,
3572 * ain't nobody got time for that.
3573 */
3574 dst_reg->smin_value = S64_MIN;
3575 dst_reg->smax_value = S64_MAX;
3576 } else {
3577 /* ANDing two positives gives a positive, so safe to
3578 * cast result into s64.
3579 */
3580 dst_reg->smin_value = dst_reg->umin_value;
3581 dst_reg->smax_value = dst_reg->umax_value;
3582 }
3583 /* We may learn something more from the var_off */
3584 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003585 break;
3586 case BPF_OR:
3587 if (src_known && dst_known) {
Edward Creeb03c9f92017-08-07 15:26:36 +01003588 __mark_reg_known(dst_reg, dst_reg->var_off.value |
3589 src_reg.var_off.value);
Edward Creef1174f72017-08-07 15:26:19 +01003590 break;
3591 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003592 /* We get our maximum from the var_off, and our minimum is the
3593 * maximum of the operands' minima
Edward Creef1174f72017-08-07 15:26:19 +01003594 */
3595 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01003596 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
3597 dst_reg->umax_value = dst_reg->var_off.value |
3598 dst_reg->var_off.mask;
3599 if (dst_reg->smin_value < 0 || smin_val < 0) {
3600 /* Lose signed bounds when ORing negative numbers,
3601 * ain't nobody got time for that.
3602 */
3603 dst_reg->smin_value = S64_MIN;
3604 dst_reg->smax_value = S64_MAX;
Edward Creef1174f72017-08-07 15:26:19 +01003605 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003606 /* ORing two positives gives a positive, so safe to
3607 * cast result into s64.
3608 */
3609 dst_reg->smin_value = dst_reg->umin_value;
3610 dst_reg->smax_value = dst_reg->umax_value;
Edward Creef1174f72017-08-07 15:26:19 +01003611 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003612 /* We may learn something more from the var_off */
3613 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003614 break;
3615 case BPF_LSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003616 if (umax_val >= insn_bitness) {
3617 /* Shifts greater than 31 or 63 are undefined.
3618 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003619 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003620 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003621 break;
3622 }
Edward Creeb03c9f92017-08-07 15:26:36 +01003623 /* We lose all sign bit information (except what we can pick
3624 * up from var_off)
Josef Bacik48461132016-09-28 10:54:32 -04003625 */
Edward Creeb03c9f92017-08-07 15:26:36 +01003626 dst_reg->smin_value = S64_MIN;
3627 dst_reg->smax_value = S64_MAX;
3628 /* If we might shift our top bit out, then we know nothing */
3629 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
3630 dst_reg->umin_value = 0;
3631 dst_reg->umax_value = U64_MAX;
David S. Millerd1174412017-05-10 11:22:52 -07003632 } else {
Edward Creeb03c9f92017-08-07 15:26:36 +01003633 dst_reg->umin_value <<= umin_val;
3634 dst_reg->umax_value <<= umax_val;
David S. Millerd1174412017-05-10 11:22:52 -07003635 }
Yonghong Songafbe1a52018-04-28 22:28:10 -07003636 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003637 /* We may learn something more from the var_off */
3638 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003639 break;
3640 case BPF_RSH:
Jann Horn468f6ea2017-12-18 20:11:56 -08003641 if (umax_val >= insn_bitness) {
3642 /* Shifts greater than 31 or 63 are undefined.
3643 * This includes shifts by a negative number.
Edward Creeb03c9f92017-08-07 15:26:36 +01003644 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003645 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003646 break;
3647 }
Edward Cree4374f252017-12-18 20:11:53 -08003648 /* BPF_RSH is an unsigned shift. If the value in dst_reg might
3649 * be negative, then either:
3650 * 1) src_reg might be zero, so the sign bit of the result is
3651 * unknown, so we lose our signed bounds
3652 * 2) it's known negative, thus the unsigned bounds capture the
3653 * signed bounds
3654 * 3) the signed bounds cross zero, so they tell us nothing
3655 * about the result
3656 * If the value in dst_reg is known nonnegative, then again the
3657 * unsigned bounts capture the signed bounds.
3658 * Thus, in all cases it suffices to blow away our signed bounds
3659 * and rely on inferring new ones from the unsigned bounds and
3660 * var_off of the result.
3661 */
3662 dst_reg->smin_value = S64_MIN;
3663 dst_reg->smax_value = S64_MAX;
Yonghong Songafbe1a52018-04-28 22:28:10 -07003664 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
Edward Creeb03c9f92017-08-07 15:26:36 +01003665 dst_reg->umin_value >>= umax_val;
3666 dst_reg->umax_value >>= umin_val;
3667 /* We may learn something more from the var_off */
3668 __update_reg_bounds(dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003669 break;
Yonghong Song9cbe1f5a2018-04-28 22:28:11 -07003670 case BPF_ARSH:
3671 if (umax_val >= insn_bitness) {
3672 /* Shifts greater than 31 or 63 are undefined.
3673 * This includes shifts by a negative number.
3674 */
3675 mark_reg_unknown(env, regs, insn->dst_reg);
3676 break;
3677 }
3678
3679 /* Upon reaching here, src_known is true and
3680 * umax_val is equal to umin_val.
3681 */
3682 dst_reg->smin_value >>= umin_val;
3683 dst_reg->smax_value >>= umin_val;
3684 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
3685
3686 /* blow away the dst_reg umin_value/umax_value and rely on
3687 * dst_reg var_off to refine the result.
3688 */
3689 dst_reg->umin_value = 0;
3690 dst_reg->umax_value = U64_MAX;
3691 __update_reg_bounds(dst_reg);
3692 break;
Josef Bacik48461132016-09-28 10:54:32 -04003693 default:
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003694 mark_reg_unknown(env, regs, insn->dst_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003695 break;
3696 }
3697
Jann Horn468f6ea2017-12-18 20:11:56 -08003698 if (BPF_CLASS(insn->code) != BPF_ALU64) {
3699 /* 32-bit ALU ops are (32,32)->32 */
3700 coerce_reg_to_size(dst_reg, 4);
Jann Horn468f6ea2017-12-18 20:11:56 -08003701 }
3702
Edward Creeb03c9f92017-08-07 15:26:36 +01003703 __reg_deduce_bounds(dst_reg);
3704 __reg_bound_offset(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003705 return 0;
3706}
3707
3708/* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
3709 * and var_off.
3710 */
3711static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
3712 struct bpf_insn *insn)
3713{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003714 struct bpf_verifier_state *vstate = env->cur_state;
3715 struct bpf_func_state *state = vstate->frame[vstate->curframe];
3716 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
Edward Creef1174f72017-08-07 15:26:19 +01003717 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
3718 u8 opcode = BPF_OP(insn->code);
Edward Creef1174f72017-08-07 15:26:19 +01003719
3720 dst_reg = &regs[insn->dst_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003721 src_reg = NULL;
3722 if (dst_reg->type != SCALAR_VALUE)
3723 ptr_reg = dst_reg;
3724 if (BPF_SRC(insn->code) == BPF_X) {
3725 src_reg = &regs[insn->src_reg];
Edward Creef1174f72017-08-07 15:26:19 +01003726 if (src_reg->type != SCALAR_VALUE) {
3727 if (dst_reg->type != SCALAR_VALUE) {
3728 /* Combining two pointers by any ALU op yields
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003729 * an arbitrary scalar. Disallow all math except
3730 * pointer subtraction
Edward Creef1174f72017-08-07 15:26:19 +01003731 */
Alexei Starovoitovdd066822018-09-12 14:06:10 -07003732 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003733 mark_reg_unknown(env, regs, insn->dst_reg);
3734 return 0;
Edward Creef1174f72017-08-07 15:26:19 +01003735 }
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003736 verbose(env, "R%d pointer %s pointer prohibited\n",
3737 insn->dst_reg,
3738 bpf_alu_string[opcode >> 4]);
3739 return -EACCES;
Edward Creef1174f72017-08-07 15:26:19 +01003740 } else {
3741 /* scalar += pointer
3742 * This is legal, but we have to reverse our
3743 * src/dest handling in computing the range
3744 */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003745 return adjust_ptr_min_max_vals(env, insn,
3746 src_reg, dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003747 }
3748 } else if (ptr_reg) {
3749 /* pointer += scalar */
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003750 return adjust_ptr_min_max_vals(env, insn,
3751 dst_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003752 }
3753 } else {
3754 /* Pretend the src is a reg with a known value, since we only
3755 * need to be able to read from this state.
3756 */
3757 off_reg.type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01003758 __mark_reg_known(&off_reg, insn->imm);
Edward Creef1174f72017-08-07 15:26:19 +01003759 src_reg = &off_reg;
Alexei Starovoitov82abbf82017-12-18 20:15:20 -08003760 if (ptr_reg) /* pointer += K */
3761 return adjust_ptr_min_max_vals(env, insn,
3762 ptr_reg, src_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003763 }
3764
3765 /* Got here implies adding two SCALAR_VALUEs */
3766 if (WARN_ON_ONCE(ptr_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003767 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003768 verbose(env, "verifier internal error: unexpected ptr_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003769 return -EINVAL;
3770 }
3771 if (WARN_ON(!src_reg)) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003772 print_verifier_state(env, state);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003773 verbose(env, "verifier internal error: no src_reg\n");
Edward Creef1174f72017-08-07 15:26:19 +01003774 return -EINVAL;
3775 }
3776 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
Josef Bacik48461132016-09-28 10:54:32 -04003777}
3778
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003779/* check validity of 32-bit and 64-bit arithmetic operations */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003780static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003781{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07003782 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003783 u8 opcode = BPF_OP(insn->code);
3784 int err;
3785
3786 if (opcode == BPF_END || opcode == BPF_NEG) {
3787 if (opcode == BPF_NEG) {
3788 if (BPF_SRC(insn->code) != 0 ||
3789 insn->src_reg != BPF_REG_0 ||
3790 insn->off != 0 || insn->imm != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003791 verbose(env, "BPF_NEG uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003792 return -EINVAL;
3793 }
3794 } else {
3795 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
Edward Creee67b8a62017-09-15 14:37:38 +01003796 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
3797 BPF_CLASS(insn->code) == BPF_ALU64) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003798 verbose(env, "BPF_END uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003799 return -EINVAL;
3800 }
3801 }
3802
3803 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003804 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003805 if (err)
3806 return err;
3807
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003808 if (is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003809 verbose(env, "R%d pointer arithmetic prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003810 insn->dst_reg);
3811 return -EACCES;
3812 }
3813
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003814 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003815 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003816 if (err)
3817 return err;
3818
3819 } else if (opcode == BPF_MOV) {
3820
3821 if (BPF_SRC(insn->code) == BPF_X) {
3822 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003823 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003824 return -EINVAL;
3825 }
3826
3827 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01003828 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003829 if (err)
3830 return err;
3831 } else {
3832 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003833 verbose(env, "BPF_MOV uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003834 return -EINVAL;
3835 }
3836 }
3837
Arthur Fabrefbeb1602018-07-31 18:17:22 +01003838 /* check dest operand, mark as required later */
3839 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003840 if (err)
3841 return err;
3842
3843 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wange434b8c2018-12-07 12:16:18 -05003844 struct bpf_reg_state *src_reg = regs + insn->src_reg;
3845 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
3846
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003847 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3848 /* case: R1 = R2
3849 * copy register state to dest reg
3850 */
Jiong Wange434b8c2018-12-07 12:16:18 -05003851 *dst_reg = *src_reg;
3852 dst_reg->live |= REG_LIVE_WRITTEN;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003853 } else {
Edward Creef1174f72017-08-07 15:26:19 +01003854 /* R1 = (u32) R2 */
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003855 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003856 verbose(env,
3857 "R%d partial copy of pointer\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003858 insn->src_reg);
3859 return -EACCES;
Jiong Wange434b8c2018-12-07 12:16:18 -05003860 } else if (src_reg->type == SCALAR_VALUE) {
3861 *dst_reg = *src_reg;
3862 dst_reg->live |= REG_LIVE_WRITTEN;
3863 } else {
3864 mark_reg_unknown(env, regs,
3865 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07003866 }
Jiong Wange434b8c2018-12-07 12:16:18 -05003867 coerce_reg_to_size(dst_reg, 4);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003868 }
3869 } else {
3870 /* case: R = imm
3871 * remember the value we stored into this reg
3872 */
Arthur Fabrefbeb1602018-07-31 18:17:22 +01003873 /* clear any state __mark_reg_known doesn't set */
3874 mark_reg_unknown(env, regs, insn->dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01003875 regs[insn->dst_reg].type = SCALAR_VALUE;
Jann Horn95a762e2017-12-18 20:11:54 -08003876 if (BPF_CLASS(insn->code) == BPF_ALU64) {
3877 __mark_reg_known(regs + insn->dst_reg,
3878 insn->imm);
3879 } else {
3880 __mark_reg_known(regs + insn->dst_reg,
3881 (u32)insn->imm);
3882 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003883 }
3884
3885 } else if (opcode > BPF_END) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003886 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003887 return -EINVAL;
3888
3889 } else { /* all other ALU ops: and, sub, xor, add, ... */
3890
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003891 if (BPF_SRC(insn->code) == BPF_X) {
3892 if (insn->imm != 0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003893 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003894 return -EINVAL;
3895 }
3896 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003897 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003898 if (err)
3899 return err;
3900 } else {
3901 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003902 verbose(env, "BPF_ALU uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003903 return -EINVAL;
3904 }
3905 }
3906
3907 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01003908 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003909 if (err)
3910 return err;
3911
3912 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
3913 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003914 verbose(env, "div by zero\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003915 return -EINVAL;
3916 }
3917
Rabin Vincent229394e82016-01-12 20:17:08 +01003918 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
3919 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
3920 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
3921
3922 if (insn->imm < 0 || insn->imm >= size) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07003923 verbose(env, "invalid shift %d\n", insn->imm);
Rabin Vincent229394e82016-01-12 20:17:08 +01003924 return -EINVAL;
3925 }
3926 }
3927
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003928 /* check dest operand */
Edward Creedc503a82017-08-15 20:34:35 +01003929 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07003930 if (err)
3931 return err;
3932
Edward Creef1174f72017-08-07 15:26:19 +01003933 return adjust_reg_min_max_vals(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07003934 }
3935
3936 return 0;
3937}
3938
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003939static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02003940 struct bpf_reg_state *dst_reg,
David S. Millerf8ddadc2017-10-22 13:36:53 +01003941 enum bpf_reg_type type,
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003942 bool range_right_open)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07003943{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003944 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003945 struct bpf_reg_state *regs = state->regs, *reg;
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003946 u16 new_range;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08003947 int i, j;
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003948
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003949 if (dst_reg->off < 0 ||
3950 (dst_reg->off == 0 && range_right_open))
Edward Creef1174f72017-08-07 15:26:19 +01003951 /* This doesn't give us any range */
3952 return;
3953
Edward Creeb03c9f92017-08-07 15:26:36 +01003954 if (dst_reg->umax_value > MAX_PACKET_OFF ||
3955 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
Edward Creef1174f72017-08-07 15:26:19 +01003956 /* Risk of overflow. For instance, ptr + (1<<63) may be less
3957 * than pkt_end, but that's because it's also less than pkt.
3958 */
3959 return;
3960
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003961 new_range = dst_reg->off;
3962 if (range_right_open)
3963 new_range--;
3964
3965 /* Examples for register markings:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003966 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003967 * pkt_data in dst register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003968 *
3969 * r2 = r3;
3970 * r2 += 8;
3971 * if (r2 > pkt_end) goto <handle exception>
3972 * <access okay>
3973 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003974 * r2 = r3;
3975 * r2 += 8;
3976 * if (r2 < pkt_end) goto <access okay>
3977 * <handle exception>
3978 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003979 * Where:
3980 * r2 == dst_reg, pkt_end == src_reg
3981 * r2=pkt(id=n,off=8,r=0)
3982 * r3=pkt(id=n,off=0,r=0)
3983 *
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02003984 * pkt_data in src register:
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003985 *
3986 * r2 = r3;
3987 * r2 += 8;
3988 * if (pkt_end >= r2) goto <access okay>
3989 * <handle exception>
3990 *
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02003991 * r2 = r3;
3992 * r2 += 8;
3993 * if (pkt_end <= r2) goto <handle exception>
3994 * <access okay>
3995 *
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02003996 * Where:
3997 * pkt_end == dst_reg, r2 == src_reg
3998 * r2=pkt(id=n,off=8,r=0)
3999 * r3=pkt(id=n,off=0,r=0)
4000 *
4001 * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004002 * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4003 * and [r3, r3 + 8-1) respectively is safe to access depending on
4004 * the check.
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004005 */
Daniel Borkmann2d2be8c2016-09-08 01:03:42 +02004006
Edward Creef1174f72017-08-07 15:26:19 +01004007 /* If our ids match, then we must have the same max_value. And we
4008 * don't care about the other reg's fixed offset, since if it's too big
4009 * the range won't allow anything.
4010 * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4011 */
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004012 for (i = 0; i < MAX_BPF_REG; i++)
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02004013 if (regs[i].type == type && regs[i].id == dst_reg->id)
Alexei Starovoitovb1977682017-03-24 15:57:33 -07004014 /* keep the maximum range already checked */
Daniel Borkmannfb2a3112017-10-21 02:34:21 +02004015 regs[i].range = max(regs[i].range, new_range);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004016
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004017 for (j = 0; j <= vstate->curframe; j++) {
4018 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004019 bpf_for_each_spilled_reg(i, state, reg) {
4020 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004021 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004022 if (reg->type == type && reg->id == dst_reg->id)
4023 reg->range = max(reg->range, new_range);
4024 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07004025 }
4026}
4027
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004028/* compute branch direction of the expression "if (reg opcode val) goto target;"
4029 * and return:
4030 * 1 - branch will be taken and "goto target" will be executed
4031 * 0 - branch will not be taken and fall-through to next insn
4032 * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4033 */
Jiong Wang092ed092019-01-26 12:26:01 -05004034static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4035 bool is_jmp32)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004036{
Jiong Wang092ed092019-01-26 12:26:01 -05004037 struct bpf_reg_state reg_lo;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004038 s64 sval;
4039
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004040 if (__is_pointer_value(false, reg))
4041 return -1;
4042
Jiong Wang092ed092019-01-26 12:26:01 -05004043 if (is_jmp32) {
4044 reg_lo = *reg;
4045 reg = &reg_lo;
4046 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4047 * could truncate high bits and update umin/umax according to
4048 * information of low bits.
4049 */
4050 coerce_reg_to_size(reg, 4);
4051 /* smin/smax need special handling. For example, after coerce,
4052 * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4053 * used as operand to JMP32. It is a negative number from s32's
4054 * point of view, while it is a positive number when seen as
4055 * s64. The smin/smax are kept as s64, therefore, when used with
4056 * JMP32, they need to be transformed into s32, then sign
4057 * extended back to s64.
4058 *
4059 * Also, smin/smax were copied from umin/umax. If umin/umax has
4060 * different sign bit, then min/max relationship doesn't
4061 * maintain after casting into s32, for this case, set smin/smax
4062 * to safest range.
4063 */
4064 if ((reg->umax_value ^ reg->umin_value) &
4065 (1ULL << 31)) {
4066 reg->smin_value = S32_MIN;
4067 reg->smax_value = S32_MAX;
4068 }
4069 reg->smin_value = (s64)(s32)reg->smin_value;
4070 reg->smax_value = (s64)(s32)reg->smax_value;
4071
4072 val = (u32)val;
4073 sval = (s64)(s32)val;
4074 } else {
4075 sval = (s64)val;
4076 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004077
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004078 switch (opcode) {
4079 case BPF_JEQ:
4080 if (tnum_is_const(reg->var_off))
4081 return !!tnum_equals_const(reg->var_off, val);
4082 break;
4083 case BPF_JNE:
4084 if (tnum_is_const(reg->var_off))
4085 return !tnum_equals_const(reg->var_off, val);
4086 break;
Jakub Kicinski960ea052018-12-19 22:13:04 -08004087 case BPF_JSET:
4088 if ((~reg->var_off.mask & reg->var_off.value) & val)
4089 return 1;
4090 if (!((reg->var_off.mask | reg->var_off.value) & val))
4091 return 0;
4092 break;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004093 case BPF_JGT:
4094 if (reg->umin_value > val)
4095 return 1;
4096 else if (reg->umax_value <= val)
4097 return 0;
4098 break;
4099 case BPF_JSGT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004100 if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004101 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004102 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004103 return 0;
4104 break;
4105 case BPF_JLT:
4106 if (reg->umax_value < val)
4107 return 1;
4108 else if (reg->umin_value >= val)
4109 return 0;
4110 break;
4111 case BPF_JSLT:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004112 if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004113 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004114 else if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004115 return 0;
4116 break;
4117 case BPF_JGE:
4118 if (reg->umin_value >= val)
4119 return 1;
4120 else if (reg->umax_value < val)
4121 return 0;
4122 break;
4123 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004124 if (reg->smin_value >= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004125 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004126 else if (reg->smax_value < sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004127 return 0;
4128 break;
4129 case BPF_JLE:
4130 if (reg->umax_value <= val)
4131 return 1;
4132 else if (reg->umin_value > val)
4133 return 0;
4134 break;
4135 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004136 if (reg->smax_value <= sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004137 return 1;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004138 else if (reg->smin_value > sval)
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004139 return 0;
4140 break;
4141 }
4142
4143 return -1;
4144}
4145
Jiong Wang092ed092019-01-26 12:26:01 -05004146/* Generate min value of the high 32-bit from TNUM info. */
4147static u64 gen_hi_min(struct tnum var)
4148{
4149 return var.value & ~0xffffffffULL;
4150}
4151
4152/* Generate max value of the high 32-bit from TNUM info. */
4153static u64 gen_hi_max(struct tnum var)
4154{
4155 return (var.value | var.mask) & ~0xffffffffULL;
4156}
4157
4158/* Return true if VAL is compared with a s64 sign extended from s32, and they
4159 * are with the same signedness.
4160 */
4161static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4162{
4163 return ((s32)sval >= 0 &&
4164 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4165 ((s32)sval < 0 &&
4166 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4167}
4168
Josef Bacik48461132016-09-28 10:54:32 -04004169/* Adjusts the register min/max values in the case that the dst_reg is the
4170 * variable register that we are working on, and src_reg is a constant or we're
4171 * simply doing a BPF_K check.
Edward Creef1174f72017-08-07 15:26:19 +01004172 * In JEQ/JNE cases we also adjust the var_off values.
Josef Bacik48461132016-09-28 10:54:32 -04004173 */
4174static void reg_set_min_max(struct bpf_reg_state *true_reg,
4175 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004176 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004177{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004178 s64 sval;
4179
Edward Creef1174f72017-08-07 15:26:19 +01004180 /* If the dst_reg is a pointer, we can't learn anything about its
4181 * variable offset from the compare (unless src_reg were a pointer into
4182 * the same object, but we don't bother with that.
4183 * Since false_reg and true_reg have the same type by construction, we
4184 * only need to check one of them for pointerness.
4185 */
4186 if (__is_pointer_value(false, false_reg))
4187 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004188
Jiong Wang092ed092019-01-26 12:26:01 -05004189 val = is_jmp32 ? (u32)val : val;
4190 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004191
Josef Bacik48461132016-09-28 10:54:32 -04004192 switch (opcode) {
4193 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004194 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004195 {
4196 struct bpf_reg_state *reg =
4197 opcode == BPF_JEQ ? true_reg : false_reg;
4198
4199 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4200 * if it is true we know the value for sure. Likewise for
4201 * BPF_JNE.
Josef Bacik48461132016-09-28 10:54:32 -04004202 */
Jiong Wang092ed092019-01-26 12:26:01 -05004203 if (is_jmp32) {
4204 u64 old_v = reg->var_off.value;
4205 u64 hi_mask = ~0xffffffffULL;
4206
4207 reg->var_off.value = (old_v & hi_mask) | val;
4208 reg->var_off.mask &= hi_mask;
4209 } else {
4210 __mark_reg_known(reg, val);
4211 }
Josef Bacik48461132016-09-28 10:54:32 -04004212 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004213 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004214 case BPF_JSET:
4215 false_reg->var_off = tnum_and(false_reg->var_off,
4216 tnum_const(~val));
4217 if (is_power_of_2(val))
4218 true_reg->var_off = tnum_or(true_reg->var_off,
4219 tnum_const(val));
4220 break;
Josef Bacik48461132016-09-28 10:54:32 -04004221 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004222 case BPF_JGT:
4223 {
4224 u64 false_umax = opcode == BPF_JGT ? val : val - 1;
4225 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4226
Jiong Wang092ed092019-01-26 12:26:01 -05004227 if (is_jmp32) {
4228 false_umax += gen_hi_max(false_reg->var_off);
4229 true_umin += gen_hi_min(true_reg->var_off);
4230 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004231 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4232 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Edward Creeb03c9f92017-08-07 15:26:36 +01004233 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004234 }
Josef Bacik48461132016-09-28 10:54:32 -04004235 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004236 case BPF_JSGT:
4237 {
4238 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
4239 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4240
Jiong Wang092ed092019-01-26 12:26:01 -05004241 /* If the full s64 was not sign-extended from s32 then don't
4242 * deduct further info.
4243 */
4244 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4245 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004246 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4247 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Josef Bacik48461132016-09-28 10:54:32 -04004248 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004249 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004250 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004251 case BPF_JLT:
4252 {
4253 u64 false_umin = opcode == BPF_JLT ? val : val + 1;
4254 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4255
Jiong Wang092ed092019-01-26 12:26:01 -05004256 if (is_jmp32) {
4257 false_umin += gen_hi_min(false_reg->var_off);
4258 true_umax += gen_hi_max(true_reg->var_off);
4259 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004260 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4261 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004262 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004263 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004264 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004265 case BPF_JSLT:
4266 {
4267 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
4268 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4269
Jiong Wang092ed092019-01-26 12:26:01 -05004270 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4271 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004272 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4273 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004274 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004275 }
Josef Bacik48461132016-09-28 10:54:32 -04004276 default:
4277 break;
4278 }
4279
Edward Creeb03c9f92017-08-07 15:26:36 +01004280 __reg_deduce_bounds(false_reg);
4281 __reg_deduce_bounds(true_reg);
4282 /* We might have learned some bits from the bounds. */
4283 __reg_bound_offset(false_reg);
4284 __reg_bound_offset(true_reg);
4285 /* Intersecting with the old var_off might have improved our bounds
4286 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4287 * then new var_off is (0; 0x7f...fc) which improves our umax.
4288 */
4289 __update_reg_bounds(false_reg);
4290 __update_reg_bounds(true_reg);
Josef Bacik48461132016-09-28 10:54:32 -04004291}
4292
Edward Creef1174f72017-08-07 15:26:19 +01004293/* Same as above, but for the case that dst_reg holds a constant and src_reg is
4294 * the variable reg.
Josef Bacik48461132016-09-28 10:54:32 -04004295 */
4296static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4297 struct bpf_reg_state *false_reg, u64 val,
Jiong Wang092ed092019-01-26 12:26:01 -05004298 u8 opcode, bool is_jmp32)
Josef Bacik48461132016-09-28 10:54:32 -04004299{
Jiong Wanga72dafa2019-01-26 12:26:00 -05004300 s64 sval;
4301
Edward Creef1174f72017-08-07 15:26:19 +01004302 if (__is_pointer_value(false, false_reg))
4303 return;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004304
Jiong Wang092ed092019-01-26 12:26:01 -05004305 val = is_jmp32 ? (u32)val : val;
4306 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004307
Josef Bacik48461132016-09-28 10:54:32 -04004308 switch (opcode) {
4309 case BPF_JEQ:
Josef Bacik48461132016-09-28 10:54:32 -04004310 case BPF_JNE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004311 {
4312 struct bpf_reg_state *reg =
4313 opcode == BPF_JEQ ? true_reg : false_reg;
4314
Jiong Wang092ed092019-01-26 12:26:01 -05004315 if (is_jmp32) {
4316 u64 old_v = reg->var_off.value;
4317 u64 hi_mask = ~0xffffffffULL;
4318
4319 reg->var_off.value = (old_v & hi_mask) | val;
4320 reg->var_off.mask &= hi_mask;
4321 } else {
4322 __mark_reg_known(reg, val);
4323 }
Josef Bacik48461132016-09-28 10:54:32 -04004324 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004325 }
Jakub Kicinski960ea052018-12-19 22:13:04 -08004326 case BPF_JSET:
4327 false_reg->var_off = tnum_and(false_reg->var_off,
4328 tnum_const(~val));
4329 if (is_power_of_2(val))
4330 true_reg->var_off = tnum_or(true_reg->var_off,
4331 tnum_const(val));
4332 break;
Josef Bacik48461132016-09-28 10:54:32 -04004333 case BPF_JGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004334 case BPF_JGT:
4335 {
4336 u64 false_umin = opcode == BPF_JGT ? val : val + 1;
4337 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4338
Jiong Wang092ed092019-01-26 12:26:01 -05004339 if (is_jmp32) {
4340 false_umin += gen_hi_min(false_reg->var_off);
4341 true_umax += gen_hi_max(true_reg->var_off);
4342 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004343 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4344 true_reg->umax_value = min(true_reg->umax_value, true_umax);
Edward Creeb03c9f92017-08-07 15:26:36 +01004345 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004346 }
Josef Bacik48461132016-09-28 10:54:32 -04004347 case BPF_JSGE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004348 case BPF_JSGT:
4349 {
4350 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
4351 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
4352
Jiong Wang092ed092019-01-26 12:26:01 -05004353 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4354 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004355 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4356 true_reg->smax_value = min(true_reg->smax_value, true_smax);
Josef Bacik48461132016-09-28 10:54:32 -04004357 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004358 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004359 case BPF_JLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004360 case BPF_JLT:
4361 {
4362 u64 false_umax = opcode == BPF_JLT ? val : val - 1;
4363 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
4364
Jiong Wang092ed092019-01-26 12:26:01 -05004365 if (is_jmp32) {
4366 false_umax += gen_hi_max(false_reg->var_off);
4367 true_umin += gen_hi_min(true_reg->var_off);
4368 }
Jiong Wanga72dafa2019-01-26 12:26:00 -05004369 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4370 true_reg->umin_value = max(true_reg->umin_value, true_umin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004371 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004372 }
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004373 case BPF_JSLE:
Jiong Wanga72dafa2019-01-26 12:26:00 -05004374 case BPF_JSLT:
4375 {
4376 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
4377 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
4378
Jiong Wang092ed092019-01-26 12:26:01 -05004379 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4380 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004381 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4382 true_reg->smin_value = max(true_reg->smin_value, true_smin);
Daniel Borkmannb4e432f2017-08-10 01:40:02 +02004383 break;
Jiong Wanga72dafa2019-01-26 12:26:00 -05004384 }
Josef Bacik48461132016-09-28 10:54:32 -04004385 default:
4386 break;
4387 }
4388
Edward Creeb03c9f92017-08-07 15:26:36 +01004389 __reg_deduce_bounds(false_reg);
4390 __reg_deduce_bounds(true_reg);
4391 /* We might have learned some bits from the bounds. */
4392 __reg_bound_offset(false_reg);
4393 __reg_bound_offset(true_reg);
4394 /* Intersecting with the old var_off might have improved our bounds
4395 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4396 * then new var_off is (0; 0x7f...fc) which improves our umax.
4397 */
4398 __update_reg_bounds(false_reg);
4399 __update_reg_bounds(true_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004400}
4401
4402/* Regs are known to be equal, so intersect their min/max/var_off */
4403static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
4404 struct bpf_reg_state *dst_reg)
4405{
Edward Creeb03c9f92017-08-07 15:26:36 +01004406 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
4407 dst_reg->umin_value);
4408 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
4409 dst_reg->umax_value);
4410 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
4411 dst_reg->smin_value);
4412 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
4413 dst_reg->smax_value);
Edward Creef1174f72017-08-07 15:26:19 +01004414 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
4415 dst_reg->var_off);
Edward Creeb03c9f92017-08-07 15:26:36 +01004416 /* We might have learned new bounds from the var_off. */
4417 __update_reg_bounds(src_reg);
4418 __update_reg_bounds(dst_reg);
4419 /* We might have learned something about the sign bit. */
4420 __reg_deduce_bounds(src_reg);
4421 __reg_deduce_bounds(dst_reg);
4422 /* We might have learned some bits from the bounds. */
4423 __reg_bound_offset(src_reg);
4424 __reg_bound_offset(dst_reg);
4425 /* Intersecting with the old var_off might have improved our bounds
4426 * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4427 * then new var_off is (0; 0x7f...fc) which improves our umax.
4428 */
4429 __update_reg_bounds(src_reg);
4430 __update_reg_bounds(dst_reg);
Edward Creef1174f72017-08-07 15:26:19 +01004431}
4432
4433static void reg_combine_min_max(struct bpf_reg_state *true_src,
4434 struct bpf_reg_state *true_dst,
4435 struct bpf_reg_state *false_src,
4436 struct bpf_reg_state *false_dst,
4437 u8 opcode)
4438{
4439 switch (opcode) {
4440 case BPF_JEQ:
4441 __reg_combine_min_max(true_src, true_dst);
4442 break;
4443 case BPF_JNE:
4444 __reg_combine_min_max(false_src, false_dst);
Edward Creeb03c9f92017-08-07 15:26:36 +01004445 break;
Daniel Borkmann4cabc5b2017-07-21 00:00:21 +02004446 }
Josef Bacik48461132016-09-28 10:54:32 -04004447}
4448
Joe Stringerfd978bf72018-10-02 13:35:35 -07004449static void mark_ptr_or_null_reg(struct bpf_func_state *state,
4450 struct bpf_reg_state *reg, u32 id,
Joe Stringer840b9612018-10-02 13:35:32 -07004451 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004452{
Joe Stringer840b9612018-10-02 13:35:32 -07004453 if (reg_type_may_be_null(reg->type) && reg->id == id) {
Edward Creef1174f72017-08-07 15:26:19 +01004454 /* Old offset (both fixed and variable parts) should
4455 * have been known-zero, because we don't allow pointer
4456 * arithmetic on pointers that might be NULL.
4457 */
Edward Creeb03c9f92017-08-07 15:26:36 +01004458 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
4459 !tnum_equals_const(reg->var_off, 0) ||
Edward Creef1174f72017-08-07 15:26:19 +01004460 reg->off)) {
Edward Creeb03c9f92017-08-07 15:26:36 +01004461 __mark_reg_known_zero(reg);
4462 reg->off = 0;
Edward Creef1174f72017-08-07 15:26:19 +01004463 }
4464 if (is_null) {
4465 reg->type = SCALAR_VALUE;
Joe Stringer840b9612018-10-02 13:35:32 -07004466 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
4467 if (reg->map_ptr->inner_map_meta) {
4468 reg->type = CONST_PTR_TO_MAP;
4469 reg->map_ptr = reg->map_ptr->inner_map_meta;
4470 } else {
4471 reg->type = PTR_TO_MAP_VALUE;
4472 }
Joe Stringerc64b7982018-10-02 13:35:33 -07004473 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
4474 reg->type = PTR_TO_SOCKET;
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07004475 }
Joe Stringerfd978bf72018-10-02 13:35:35 -07004476 if (is_null || !reg_is_refcounted(reg)) {
4477 /* We don't need id from this point onwards anymore,
4478 * thus we should better reset it, so that state
4479 * pruning has chances to take effect.
4480 */
4481 reg->id = 0;
4482 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004483 }
4484}
4485
4486/* The logic is similar to find_good_pkt_pointers(), both could eventually
4487 * be folded together at some point.
4488 */
Joe Stringer840b9612018-10-02 13:35:32 -07004489static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4490 bool is_null)
Thomas Graf57a09bf2016-10-18 19:51:19 +02004491{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004492 struct bpf_func_state *state = vstate->frame[vstate->curframe];
Joe Stringerf3709f62018-10-02 13:35:29 -07004493 struct bpf_reg_state *reg, *regs = state->regs;
Daniel Borkmanna08dd0d2016-12-15 01:30:06 +01004494 u32 id = regs[regno].id;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004495 int i, j;
Thomas Graf57a09bf2016-10-18 19:51:19 +02004496
Joe Stringerfd978bf72018-10-02 13:35:35 -07004497 if (reg_is_refcounted_or_null(&regs[regno]) && is_null)
4498 __release_reference_state(state, id);
4499
Thomas Graf57a09bf2016-10-18 19:51:19 +02004500 for (i = 0; i < MAX_BPF_REG; i++)
Joe Stringerfd978bf72018-10-02 13:35:35 -07004501 mark_ptr_or_null_reg(state, &regs[i], id, is_null);
Thomas Graf57a09bf2016-10-18 19:51:19 +02004502
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004503 for (j = 0; j <= vstate->curframe; j++) {
4504 state = vstate->frame[j];
Joe Stringerf3709f62018-10-02 13:35:29 -07004505 bpf_for_each_spilled_reg(i, state, reg) {
4506 if (!reg)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004507 continue;
Joe Stringerfd978bf72018-10-02 13:35:35 -07004508 mark_ptr_or_null_reg(state, reg, id, is_null);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004509 }
Thomas Graf57a09bf2016-10-18 19:51:19 +02004510 }
4511}
4512
Daniel Borkmann5beca082017-11-01 23:58:10 +01004513static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4514 struct bpf_reg_state *dst_reg,
4515 struct bpf_reg_state *src_reg,
4516 struct bpf_verifier_state *this_branch,
4517 struct bpf_verifier_state *other_branch)
4518{
4519 if (BPF_SRC(insn->code) != BPF_X)
4520 return false;
4521
Jiong Wang092ed092019-01-26 12:26:01 -05004522 /* Pointers are always 64-bit. */
4523 if (BPF_CLASS(insn->code) == BPF_JMP32)
4524 return false;
4525
Daniel Borkmann5beca082017-11-01 23:58:10 +01004526 switch (BPF_OP(insn->code)) {
4527 case BPF_JGT:
4528 if ((dst_reg->type == PTR_TO_PACKET &&
4529 src_reg->type == PTR_TO_PACKET_END) ||
4530 (dst_reg->type == PTR_TO_PACKET_META &&
4531 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4532 /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4533 find_good_pkt_pointers(this_branch, dst_reg,
4534 dst_reg->type, false);
4535 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4536 src_reg->type == PTR_TO_PACKET) ||
4537 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4538 src_reg->type == PTR_TO_PACKET_META)) {
4539 /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4540 find_good_pkt_pointers(other_branch, src_reg,
4541 src_reg->type, true);
4542 } else {
4543 return false;
4544 }
4545 break;
4546 case BPF_JLT:
4547 if ((dst_reg->type == PTR_TO_PACKET &&
4548 src_reg->type == PTR_TO_PACKET_END) ||
4549 (dst_reg->type == PTR_TO_PACKET_META &&
4550 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4551 /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4552 find_good_pkt_pointers(other_branch, dst_reg,
4553 dst_reg->type, true);
4554 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4555 src_reg->type == PTR_TO_PACKET) ||
4556 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4557 src_reg->type == PTR_TO_PACKET_META)) {
4558 /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4559 find_good_pkt_pointers(this_branch, src_reg,
4560 src_reg->type, false);
4561 } else {
4562 return false;
4563 }
4564 break;
4565 case BPF_JGE:
4566 if ((dst_reg->type == PTR_TO_PACKET &&
4567 src_reg->type == PTR_TO_PACKET_END) ||
4568 (dst_reg->type == PTR_TO_PACKET_META &&
4569 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4570 /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
4571 find_good_pkt_pointers(this_branch, dst_reg,
4572 dst_reg->type, true);
4573 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4574 src_reg->type == PTR_TO_PACKET) ||
4575 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4576 src_reg->type == PTR_TO_PACKET_META)) {
4577 /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
4578 find_good_pkt_pointers(other_branch, src_reg,
4579 src_reg->type, false);
4580 } else {
4581 return false;
4582 }
4583 break;
4584 case BPF_JLE:
4585 if ((dst_reg->type == PTR_TO_PACKET &&
4586 src_reg->type == PTR_TO_PACKET_END) ||
4587 (dst_reg->type == PTR_TO_PACKET_META &&
4588 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4589 /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
4590 find_good_pkt_pointers(other_branch, dst_reg,
4591 dst_reg->type, false);
4592 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4593 src_reg->type == PTR_TO_PACKET) ||
4594 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4595 src_reg->type == PTR_TO_PACKET_META)) {
4596 /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
4597 find_good_pkt_pointers(this_branch, src_reg,
4598 src_reg->type, true);
4599 } else {
4600 return false;
4601 }
4602 break;
4603 default:
4604 return false;
4605 }
4606
4607 return true;
4608}
4609
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004610static int check_cond_jmp_op(struct bpf_verifier_env *env,
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004611 struct bpf_insn *insn, int *insn_idx)
4612{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004613 struct bpf_verifier_state *this_branch = env->cur_state;
4614 struct bpf_verifier_state *other_branch;
4615 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
4616 struct bpf_reg_state *dst_reg, *other_branch_regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004617 u8 opcode = BPF_OP(insn->code);
Jiong Wang092ed092019-01-26 12:26:01 -05004618 bool is_jmp32;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004619 int err;
4620
Jiong Wang092ed092019-01-26 12:26:01 -05004621 /* Only conditional jumps are expected to reach here. */
4622 if (opcode == BPF_JA || opcode > BPF_JSLE) {
4623 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004624 return -EINVAL;
4625 }
4626
4627 if (BPF_SRC(insn->code) == BPF_X) {
4628 if (insn->imm != 0) {
Jiong Wang092ed092019-01-26 12:26:01 -05004629 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004630 return -EINVAL;
4631 }
4632
4633 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004634 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004635 if (err)
4636 return err;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004637
4638 if (is_pointer_value(env, insn->src_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004639 verbose(env, "R%d pointer comparison prohibited\n",
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004640 insn->src_reg);
4641 return -EACCES;
4642 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004643 } else {
4644 if (insn->src_reg != BPF_REG_0) {
Jiong Wang092ed092019-01-26 12:26:01 -05004645 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004646 return -EINVAL;
4647 }
4648 }
4649
4650 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01004651 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004652 if (err)
4653 return err;
4654
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004655 dst_reg = &regs[insn->dst_reg];
Jiong Wang092ed092019-01-26 12:26:01 -05004656 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004657
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004658 if (BPF_SRC(insn->code) == BPF_K) {
Jiong Wang092ed092019-01-26 12:26:01 -05004659 int pred = is_branch_taken(dst_reg, insn->imm, opcode,
4660 is_jmp32);
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004661
4662 if (pred == 1) {
4663 /* only follow the goto, ignore fall-through */
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004664 *insn_idx += insn->off;
4665 return 0;
Alexei Starovoitov4f7b3e82018-12-03 22:46:05 -08004666 } else if (pred == 0) {
4667 /* only follow fall-through branch, since
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004668 * that's where the program will go
4669 */
4670 return 0;
4671 }
4672 }
4673
Daniel Borkmann979d63d2019-01-03 00:58:34 +01004674 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
4675 false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004676 if (!other_branch)
4677 return -EFAULT;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004678 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004679
Josef Bacik48461132016-09-28 10:54:32 -04004680 /* detect if we are comparing against a constant value so we can adjust
4681 * our min/max values for our dst register.
Edward Creef1174f72017-08-07 15:26:19 +01004682 * this is only legit if both are scalars (or pointers to the same
4683 * object, I suppose, but we don't support that right now), because
4684 * otherwise the different base pointers mean the offsets aren't
4685 * comparable.
Josef Bacik48461132016-09-28 10:54:32 -04004686 */
4687 if (BPF_SRC(insn->code) == BPF_X) {
Jiong Wang092ed092019-01-26 12:26:01 -05004688 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
4689 struct bpf_reg_state lo_reg0 = *dst_reg;
4690 struct bpf_reg_state lo_reg1 = *src_reg;
4691 struct bpf_reg_state *src_lo, *dst_lo;
4692
4693 dst_lo = &lo_reg0;
4694 src_lo = &lo_reg1;
4695 coerce_reg_to_size(dst_lo, 4);
4696 coerce_reg_to_size(src_lo, 4);
4697
Edward Creef1174f72017-08-07 15:26:19 +01004698 if (dst_reg->type == SCALAR_VALUE &&
Jiong Wang092ed092019-01-26 12:26:01 -05004699 src_reg->type == SCALAR_VALUE) {
4700 if (tnum_is_const(src_reg->var_off) ||
4701 (is_jmp32 && tnum_is_const(src_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004702 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004703 dst_reg,
4704 is_jmp32
4705 ? src_lo->var_off.value
4706 : src_reg->var_off.value,
4707 opcode, is_jmp32);
4708 else if (tnum_is_const(dst_reg->var_off) ||
4709 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004710 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004711 src_reg,
4712 is_jmp32
4713 ? dst_lo->var_off.value
4714 : dst_reg->var_off.value,
4715 opcode, is_jmp32);
4716 else if (!is_jmp32 &&
4717 (opcode == BPF_JEQ || opcode == BPF_JNE))
Edward Creef1174f72017-08-07 15:26:19 +01004718 /* Comparing for equality, we can combine knowledge */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004719 reg_combine_min_max(&other_branch_regs[insn->src_reg],
4720 &other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004721 src_reg, dst_reg, opcode);
Edward Creef1174f72017-08-07 15:26:19 +01004722 }
4723 } else if (dst_reg->type == SCALAR_VALUE) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004724 reg_set_min_max(&other_branch_regs[insn->dst_reg],
Jiong Wang092ed092019-01-26 12:26:01 -05004725 dst_reg, insn->imm, opcode, is_jmp32);
Josef Bacik48461132016-09-28 10:54:32 -04004726 }
4727
Jiong Wang092ed092019-01-26 12:26:01 -05004728 /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
4729 * NOTE: these optimizations below are related with pointer comparison
4730 * which will never be JMP32.
4731 */
4732 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07004733 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
Joe Stringer840b9612018-10-02 13:35:32 -07004734 reg_type_may_be_null(dst_reg->type)) {
4735 /* Mark all identical registers in each branch as either
Thomas Graf57a09bf2016-10-18 19:51:19 +02004736 * safe or unknown depending R == 0 or R != 0 conditional.
4737 */
Joe Stringer840b9612018-10-02 13:35:32 -07004738 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
4739 opcode == BPF_JNE);
4740 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
4741 opcode == BPF_JEQ);
Daniel Borkmann5beca082017-11-01 23:58:10 +01004742 } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
4743 this_branch, other_branch) &&
4744 is_pointer_value(env, insn->dst_reg)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004745 verbose(env, "R%d pointer comparison prohibited\n",
4746 insn->dst_reg);
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07004747 return -EACCES;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004748 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004749 if (env->log.level)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004750 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004751 return 0;
4752}
4753
Alexei Starovoitov0246e642014-09-26 00:17:04 -07004754/* return the map pointer stored inside BPF_LD_IMM64 instruction */
4755static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
4756{
4757 u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32;
4758
4759 return (struct bpf_map *) (unsigned long) imm64;
4760}
4761
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004762/* verify BPF_LD_IMM64 instruction */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004763static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004764{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004765 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004766 int err;
4767
4768 if (BPF_SIZE(insn->code) != BPF_DW) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004769 verbose(env, "invalid BPF_LD_IMM insn\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004770 return -EINVAL;
4771 }
4772 if (insn->off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004773 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004774 return -EINVAL;
4775 }
4776
Edward Creedc503a82017-08-15 20:34:35 +01004777 err = check_reg_arg(env, insn->dst_reg, DST_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004778 if (err)
4779 return err;
4780
Jakub Kicinski6b173872016-09-21 11:43:59 +01004781 if (insn->src_reg == 0) {
Jakub Kicinski6b173872016-09-21 11:43:59 +01004782 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
4783
Edward Creef1174f72017-08-07 15:26:19 +01004784 regs[insn->dst_reg].type = SCALAR_VALUE;
Edward Creeb03c9f92017-08-07 15:26:36 +01004785 __mark_reg_known(&regs[insn->dst_reg], imm);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004786 return 0;
Jakub Kicinski6b173872016-09-21 11:43:59 +01004787 }
Alexei Starovoitov17a52672014-09-26 00:17:06 -07004788
4789 /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */
4790 BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD);
4791
4792 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
4793 regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn);
4794 return 0;
4795}
4796
Daniel Borkmann96be4322015-03-01 12:31:46 +01004797static bool may_access_skb(enum bpf_prog_type type)
4798{
4799 switch (type) {
4800 case BPF_PROG_TYPE_SOCKET_FILTER:
4801 case BPF_PROG_TYPE_SCHED_CLS:
Daniel Borkmann94caee8c2015-03-20 15:11:11 +01004802 case BPF_PROG_TYPE_SCHED_ACT:
Daniel Borkmann96be4322015-03-01 12:31:46 +01004803 return true;
4804 default:
4805 return false;
4806 }
4807}
4808
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004809/* verify safety of LD_ABS|LD_IND instructions:
4810 * - they can only appear in the programs where ctx == skb
4811 * - since they are wrappers of function calls, they scratch R1-R5 registers,
4812 * preserve R6-R9, and store return value into R0
4813 *
4814 * Implicit input:
4815 * ctx == skb == R6 == CTX
4816 *
4817 * Explicit input:
4818 * SRC == any register
4819 * IMM == 32-bit immediate
4820 *
4821 * Output:
4822 * R0 - 8/16/32-bit skb data converted to cpu endianness
4823 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004824static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004825{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004826 struct bpf_reg_state *regs = cur_regs(env);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004827 u8 mode = BPF_MODE(insn->code);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004828 int i, err;
4829
Daniel Borkmann24701ec2015-03-01 12:31:47 +01004830 if (!may_access_skb(env->prog->type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004831 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004832 return -EINVAL;
4833 }
4834
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02004835 if (!env->ops->gen_ld_abs) {
4836 verbose(env, "bpf verifier is misconfigured\n");
4837 return -EINVAL;
4838 }
4839
Jiong Wangf910cef2018-05-02 16:17:17 -04004840 if (env->subprog_cnt > 1) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08004841 /* when program has LD_ABS insn JITs and interpreter assume
4842 * that r1 == ctx == skb which is not the case for callees
4843 * that can have arbitrary arguments. It's problematic
4844 * for main prog as well since JITs would need to analyze
4845 * all functions in order to make proper register save/restore
4846 * decisions in the main prog. Hence disallow LD_ABS with calls
4847 */
4848 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
4849 return -EINVAL;
4850 }
4851
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004852 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
Alexei Starovoitovd82bccc2016-04-12 10:26:19 -07004853 BPF_SIZE(insn->code) == BPF_DW ||
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004854 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004855 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004856 return -EINVAL;
4857 }
4858
4859 /* check whether implicit source operand (register R6) is readable */
Edward Creedc503a82017-08-15 20:34:35 +01004860 err = check_reg_arg(env, BPF_REG_6, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004861 if (err)
4862 return err;
4863
Joe Stringerfd978bf72018-10-02 13:35:35 -07004864 /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
4865 * gen_ld_abs() may terminate the program at runtime, leading to
4866 * reference leak.
4867 */
4868 err = check_reference_leak(env);
4869 if (err) {
4870 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
4871 return err;
4872 }
4873
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004874 if (regs[BPF_REG_6].type != PTR_TO_CTX) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004875 verbose(env,
4876 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004877 return -EINVAL;
4878 }
4879
4880 if (mode == BPF_IND) {
4881 /* check explicit source operand */
Edward Creedc503a82017-08-15 20:34:35 +01004882 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004883 if (err)
4884 return err;
4885 }
4886
4887 /* reset caller saved regs to unreadable */
Edward Creedc503a82017-08-15 20:34:35 +01004888 for (i = 0; i < CALLER_SAVED_REGS; i++) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004889 mark_reg_not_init(env, regs, caller_saved[i]);
Edward Creedc503a82017-08-15 20:34:35 +01004890 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4891 }
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004892
4893 /* mark destination R0 register as readable, since it contains
Edward Creedc503a82017-08-15 20:34:35 +01004894 * the value fetched from the packet.
4895 * Already marked as written above.
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004896 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004897 mark_reg_unknown(env, regs, BPF_REG_0);
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08004898 return 0;
4899}
4900
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004901static int check_return_code(struct bpf_verifier_env *env)
4902{
4903 struct bpf_reg_state *reg;
4904 struct tnum range = tnum_range(0, 1);
4905
4906 switch (env->prog->type) {
4907 case BPF_PROG_TYPE_CGROUP_SKB:
4908 case BPF_PROG_TYPE_CGROUP_SOCK:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07004909 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004910 case BPF_PROG_TYPE_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05004911 case BPF_PROG_TYPE_CGROUP_DEVICE:
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004912 break;
4913 default:
4914 return 0;
4915 }
4916
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07004917 reg = cur_regs(env) + BPF_REG_0;
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004918 if (reg->type != SCALAR_VALUE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004919 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004920 reg_type_str[reg->type]);
4921 return -EINVAL;
4922 }
4923
4924 if (!tnum_in(range, reg->var_off)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004925 verbose(env, "At program exit the register R0 ");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004926 if (!tnum_is_unknown(reg->var_off)) {
4927 char tn_buf[48];
4928
4929 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004930 verbose(env, "has value %s", tn_buf);
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004931 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004932 verbose(env, "has unknown scalar value");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004933 }
Jakub Kicinski61bd5212017-10-09 10:30:11 -07004934 verbose(env, " should have been 0 or 1\n");
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07004935 return -EINVAL;
4936 }
4937 return 0;
4938}
4939
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004940/* non-recursive DFS pseudo code
4941 * 1 procedure DFS-iterative(G,v):
4942 * 2 label v as discovered
4943 * 3 let S be a stack
4944 * 4 S.push(v)
4945 * 5 while S is not empty
4946 * 6 t <- S.pop()
4947 * 7 if t is what we're looking for:
4948 * 8 return t
4949 * 9 for all edges e in G.adjacentEdges(t) do
4950 * 10 if edge e is already labelled
4951 * 11 continue with the next edge
4952 * 12 w <- G.adjacentVertex(t,e)
4953 * 13 if vertex w is not discovered and not explored
4954 * 14 label e as tree-edge
4955 * 15 label w as discovered
4956 * 16 S.push(w)
4957 * 17 continue at 5
4958 * 18 else if vertex w is discovered
4959 * 19 label e as back-edge
4960 * 20 else
4961 * 21 // vertex w is explored
4962 * 22 label e as forward- or cross-edge
4963 * 23 label t as explored
4964 * 24 S.pop()
4965 *
4966 * convention:
4967 * 0x10 - discovered
4968 * 0x11 - discovered and fall-through edge labelled
4969 * 0x12 - discovered and fall-through and branch edges labelled
4970 * 0x20 - explored
4971 */
4972
4973enum {
4974 DISCOVERED = 0x10,
4975 EXPLORED = 0x20,
4976 FALLTHROUGH = 1,
4977 BRANCH = 2,
4978};
4979
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004980#define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07004981
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004982static int *insn_stack; /* stack of insns to process */
4983static int cur_stack; /* current stack index */
4984static int *insn_state;
4985
4986/* t, w, e - match pseudo-code above:
4987 * t - index of current instruction
4988 * w - next instruction
4989 * e - edge
4990 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01004991static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07004992{
4993 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
4994 return 0;
4995
4996 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
4997 return 0;
4998
4999 if (w < 0 || w >= env->prog->len) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005000 verbose_linfo(env, t, "%d: ", t);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005001 verbose(env, "jump out of range from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005002 return -EINVAL;
5003 }
5004
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005005 if (e == BRANCH)
5006 /* mark branch target for state pruning */
5007 env->explored_states[w] = STATE_LIST_MARK;
5008
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005009 if (insn_state[w] == 0) {
5010 /* tree-edge */
5011 insn_state[t] = DISCOVERED | e;
5012 insn_state[w] = DISCOVERED;
5013 if (cur_stack >= env->prog->len)
5014 return -E2BIG;
5015 insn_stack[cur_stack++] = w;
5016 return 1;
5017 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
Martin KaFai Laud9762e82018-12-13 10:41:48 -08005018 verbose_linfo(env, t, "%d: ", t);
5019 verbose_linfo(env, w, "%d: ", w);
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005020 verbose(env, "back-edge from insn %d to %d\n", t, w);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005021 return -EINVAL;
5022 } else if (insn_state[w] == EXPLORED) {
5023 /* forward- or cross-edge */
5024 insn_state[t] = DISCOVERED | e;
5025 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005026 verbose(env, "insn state internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005027 return -EFAULT;
5028 }
5029 return 0;
5030}
5031
5032/* non-recursive depth-first-search to detect loops in BPF program
5033 * loop == back-edge in directed graph
5034 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005035static int check_cfg(struct bpf_verifier_env *env)
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005036{
5037 struct bpf_insn *insns = env->prog->insnsi;
5038 int insn_cnt = env->prog->len;
5039 int ret = 0;
5040 int i, t;
5041
5042 insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5043 if (!insn_state)
5044 return -ENOMEM;
5045
5046 insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5047 if (!insn_stack) {
5048 kfree(insn_state);
5049 return -ENOMEM;
5050 }
5051
5052 insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5053 insn_stack[0] = 0; /* 0 is the first instruction */
5054 cur_stack = 1;
5055
5056peek_stack:
5057 if (cur_stack == 0)
5058 goto check_state;
5059 t = insn_stack[cur_stack - 1];
5060
Jiong Wang092ed092019-01-26 12:26:01 -05005061 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5062 BPF_CLASS(insns[t].code) == BPF_JMP32) {
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005063 u8 opcode = BPF_OP(insns[t].code);
5064
5065 if (opcode == BPF_EXIT) {
5066 goto mark_explored;
5067 } else if (opcode == BPF_CALL) {
5068 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5069 if (ret == 1)
5070 goto peek_stack;
5071 else if (ret < 0)
5072 goto err_free;
Daniel Borkmann07016152016-04-05 22:33:17 +02005073 if (t + 1 < insn_cnt)
5074 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08005075 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
5076 env->explored_states[t] = STATE_LIST_MARK;
5077 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
5078 if (ret == 1)
5079 goto peek_stack;
5080 else if (ret < 0)
5081 goto err_free;
5082 }
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005083 } else if (opcode == BPF_JA) {
5084 if (BPF_SRC(insns[t].code) != BPF_K) {
5085 ret = -EINVAL;
5086 goto err_free;
5087 }
5088 /* unconditional jump with single edge */
5089 ret = push_insn(t, t + insns[t].off + 1,
5090 FALLTHROUGH, env);
5091 if (ret == 1)
5092 goto peek_stack;
5093 else if (ret < 0)
5094 goto err_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005095 /* tell verifier to check for equivalent states
5096 * after every call and jump
5097 */
Alexei Starovoitovc3de6312015-04-14 15:57:13 -07005098 if (t + 1 < insn_cnt)
5099 env->explored_states[t + 1] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005100 } else {
5101 /* conditional jump with two edges */
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02005102 env->explored_states[t] = STATE_LIST_MARK;
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005103 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5104 if (ret == 1)
5105 goto peek_stack;
5106 else if (ret < 0)
5107 goto err_free;
5108
5109 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
5110 if (ret == 1)
5111 goto peek_stack;
5112 else if (ret < 0)
5113 goto err_free;
5114 }
5115 } else {
5116 /* all other non-branch instructions with single
5117 * fall-through edge
5118 */
5119 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5120 if (ret == 1)
5121 goto peek_stack;
5122 else if (ret < 0)
5123 goto err_free;
5124 }
5125
5126mark_explored:
5127 insn_state[t] = EXPLORED;
5128 if (cur_stack-- <= 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005129 verbose(env, "pop stack internal bug\n");
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005130 ret = -EFAULT;
5131 goto err_free;
5132 }
5133 goto peek_stack;
5134
5135check_state:
5136 for (i = 0; i < insn_cnt; i++) {
5137 if (insn_state[i] != EXPLORED) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07005138 verbose(env, "unreachable insn %d\n", i);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07005139 ret = -EINVAL;
5140 goto err_free;
5141 }
5142 }
5143 ret = 0; /* cfg looks good */
5144
5145err_free:
5146 kfree(insn_state);
5147 kfree(insn_stack);
5148 return ret;
5149}
5150
Yonghong Song838e9692018-11-19 15:29:11 -08005151/* The minimum supported BTF func info size */
5152#define MIN_BPF_FUNCINFO_SIZE 8
5153#define MAX_FUNCINFO_REC_SIZE 252
5154
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005155static int check_btf_func(struct bpf_verifier_env *env,
5156 const union bpf_attr *attr,
5157 union bpf_attr __user *uattr)
Yonghong Song838e9692018-11-19 15:29:11 -08005158{
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005159 u32 i, nfuncs, urec_size, min_size;
Yonghong Song838e9692018-11-19 15:29:11 -08005160 u32 krec_size = sizeof(struct bpf_func_info);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005161 struct bpf_func_info *krecord;
Yonghong Song838e9692018-11-19 15:29:11 -08005162 const struct btf_type *type;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005163 struct bpf_prog *prog;
5164 const struct btf *btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005165 void __user *urecord;
Peter Oskolkovd0b28182019-01-16 10:43:01 -08005166 u32 prev_offset = 0;
Yonghong Song838e9692018-11-19 15:29:11 -08005167 int ret = 0;
5168
5169 nfuncs = attr->func_info_cnt;
5170 if (!nfuncs)
5171 return 0;
5172
5173 if (nfuncs != env->subprog_cnt) {
5174 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5175 return -EINVAL;
5176 }
5177
5178 urec_size = attr->func_info_rec_size;
5179 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5180 urec_size > MAX_FUNCINFO_REC_SIZE ||
5181 urec_size % sizeof(u32)) {
5182 verbose(env, "invalid func info rec size %u\n", urec_size);
5183 return -EINVAL;
5184 }
5185
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005186 prog = env->prog;
5187 btf = prog->aux->btf;
Yonghong Song838e9692018-11-19 15:29:11 -08005188
5189 urecord = u64_to_user_ptr(attr->func_info);
5190 min_size = min_t(u32, krec_size, urec_size);
5191
Yonghong Songba64e7d2018-11-24 23:20:44 -08005192 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005193 if (!krecord)
5194 return -ENOMEM;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005195
Yonghong Song838e9692018-11-19 15:29:11 -08005196 for (i = 0; i < nfuncs; i++) {
5197 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5198 if (ret) {
5199 if (ret == -E2BIG) {
5200 verbose(env, "nonzero tailing record in func info");
5201 /* set the size kernel expects so loader can zero
5202 * out the rest of the record.
5203 */
5204 if (put_user(min_size, &uattr->func_info_rec_size))
5205 ret = -EFAULT;
5206 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005207 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005208 }
5209
Yonghong Songba64e7d2018-11-24 23:20:44 -08005210 if (copy_from_user(&krecord[i], urecord, min_size)) {
Yonghong Song838e9692018-11-19 15:29:11 -08005211 ret = -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005212 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005213 }
5214
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005215 /* check insn_off */
Yonghong Song838e9692018-11-19 15:29:11 -08005216 if (i == 0) {
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005217 if (krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005218 verbose(env,
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005219 "nonzero insn_off %u for the first func info record",
5220 krecord[i].insn_off);
Yonghong Song838e9692018-11-19 15:29:11 -08005221 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005222 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005223 }
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005224 } else if (krecord[i].insn_off <= prev_offset) {
Yonghong Song838e9692018-11-19 15:29:11 -08005225 verbose(env,
5226 "same or smaller insn offset (%u) than previous func info record (%u)",
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005227 krecord[i].insn_off, prev_offset);
Yonghong Song838e9692018-11-19 15:29:11 -08005228 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005229 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005230 }
5231
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005232 if (env->subprog_info[i].start != krecord[i].insn_off) {
Yonghong Song838e9692018-11-19 15:29:11 -08005233 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5234 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005235 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005236 }
5237
5238 /* check type_id */
Yonghong Songba64e7d2018-11-24 23:20:44 -08005239 type = btf_type_by_id(btf, krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005240 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5241 verbose(env, "invalid type id %d in func info",
Yonghong Songba64e7d2018-11-24 23:20:44 -08005242 krecord[i].type_id);
Yonghong Song838e9692018-11-19 15:29:11 -08005243 ret = -EINVAL;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005244 goto err_free;
Yonghong Song838e9692018-11-19 15:29:11 -08005245 }
5246
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005247 prev_offset = krecord[i].insn_off;
Yonghong Song838e9692018-11-19 15:29:11 -08005248 urecord += urec_size;
5249 }
5250
Yonghong Songba64e7d2018-11-24 23:20:44 -08005251 prog->aux->func_info = krecord;
5252 prog->aux->func_info_cnt = nfuncs;
Yonghong Song838e9692018-11-19 15:29:11 -08005253 return 0;
5254
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005255err_free:
Yonghong Songba64e7d2018-11-24 23:20:44 -08005256 kvfree(krecord);
Yonghong Song838e9692018-11-19 15:29:11 -08005257 return ret;
5258}
5259
Yonghong Songba64e7d2018-11-24 23:20:44 -08005260static void adjust_btf_func(struct bpf_verifier_env *env)
5261{
5262 int i;
5263
5264 if (!env->prog->aux->func_info)
5265 return;
5266
5267 for (i = 0; i < env->subprog_cnt; i++)
Martin KaFai Laud30d42e2018-12-05 17:35:44 -08005268 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
Yonghong Songba64e7d2018-11-24 23:20:44 -08005269}
5270
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005271#define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
5272 sizeof(((struct bpf_line_info *)(0))->line_col))
5273#define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
5274
5275static int check_btf_line(struct bpf_verifier_env *env,
5276 const union bpf_attr *attr,
5277 union bpf_attr __user *uattr)
5278{
5279 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
5280 struct bpf_subprog_info *sub;
5281 struct bpf_line_info *linfo;
5282 struct bpf_prog *prog;
5283 const struct btf *btf;
5284 void __user *ulinfo;
5285 int err;
5286
5287 nr_linfo = attr->line_info_cnt;
5288 if (!nr_linfo)
5289 return 0;
5290
5291 rec_size = attr->line_info_rec_size;
5292 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
5293 rec_size > MAX_LINEINFO_REC_SIZE ||
5294 rec_size & (sizeof(u32) - 1))
5295 return -EINVAL;
5296
5297 /* Need to zero it in case the userspace may
5298 * pass in a smaller bpf_line_info object.
5299 */
5300 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
5301 GFP_KERNEL | __GFP_NOWARN);
5302 if (!linfo)
5303 return -ENOMEM;
5304
5305 prog = env->prog;
5306 btf = prog->aux->btf;
5307
5308 s = 0;
5309 sub = env->subprog_info;
5310 ulinfo = u64_to_user_ptr(attr->line_info);
5311 expected_size = sizeof(struct bpf_line_info);
5312 ncopy = min_t(u32, expected_size, rec_size);
5313 for (i = 0; i < nr_linfo; i++) {
5314 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
5315 if (err) {
5316 if (err == -E2BIG) {
5317 verbose(env, "nonzero tailing record in line_info");
5318 if (put_user(expected_size,
5319 &uattr->line_info_rec_size))
5320 err = -EFAULT;
5321 }
5322 goto err_free;
5323 }
5324
5325 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
5326 err = -EFAULT;
5327 goto err_free;
5328 }
5329
5330 /*
5331 * Check insn_off to ensure
5332 * 1) strictly increasing AND
5333 * 2) bounded by prog->len
5334 *
5335 * The linfo[0].insn_off == 0 check logically falls into
5336 * the later "missing bpf_line_info for func..." case
5337 * because the first linfo[0].insn_off must be the
5338 * first sub also and the first sub must have
5339 * subprog_info[0].start == 0.
5340 */
5341 if ((i && linfo[i].insn_off <= prev_offset) ||
5342 linfo[i].insn_off >= prog->len) {
5343 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
5344 i, linfo[i].insn_off, prev_offset,
5345 prog->len);
5346 err = -EINVAL;
5347 goto err_free;
5348 }
5349
Martin KaFai Laufdbaa0b2018-12-19 13:01:01 -08005350 if (!prog->insnsi[linfo[i].insn_off].code) {
5351 verbose(env,
5352 "Invalid insn code at line_info[%u].insn_off\n",
5353 i);
5354 err = -EINVAL;
5355 goto err_free;
5356 }
5357
Martin KaFai Lau23127b32018-12-13 10:41:46 -08005358 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
5359 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08005360 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
5361 err = -EINVAL;
5362 goto err_free;
5363 }
5364
5365 if (s != env->subprog_cnt) {
5366 if (linfo[i].insn_off == sub[s].start) {
5367 sub[s].linfo_idx = i;
5368 s++;
5369 } else if (sub[s].start < linfo[i].insn_off) {
5370 verbose(env, "missing bpf_line_info for func#%u\n", s);
5371 err = -EINVAL;
5372 goto err_free;
5373 }
5374 }
5375
5376 prev_offset = linfo[i].insn_off;
5377 ulinfo += rec_size;
5378 }
5379
5380 if (s != env->subprog_cnt) {
5381 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
5382 env->subprog_cnt - s, s);
5383 err = -EINVAL;
5384 goto err_free;
5385 }
5386
5387 prog->aux->linfo = linfo;
5388 prog->aux->nr_linfo = nr_linfo;
5389
5390 return 0;
5391
5392err_free:
5393 kvfree(linfo);
5394 return err;
5395}
5396
5397static int check_btf_info(struct bpf_verifier_env *env,
5398 const union bpf_attr *attr,
5399 union bpf_attr __user *uattr)
5400{
5401 struct btf *btf;
5402 int err;
5403
5404 if (!attr->func_info_cnt && !attr->line_info_cnt)
5405 return 0;
5406
5407 btf = btf_get_by_fd(attr->prog_btf_fd);
5408 if (IS_ERR(btf))
5409 return PTR_ERR(btf);
5410 env->prog->aux->btf = btf;
5411
5412 err = check_btf_func(env, attr, uattr);
5413 if (err)
5414 return err;
5415
5416 err = check_btf_line(env, attr, uattr);
5417 if (err)
5418 return err;
5419
5420 return 0;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07005421}
5422
Edward Creef1174f72017-08-07 15:26:19 +01005423/* check %cur's range satisfies %old's */
5424static bool range_within(struct bpf_reg_state *old,
5425 struct bpf_reg_state *cur)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005426{
Edward Creeb03c9f92017-08-07 15:26:36 +01005427 return old->umin_value <= cur->umin_value &&
5428 old->umax_value >= cur->umax_value &&
5429 old->smin_value <= cur->smin_value &&
5430 old->smax_value >= cur->smax_value;
Edward Creef1174f72017-08-07 15:26:19 +01005431}
5432
5433/* Maximum number of register states that can exist at once */
5434#define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
5435struct idpair {
5436 u32 old;
5437 u32 cur;
5438};
5439
5440/* If in the old state two registers had the same id, then they need to have
5441 * the same id in the new state as well. But that id could be different from
5442 * the old state, so we need to track the mapping from old to new ids.
5443 * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
5444 * regs with old id 5 must also have new id 9 for the new state to be safe. But
5445 * regs with a different old id could still have new id 9, we don't care about
5446 * that.
5447 * So we look through our idmap to see if this old id has been seen before. If
5448 * so, we require the new id to match; otherwise, we add the id pair to the map.
5449 */
5450static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
5451{
5452 unsigned int i;
5453
5454 for (i = 0; i < ID_MAP_SIZE; i++) {
5455 if (!idmap[i].old) {
5456 /* Reached an empty slot; haven't seen this id before */
5457 idmap[i].old = old_id;
5458 idmap[i].cur = cur_id;
5459 return true;
5460 }
5461 if (idmap[i].old == old_id)
5462 return idmap[i].cur == cur_id;
5463 }
5464 /* We ran out of idmap slots, which should be impossible */
5465 WARN_ON_ONCE(1);
5466 return false;
5467}
5468
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08005469static void clean_func_state(struct bpf_verifier_env *env,
5470 struct bpf_func_state *st)
5471{
5472 enum bpf_reg_liveness live;
5473 int i, j;
5474
5475 for (i = 0; i < BPF_REG_FP; i++) {
5476 live = st->regs[i].live;
5477 /* liveness must not touch this register anymore */
5478 st->regs[i].live |= REG_LIVE_DONE;
5479 if (!(live & REG_LIVE_READ))
5480 /* since the register is unused, clear its state
5481 * to make further comparison simpler
5482 */
5483 __mark_reg_not_init(&st->regs[i]);
5484 }
5485
5486 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5487 live = st->stack[i].spilled_ptr.live;
5488 /* liveness must not touch this stack slot anymore */
5489 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5490 if (!(live & REG_LIVE_READ)) {
5491 __mark_reg_not_init(&st->stack[i].spilled_ptr);
5492 for (j = 0; j < BPF_REG_SIZE; j++)
5493 st->stack[i].slot_type[j] = STACK_INVALID;
5494 }
5495 }
5496}
5497
5498static void clean_verifier_state(struct bpf_verifier_env *env,
5499 struct bpf_verifier_state *st)
5500{
5501 int i;
5502
5503 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5504 /* all regs in this state in all frames were already marked */
5505 return;
5506
5507 for (i = 0; i <= st->curframe; i++)
5508 clean_func_state(env, st->frame[i]);
5509}
5510
5511/* the parentage chains form a tree.
5512 * the verifier states are added to state lists at given insn and
5513 * pushed into state stack for future exploration.
5514 * when the verifier reaches bpf_exit insn some of the verifer states
5515 * stored in the state lists have their final liveness state already,
5516 * but a lot of states will get revised from liveness point of view when
5517 * the verifier explores other branches.
5518 * Example:
5519 * 1: r0 = 1
5520 * 2: if r1 == 100 goto pc+1
5521 * 3: r0 = 2
5522 * 4: exit
5523 * when the verifier reaches exit insn the register r0 in the state list of
5524 * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5525 * of insn 2 and goes exploring further. At the insn 4 it will walk the
5526 * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5527 *
5528 * Since the verifier pushes the branch states as it sees them while exploring
5529 * the program the condition of walking the branch instruction for the second
5530 * time means that all states below this branch were already explored and
5531 * their final liveness markes are already propagated.
5532 * Hence when the verifier completes the search of state list in is_state_visited()
5533 * we can call this clean_live_states() function to mark all liveness states
5534 * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5535 * will not be used.
5536 * This function also clears the registers and stack for states that !READ
5537 * to simplify state merging.
5538 *
5539 * Important note here that walking the same branch instruction in the callee
5540 * doesn't meant that the states are DONE. The verifier has to compare
5541 * the callsites
5542 */
5543static void clean_live_states(struct bpf_verifier_env *env, int insn,
5544 struct bpf_verifier_state *cur)
5545{
5546 struct bpf_verifier_state_list *sl;
5547 int i;
5548
5549 sl = env->explored_states[insn];
5550 if (!sl)
5551 return;
5552
5553 while (sl != STATE_LIST_MARK) {
5554 if (sl->state.curframe != cur->curframe)
5555 goto next;
5556 for (i = 0; i <= cur->curframe; i++)
5557 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
5558 goto next;
5559 clean_verifier_state(env, &sl->state);
5560next:
5561 sl = sl->next;
5562 }
5563}
5564
Edward Creef1174f72017-08-07 15:26:19 +01005565/* Returns true if (rold safe implies rcur safe) */
Edward Cree1b688a12017-08-23 15:10:50 +01005566static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
5567 struct idpair *idmap)
Edward Creef1174f72017-08-07 15:26:19 +01005568{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005569 bool equal;
5570
Edward Creedc503a82017-08-15 20:34:35 +01005571 if (!(rold->live & REG_LIVE_READ))
5572 /* explored state didn't use this */
5573 return true;
5574
Edward Cree679c7822018-08-22 20:02:19 +01005575 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005576
5577 if (rold->type == PTR_TO_STACK)
5578 /* two stack pointers are equal only if they're pointing to
5579 * the same stack frame, since fp-8 in foo != fp-8 in bar
5580 */
5581 return equal && rold->frameno == rcur->frameno;
5582
5583 if (equal)
Edward Creef1174f72017-08-07 15:26:19 +01005584 return true;
5585
5586 if (rold->type == NOT_INIT)
5587 /* explored state can't have used this */
5588 return true;
5589 if (rcur->type == NOT_INIT)
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005590 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005591 switch (rold->type) {
5592 case SCALAR_VALUE:
5593 if (rcur->type == SCALAR_VALUE) {
5594 /* new val must satisfy old val knowledge */
5595 return range_within(rold, rcur) &&
5596 tnum_in(rold->var_off, rcur->var_off);
5597 } else {
Jann Horn179d1c52017-12-18 20:11:59 -08005598 /* We're trying to use a pointer in place of a scalar.
5599 * Even if the scalar was unbounded, this could lead to
5600 * pointer leaks because scalars are allowed to leak
5601 * while pointers are not. We could make this safe in
5602 * special cases if root is calling us, but it's
5603 * probably not worth the hassle.
Edward Creef1174f72017-08-07 15:26:19 +01005604 */
Jann Horn179d1c52017-12-18 20:11:59 -08005605 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005606 }
5607 case PTR_TO_MAP_VALUE:
Edward Cree1b688a12017-08-23 15:10:50 +01005608 /* If the new min/max/var_off satisfy the old ones and
5609 * everything else matches, we are OK.
5610 * We don't care about the 'id' value, because nothing
5611 * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL)
5612 */
5613 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
5614 range_within(rold, rcur) &&
5615 tnum_in(rold->var_off, rcur->var_off);
Edward Creef1174f72017-08-07 15:26:19 +01005616 case PTR_TO_MAP_VALUE_OR_NULL:
5617 /* a PTR_TO_MAP_VALUE could be safe to use as a
5618 * PTR_TO_MAP_VALUE_OR_NULL into the same map.
5619 * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
5620 * checked, doing so could have affected others with the same
5621 * id, and we can't check for that because we lost the id when
5622 * we converted to a PTR_TO_MAP_VALUE.
5623 */
5624 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
5625 return false;
5626 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
5627 return false;
5628 /* Check our ids match any regs they're supposed to */
5629 return check_ids(rold->id, rcur->id, idmap);
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005630 case PTR_TO_PACKET_META:
Edward Creef1174f72017-08-07 15:26:19 +01005631 case PTR_TO_PACKET:
Daniel Borkmannde8f3a82017-09-25 02:25:51 +02005632 if (rcur->type != rold->type)
Edward Creef1174f72017-08-07 15:26:19 +01005633 return false;
5634 /* We must have at least as much range as the old ptr
5635 * did, so that any accesses which were safe before are
5636 * still safe. This is true even if old range < old off,
5637 * since someone could have accessed through (ptr - k), or
5638 * even done ptr -= k in a register, to get a safe access.
5639 */
5640 if (rold->range > rcur->range)
5641 return false;
5642 /* If the offsets don't match, we can't trust our alignment;
5643 * nor can we be sure that we won't fall out of range.
5644 */
5645 if (rold->off != rcur->off)
5646 return false;
5647 /* id relations must be preserved */
5648 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
5649 return false;
5650 /* new val must satisfy old val knowledge */
5651 return range_within(rold, rcur) &&
5652 tnum_in(rold->var_off, rcur->var_off);
5653 case PTR_TO_CTX:
5654 case CONST_PTR_TO_MAP:
Edward Creef1174f72017-08-07 15:26:19 +01005655 case PTR_TO_PACKET_END:
Petar Penkovd58e4682018-09-14 07:46:18 -07005656 case PTR_TO_FLOW_KEYS:
Joe Stringerc64b7982018-10-02 13:35:33 -07005657 case PTR_TO_SOCKET:
5658 case PTR_TO_SOCKET_OR_NULL:
Edward Creef1174f72017-08-07 15:26:19 +01005659 /* Only valid matches are exact, which memcmp() above
5660 * would have accepted
5661 */
5662 default:
5663 /* Don't know what's going on, just say it's not safe */
5664 return false;
5665 }
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005666
Edward Creef1174f72017-08-07 15:26:19 +01005667 /* Shouldn't get here; if we do, say it's not safe */
5668 WARN_ON_ONCE(1);
Alexei Starovoitov969bf052016-05-05 19:49:10 -07005669 return false;
5670}
5671
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005672static bool stacksafe(struct bpf_func_state *old,
5673 struct bpf_func_state *cur,
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005674 struct idpair *idmap)
5675{
5676 int i, spi;
5677
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005678 /* walk slots of the explored stack and ignore any additional
5679 * slots in the current stack, since explored(safe) state
5680 * didn't use them
5681 */
5682 for (i = 0; i < old->allocated_stack; i++) {
5683 spi = i / BPF_REG_SIZE;
5684
Alexei Starovoitovb2339202018-12-13 11:42:31 -08005685 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
5686 i += BPF_REG_SIZE - 1;
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005687 /* explored state didn't use this */
Gianluca Borellofd05e572017-12-23 10:09:55 +00005688 continue;
Alexei Starovoitovb2339202018-12-13 11:42:31 -08005689 }
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005690
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005691 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
5692 continue;
Alexei Starovoitov19e2dbb2018-12-13 11:42:33 -08005693
5694 /* explored stack has more populated slots than current stack
5695 * and these slots were used
5696 */
5697 if (i >= cur->allocated_stack)
5698 return false;
5699
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005700 /* if old state was safe with misc data in the stack
5701 * it will be safe with zero-initialized stack.
5702 * The opposite is not true
5703 */
5704 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
5705 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
5706 continue;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005707 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
5708 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
5709 /* Ex: old explored (safe) state has STACK_SPILL in
5710 * this stack slot, but current has has STACK_MISC ->
5711 * this verifier states are not equivalent,
5712 * return false to continue verification of this path
5713 */
5714 return false;
5715 if (i % BPF_REG_SIZE)
5716 continue;
5717 if (old->stack[spi].slot_type[0] != STACK_SPILL)
5718 continue;
5719 if (!regsafe(&old->stack[spi].spilled_ptr,
5720 &cur->stack[spi].spilled_ptr,
5721 idmap))
5722 /* when explored and current stack slot are both storing
5723 * spilled registers, check that stored pointers types
5724 * are the same as well.
5725 * Ex: explored safe path could have stored
5726 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
5727 * but current path has stored:
5728 * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
5729 * such verifier states are not equivalent.
5730 * return false to continue verification of this path
5731 */
5732 return false;
5733 }
5734 return true;
5735}
5736
Joe Stringerfd978bf72018-10-02 13:35:35 -07005737static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
5738{
5739 if (old->acquired_refs != cur->acquired_refs)
5740 return false;
5741 return !memcmp(old->refs, cur->refs,
5742 sizeof(*old->refs) * old->acquired_refs);
5743}
5744
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005745/* compare two verifier states
5746 *
5747 * all states stored in state_list are known to be valid, since
5748 * verifier reached 'bpf_exit' instruction through them
5749 *
5750 * this function is called when verifier exploring different branches of
5751 * execution popped from the state stack. If it sees an old state that has
5752 * more strict register state and more strict stack state then this execution
5753 * branch doesn't need to be explored further, since verifier already
5754 * concluded that more strict state leads to valid finish.
5755 *
5756 * Therefore two states are equivalent if register state is more conservative
5757 * and explored stack state is more conservative than the current one.
5758 * Example:
5759 * explored current
5760 * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
5761 * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
5762 *
5763 * In other words if current stack state (one being explored) has more
5764 * valid slots than old one that already passed validation, it means
5765 * the verifier can stop exploring and conclude that current state is valid too
5766 *
5767 * Similarly with registers. If explored state has register type as invalid
5768 * whereas register type in current state is meaningful, it means that
5769 * the current state will reach 'bpf_exit' instruction safely
5770 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005771static bool func_states_equal(struct bpf_func_state *old,
5772 struct bpf_func_state *cur)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005773{
Edward Creef1174f72017-08-07 15:26:19 +01005774 struct idpair *idmap;
5775 bool ret = false;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005776 int i;
5777
Edward Creef1174f72017-08-07 15:26:19 +01005778 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
5779 /* If we failed to allocate the idmap, just say it's not safe */
5780 if (!idmap)
Alexei Starovoitov1a0dc1a2016-05-05 19:49:09 -07005781 return false;
Edward Creef1174f72017-08-07 15:26:19 +01005782
5783 for (i = 0; i < MAX_BPF_REG; i++) {
Edward Cree1b688a12017-08-23 15:10:50 +01005784 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
Edward Creef1174f72017-08-07 15:26:19 +01005785 goto out_free;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005786 }
5787
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005788 if (!stacksafe(old, cur, idmap))
5789 goto out_free;
Joe Stringerfd978bf72018-10-02 13:35:35 -07005790
5791 if (!refsafe(old, cur))
5792 goto out_free;
Edward Creef1174f72017-08-07 15:26:19 +01005793 ret = true;
5794out_free:
5795 kfree(idmap);
5796 return ret;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005797}
5798
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005799static bool states_equal(struct bpf_verifier_env *env,
5800 struct bpf_verifier_state *old,
5801 struct bpf_verifier_state *cur)
Edward Creedc503a82017-08-15 20:34:35 +01005802{
Edward Creedc503a82017-08-15 20:34:35 +01005803 int i;
5804
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005805 if (old->curframe != cur->curframe)
5806 return false;
5807
Daniel Borkmann979d63d2019-01-03 00:58:34 +01005808 /* Verification state from speculative execution simulation
5809 * must never prune a non-speculative execution one.
5810 */
5811 if (old->speculative && !cur->speculative)
5812 return false;
5813
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005814 /* for states to be equal callsites have to be the same
5815 * and all frame states need to be equivalent
5816 */
5817 for (i = 0; i <= old->curframe; i++) {
5818 if (old->frame[i]->callsite != cur->frame[i]->callsite)
5819 return false;
5820 if (!func_states_equal(old->frame[i], cur->frame[i]))
5821 return false;
5822 }
5823 return true;
5824}
5825
5826/* A write screens off any subsequent reads; but write marks come from the
5827 * straight-line code between a state and its parent. When we arrive at an
5828 * equivalent state (jump target or such) we didn't arrive by the straight-line
5829 * code, so read marks in the state must propagate to the parent regardless
5830 * of the state's write marks. That's what 'parent == state->parent' comparison
Edward Cree679c7822018-08-22 20:02:19 +01005831 * in mark_reg_read() is for.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005832 */
5833static int propagate_liveness(struct bpf_verifier_env *env,
5834 const struct bpf_verifier_state *vstate,
5835 struct bpf_verifier_state *vparent)
5836{
5837 int i, frame, err = 0;
5838 struct bpf_func_state *state, *parent;
5839
5840 if (vparent->curframe != vstate->curframe) {
5841 WARN(1, "propagate_live: parent frame %d current frame %d\n",
5842 vparent->curframe, vstate->curframe);
5843 return -EFAULT;
5844 }
Edward Creedc503a82017-08-15 20:34:35 +01005845 /* Propagate read liveness of registers... */
5846 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
5847 /* We don't need to worry about FP liveness because it's read-only */
5848 for (i = 0; i < BPF_REG_FP; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005849 if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ)
Edward Creedc503a82017-08-15 20:34:35 +01005850 continue;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005851 if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) {
Edward Cree679c7822018-08-22 20:02:19 +01005852 err = mark_reg_read(env, &vstate->frame[vstate->curframe]->regs[i],
5853 &vparent->frame[vstate->curframe]->regs[i]);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005854 if (err)
5855 return err;
Edward Creedc503a82017-08-15 20:34:35 +01005856 }
5857 }
Edward Creedc503a82017-08-15 20:34:35 +01005858
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005859 /* ... and stack slots */
5860 for (frame = 0; frame <= vstate->curframe; frame++) {
5861 state = vstate->frame[frame];
5862 parent = vparent->frame[frame];
5863 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
5864 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005865 if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
5866 continue;
5867 if (state->stack[i].spilled_ptr.live & REG_LIVE_READ)
Edward Cree679c7822018-08-22 20:02:19 +01005868 mark_reg_read(env, &state->stack[i].spilled_ptr,
5869 &parent->stack[i].spilled_ptr);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005870 }
Edward Creedc503a82017-08-15 20:34:35 +01005871 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005872 return err;
Edward Creedc503a82017-08-15 20:34:35 +01005873}
5874
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005875static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005876{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01005877 struct bpf_verifier_state_list *new_sl;
5878 struct bpf_verifier_state_list *sl;
Edward Cree679c7822018-08-22 20:02:19 +01005879 struct bpf_verifier_state *cur = env->cur_state, *new;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08005880 int i, j, err, states_cnt = 0;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005881
5882 sl = env->explored_states[insn_idx];
5883 if (!sl)
5884 /* this 'insn_idx' instruction wasn't marked, so we will not
5885 * be doing state search here
5886 */
5887 return 0;
5888
Alexei Starovoitov9242b5f2018-12-13 11:42:34 -08005889 clean_live_states(env, insn_idx, cur);
5890
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005891 while (sl != STATE_LIST_MARK) {
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005892 if (states_equal(env, &sl->state, cur)) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005893 /* reached equivalent register/stack state,
Edward Creedc503a82017-08-15 20:34:35 +01005894 * prune the search.
5895 * Registers read by the continuation are read by us.
Edward Cree8e9cd9c2017-08-23 15:11:21 +01005896 * If we have any write marks in env->cur_state, they
5897 * will prevent corresponding reads in the continuation
5898 * from reaching our parent (an explored_state). Our
5899 * own state will get the read marks recorded, but
5900 * they'll be immediately forgotten as we're pruning
5901 * this state and will pop a new one.
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005902 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005903 err = propagate_liveness(env, &sl->state, cur);
5904 if (err)
5905 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005906 return 1;
Edward Creedc503a82017-08-15 20:34:35 +01005907 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005908 sl = sl->next;
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08005909 states_cnt++;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005910 }
5911
Alexei Starovoitovceefbc92018-12-03 22:46:06 -08005912 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
5913 return 0;
5914
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005915 /* there were no equivalent states, remember current one.
5916 * technically the current state is not proven to be safe yet,
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005917 * but it will either reach outer most bpf_exit (which means it's safe)
5918 * or it will be rejected. Since there are no loops, we won't be
5919 * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
5920 * again on the way to bpf_exit
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005921 */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07005922 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005923 if (!new_sl)
5924 return -ENOMEM;
5925
5926 /* add new state to the head of linked list */
Edward Cree679c7822018-08-22 20:02:19 +01005927 new = &new_sl->state;
5928 err = copy_verifier_state(new, cur);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005929 if (err) {
Edward Cree679c7822018-08-22 20:02:19 +01005930 free_verifier_state(new, false);
Alexei Starovoitov1969db42017-11-01 00:08:04 -07005931 kfree(new_sl);
5932 return err;
5933 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005934 new_sl->next = env->explored_states[insn_idx];
5935 env->explored_states[insn_idx] = new_sl;
Jakub Kicinski7640ead2018-12-12 16:29:07 -08005936 /* connect new state to parentage chain. Current frame needs all
5937 * registers connected. Only r6 - r9 of the callers are alive (pushed
5938 * to the stack implicitly by JITs) so in callers' frames connect just
5939 * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
5940 * the state of the call instruction (with WRITTEN set), and r0 comes
5941 * from callee with its full parentage chain, anyway.
5942 */
5943 for (j = 0; j <= cur->curframe; j++)
5944 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
5945 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
Edward Cree8e9cd9c2017-08-23 15:11:21 +01005946 /* clear write marks in current state: the writes we did are not writes
5947 * our child did, so they don't screen off its reads from us.
5948 * (There are no read marks in current state, because reads always mark
5949 * their parent and current state never has children yet. Only
5950 * explored_states can get read marks.)
5951 */
Edward Creedc503a82017-08-15 20:34:35 +01005952 for (i = 0; i < BPF_REG_FP; i++)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005953 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
5954
5955 /* all stack frames are accessible from callee, clear them all */
5956 for (j = 0; j <= cur->curframe; j++) {
5957 struct bpf_func_state *frame = cur->frame[j];
Edward Cree679c7822018-08-22 20:02:19 +01005958 struct bpf_func_state *newframe = new->frame[j];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005959
Edward Cree679c7822018-08-22 20:02:19 +01005960 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -08005961 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
Edward Cree679c7822018-08-22 20:02:19 +01005962 frame->stack[i].spilled_ptr.parent =
5963 &newframe->stack[i].spilled_ptr;
5964 }
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08005965 }
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07005966 return 0;
5967}
5968
Joe Stringerc64b7982018-10-02 13:35:33 -07005969/* Return true if it's OK to have the same insn return a different type. */
5970static bool reg_type_mismatch_ok(enum bpf_reg_type type)
5971{
5972 switch (type) {
5973 case PTR_TO_CTX:
5974 case PTR_TO_SOCKET:
5975 case PTR_TO_SOCKET_OR_NULL:
5976 return false;
5977 default:
5978 return true;
5979 }
5980}
5981
5982/* If an instruction was previously used with particular pointer types, then we
5983 * need to be careful to avoid cases such as the below, where it may be ok
5984 * for one branch accessing the pointer, but not ok for the other branch:
5985 *
5986 * R1 = sock_ptr
5987 * goto X;
5988 * ...
5989 * R1 = some_other_valid_ptr;
5990 * goto X;
5991 * ...
5992 * R2 = *(u32 *)(R1 + 0);
5993 */
5994static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
5995{
5996 return src != prev && (!reg_type_mismatch_ok(src) ||
5997 !reg_type_mismatch_ok(prev));
5998}
5999
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006000static int do_check(struct bpf_verifier_env *env)
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006001{
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006002 struct bpf_verifier_state *state;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006003 struct bpf_insn *insns = env->prog->insnsi;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006004 struct bpf_reg_state *regs;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006005 int insn_cnt = env->prog->len, i;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006006 int insn_processed = 0;
6007 bool do_print_state = false;
6008
Martin KaFai Laud9762e82018-12-13 10:41:48 -08006009 env->prev_linfo = NULL;
6010
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006011 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6012 if (!state)
6013 return -ENOMEM;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006014 state->curframe = 0;
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006015 state->speculative = false;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006016 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6017 if (!state->frame[0]) {
6018 kfree(state);
6019 return -ENOMEM;
6020 }
6021 env->cur_state = state;
6022 init_func_state(env, state->frame[0],
6023 BPF_MAIN_FUNC /* callsite */,
6024 0 /* frameno */,
6025 0 /* subprogno, zero == main subprog */);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006026
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006027 for (;;) {
6028 struct bpf_insn *insn;
6029 u8 class;
6030 int err;
6031
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006032 if (env->insn_idx >= insn_cnt) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006033 verbose(env, "invalid insn idx %d insn_cnt %d\n",
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006034 env->insn_idx, insn_cnt);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006035 return -EFAULT;
6036 }
6037
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006038 insn = &insns[env->insn_idx];
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006039 class = BPF_CLASS(insn->code);
6040
Daniel Borkmann07016152016-04-05 22:33:17 +02006041 if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006042 verbose(env,
6043 "BPF program is too large. Processed %d insn\n",
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006044 insn_processed);
6045 return -E2BIG;
6046 }
6047
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006048 err = is_state_visited(env, env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006049 if (err < 0)
6050 return err;
6051 if (err == 1) {
6052 /* found equivalent state, can prune the search */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006053 if (env->log.level) {
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006054 if (do_print_state)
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006055 verbose(env, "\nfrom %d to %d%s: safe\n",
6056 env->prev_insn_idx, env->insn_idx,
6057 env->cur_state->speculative ?
6058 " (speculative execution)" : "");
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006059 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006060 verbose(env, "%d: safe\n", env->insn_idx);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006061 }
6062 goto process_bpf_exit;
6063 }
6064
Alexei Starovoitovc3494802018-12-03 22:46:04 -08006065 if (signal_pending(current))
6066 return -EAGAIN;
6067
Daniel Borkmann3c2ce602017-05-18 03:00:06 +02006068 if (need_resched())
6069 cond_resched();
6070
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006071 if (env->log.level > 1 || (env->log.level && do_print_state)) {
6072 if (env->log.level > 1)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006073 verbose(env, "%d:", env->insn_idx);
David S. Millerc5fc9692017-05-10 11:25:17 -07006074 else
Daniel Borkmann979d63d2019-01-03 00:58:34 +01006075 verbose(env, "\nfrom %d to %d%s:",
6076 env->prev_insn_idx, env->insn_idx,
6077 env->cur_state->speculative ?
6078 " (speculative execution)" : "");
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006079 print_verifier_state(env, state->frame[state->curframe]);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006080 do_print_state = false;
6081 }
6082
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006083 if (env->log.level) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01006084 const struct bpf_insn_cbs cbs = {
6085 .cb_print = verbose,
Jiri Olsaabe08842018-03-23 11:41:28 +01006086 .private_data = env,
Daniel Borkmann7105e822017-12-20 13:42:57 +01006087 };
6088
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006089 verbose_linfo(env, env->insn_idx, "; ");
6090 verbose(env, "%d: ", env->insn_idx);
Jiri Olsaabe08842018-03-23 11:41:28 +01006091 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006092 }
6093
Jakub Kicinskicae19272017-12-27 18:39:05 -08006094 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006095 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6096 env->prev_insn_idx);
Jakub Kicinskicae19272017-12-27 18:39:05 -08006097 if (err)
6098 return err;
6099 }
Jakub Kicinski13a27df2016-09-21 11:43:58 +01006100
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006101 regs = cur_regs(env);
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006102 env->insn_aux_data[env->insn_idx].seen = true;
Joe Stringerfd978bf72018-10-02 13:35:35 -07006103
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006104 if (class == BPF_ALU || class == BPF_ALU64) {
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006105 err = check_alu_op(env, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006106 if (err)
6107 return err;
6108
6109 } else if (class == BPF_LDX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006110 enum bpf_reg_type *prev_src_type, src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006111
6112 /* check for reserved fields is already done */
6113
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006114 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006115 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006116 if (err)
6117 return err;
6118
Edward Creedc503a82017-08-15 20:34:35 +01006119 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006120 if (err)
6121 return err;
6122
Alexei Starovoitov725f9dc2015-04-15 16:19:33 -07006123 src_reg_type = regs[insn->src_reg].type;
6124
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006125 /* check that memory (src_reg + off) is readable,
6126 * the state of dst_reg will be updated by this func
6127 */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006128 err = check_mem_access(env, env->insn_idx, insn->src_reg,
6129 insn->off, BPF_SIZE(insn->code),
6130 BPF_READ, insn->dst_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006131 if (err)
6132 return err;
6133
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006134 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006135
6136 if (*prev_src_type == NOT_INIT) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006137 /* saw a valid insn
6138 * dst_reg = *(u32 *)(src_reg + off)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006139 * save type to validate intersecting paths
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006140 */
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006141 *prev_src_type = src_reg_type;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006142
Joe Stringerc64b7982018-10-02 13:35:33 -07006143 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006144 /* ABuser program is trying to use the same insn
6145 * dst_reg = *(u32*) (src_reg + off)
6146 * with different pointer types:
6147 * src_reg == ctx in one branch and
6148 * src_reg == stack|map in some other branch.
6149 * Reject it.
6150 */
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006151 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006152 return -EINVAL;
6153 }
6154
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006155 } else if (class == BPF_STX) {
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006156 enum bpf_reg_type *prev_dst_type, dst_reg_type;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006157
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006158 if (BPF_MODE(insn->code) == BPF_XADD) {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006159 err = check_xadd(env, env->insn_idx, insn);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006160 if (err)
6161 return err;
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006162 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006163 continue;
6164 }
6165
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006166 /* check src1 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006167 err = check_reg_arg(env, insn->src_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006168 if (err)
6169 return err;
6170 /* check src2 operand */
Edward Creedc503a82017-08-15 20:34:35 +01006171 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006172 if (err)
6173 return err;
6174
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006175 dst_reg_type = regs[insn->dst_reg].type;
6176
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006177 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006178 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6179 insn->off, BPF_SIZE(insn->code),
6180 BPF_WRITE, insn->src_reg, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006181 if (err)
6182 return err;
6183
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006184 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006185
6186 if (*prev_dst_type == NOT_INIT) {
6187 *prev_dst_type = dst_reg_type;
Joe Stringerc64b7982018-10-02 13:35:33 -07006188 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006189 verbose(env, "same insn cannot be used with different pointers\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006190 return -EINVAL;
6191 }
6192
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006193 } else if (class == BPF_ST) {
6194 if (BPF_MODE(insn->code) != BPF_MEM ||
6195 insn->src_reg != BPF_REG_0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006196 verbose(env, "BPF_ST uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006197 return -EINVAL;
6198 }
6199 /* check src operand */
Edward Creedc503a82017-08-15 20:34:35 +01006200 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006201 if (err)
6202 return err;
6203
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006204 if (is_ctx_reg(env, insn->dst_reg)) {
Joe Stringer9d2be442018-10-02 13:35:31 -07006205 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
Daniel Borkmann2a159c62018-10-21 02:09:24 +02006206 insn->dst_reg,
6207 reg_type_str[reg_state(env, insn->dst_reg)->type]);
Daniel Borkmannf37a8cb2018-01-16 23:30:10 +01006208 return -EACCES;
6209 }
6210
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006211 /* check that memory (dst_reg + off) is writeable */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006212 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6213 insn->off, BPF_SIZE(insn->code),
6214 BPF_WRITE, -1, false);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006215 if (err)
6216 return err;
6217
Jiong Wang092ed092019-01-26 12:26:01 -05006218 } else if (class == BPF_JMP || class == BPF_JMP32) {
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006219 u8 opcode = BPF_OP(insn->code);
6220
6221 if (opcode == BPF_CALL) {
6222 if (BPF_SRC(insn->code) != BPF_K ||
6223 insn->off != 0 ||
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006224 (insn->src_reg != BPF_REG_0 &&
6225 insn->src_reg != BPF_PSEUDO_CALL) ||
Jiong Wang092ed092019-01-26 12:26:01 -05006226 insn->dst_reg != BPF_REG_0 ||
6227 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006228 verbose(env, "BPF_CALL uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006229 return -EINVAL;
6230 }
6231
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006232 if (insn->src_reg == BPF_PSEUDO_CALL)
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006233 err = check_func_call(env, insn, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006234 else
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006235 err = check_helper_call(env, insn->imm, env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006236 if (err)
6237 return err;
6238
6239 } else if (opcode == BPF_JA) {
6240 if (BPF_SRC(insn->code) != BPF_K ||
6241 insn->imm != 0 ||
6242 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006243 insn->dst_reg != BPF_REG_0 ||
6244 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006245 verbose(env, "BPF_JA uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006246 return -EINVAL;
6247 }
6248
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006249 env->insn_idx += insn->off + 1;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006250 continue;
6251
6252 } else if (opcode == BPF_EXIT) {
6253 if (BPF_SRC(insn->code) != BPF_K ||
6254 insn->imm != 0 ||
6255 insn->src_reg != BPF_REG_0 ||
Jiong Wang092ed092019-01-26 12:26:01 -05006256 insn->dst_reg != BPF_REG_0 ||
6257 class == BPF_JMP32) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006258 verbose(env, "BPF_EXIT uses reserved fields\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006259 return -EINVAL;
6260 }
6261
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006262 if (state->curframe) {
6263 /* exit from nested function */
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006264 env->prev_insn_idx = env->insn_idx;
6265 err = prepare_func_exit(env, &env->insn_idx);
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006266 if (err)
6267 return err;
6268 do_print_state = true;
6269 continue;
6270 }
6271
Joe Stringerfd978bf72018-10-02 13:35:35 -07006272 err = check_reference_leak(env);
6273 if (err)
6274 return err;
6275
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006276 /* eBPF calling convetion is such that R0 is used
6277 * to return the value from eBPF program.
6278 * Make sure that it's readable at this time
6279 * of bpf_exit, which means that program wrote
6280 * something into it earlier
6281 */
Edward Creedc503a82017-08-15 20:34:35 +01006282 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006283 if (err)
6284 return err;
6285
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006286 if (is_pointer_value(env, BPF_REG_0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006287 verbose(env, "R0 leaks addr as return value\n");
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07006288 return -EACCES;
6289 }
6290
Alexei Starovoitov390ee7e2017-10-02 22:50:23 -07006291 err = check_return_code(env);
6292 if (err)
6293 return err;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07006294process_bpf_exit:
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006295 err = pop_stack(env, &env->prev_insn_idx,
6296 &env->insn_idx);
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07006297 if (err < 0) {
6298 if (err != -ENOENT)
6299 return err;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006300 break;
6301 } else {
6302 do_print_state = true;
6303 continue;
6304 }
6305 } else {
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006306 err = check_cond_jmp_op(env, insn, &env->insn_idx);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006307 if (err)
6308 return err;
6309 }
6310 } else if (class == BPF_LD) {
6311 u8 mode = BPF_MODE(insn->code);
6312
6313 if (mode == BPF_ABS || mode == BPF_IND) {
Alexei Starovoitovddd872b2014-12-01 15:06:34 -08006314 err = check_ld_abs(env, insn);
6315 if (err)
6316 return err;
6317
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006318 } else if (mode == BPF_IMM) {
6319 err = check_ld_imm(env, insn);
6320 if (err)
6321 return err;
6322
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006323 env->insn_idx++;
6324 env->insn_aux_data[env->insn_idx].seen = true;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006325 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006326 verbose(env, "invalid BPF_LD mode\n");
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006327 return -EINVAL;
6328 }
6329 } else {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006330 verbose(env, "unknown insn class %d\n", class);
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006331 return -EINVAL;
6332 }
6333
Daniel Borkmannc08435e2019-01-03 00:58:27 +01006334 env->insn_idx++;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006335 }
6336
Daniel Borkmann4bd95f42018-01-20 01:24:36 +01006337 verbose(env, "processed %d insns (limit %d), stack depth ",
6338 insn_processed, BPF_COMPLEXITY_LIMIT_INSNS);
Jiong Wangf910cef2018-05-02 16:17:17 -04006339 for (i = 0; i < env->subprog_cnt; i++) {
Jiong Wang9c8105b2018-05-02 16:17:18 -04006340 u32 depth = env->subprog_info[i].stack_depth;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006341
6342 verbose(env, "%d", depth);
Jiong Wangf910cef2018-05-02 16:17:17 -04006343 if (i + 1 < env->subprog_cnt)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -08006344 verbose(env, "+");
6345 }
6346 verbose(env, "\n");
Jiong Wang9c8105b2018-05-02 16:17:18 -04006347 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
Alexei Starovoitov17a52672014-09-26 00:17:06 -07006348 return 0;
6349}
6350
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006351static int check_map_prealloc(struct bpf_map *map)
6352{
6353 return (map->map_type != BPF_MAP_TYPE_HASH &&
Martin KaFai Laubcc6b1b2017-03-22 10:00:34 -07006354 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
6355 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006356 !(map->map_flags & BPF_F_NO_PREALLOC);
6357}
6358
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006359static int check_map_prog_compatibility(struct bpf_verifier_env *env,
6360 struct bpf_map *map,
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006361 struct bpf_prog *prog)
6362
6363{
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006364 /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
6365 * preallocated hash maps, since doing memory allocation
6366 * in overflow_handler can crash depending on where nmi got
6367 * triggered.
6368 */
6369 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
6370 if (!check_map_prealloc(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006371 verbose(env, "perf_event programs can only use preallocated hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006372 return -EINVAL;
6373 }
6374 if (map->inner_map_meta &&
6375 !check_map_prealloc(map->inner_map_meta)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006376 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
Martin KaFai Lau56f668d2017-03-22 10:00:33 -07006377 return -EINVAL;
6378 }
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006379 }
Jakub Kicinskia3884572018-01-11 20:29:09 -08006380
6381 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
Jakub Kicinski09728262018-07-17 10:53:23 -07006382 !bpf_offload_prog_map_match(prog, map)) {
Jakub Kicinskia3884572018-01-11 20:29:09 -08006383 verbose(env, "offload device mismatch between prog and map\n");
6384 return -EINVAL;
6385 }
6386
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006387 return 0;
6388}
6389
Roman Gushchinb741f162018-09-28 14:45:43 +00006390static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
6391{
6392 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
6393 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
6394}
6395
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006396/* look for pseudo eBPF instructions that access map FDs and
6397 * replace them with actual map pointers
6398 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006399static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006400{
6401 struct bpf_insn *insn = env->prog->insnsi;
6402 int insn_cnt = env->prog->len;
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006403 int i, j, err;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006404
Daniel Borkmannf1f77142017-01-13 23:38:15 +01006405 err = bpf_prog_calc_tag(env->prog);
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01006406 if (err)
6407 return err;
6408
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006409 for (i = 0; i < insn_cnt; i++, insn++) {
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006410 if (BPF_CLASS(insn->code) == BPF_LDX &&
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006411 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006412 verbose(env, "BPF_LDX uses reserved fields\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006413 return -EINVAL;
6414 }
6415
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006416 if (BPF_CLASS(insn->code) == BPF_STX &&
6417 ((BPF_MODE(insn->code) != BPF_MEM &&
6418 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006419 verbose(env, "BPF_STX uses reserved fields\n");
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006420 return -EINVAL;
6421 }
6422
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006423 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
6424 struct bpf_map *map;
6425 struct fd f;
6426
6427 if (i == insn_cnt - 1 || insn[1].code != 0 ||
6428 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
6429 insn[1].off != 0) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006430 verbose(env, "invalid bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006431 return -EINVAL;
6432 }
6433
6434 if (insn->src_reg == 0)
6435 /* valid generic load 64-bit imm */
6436 goto next_insn;
6437
6438 if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006439 verbose(env,
6440 "unrecognized bpf_ld_imm64 insn\n");
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006441 return -EINVAL;
6442 }
6443
6444 f = fdget(insn->imm);
Daniel Borkmannc2101292015-10-29 14:58:07 +01006445 map = __bpf_map_get(f);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006446 if (IS_ERR(map)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006447 verbose(env, "fd %d is not pointing to valid bpf_map\n",
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006448 insn->imm);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006449 return PTR_ERR(map);
6450 }
6451
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006452 err = check_map_prog_compatibility(env, map, env->prog);
Alexei Starovoitovfdc15d32016-09-01 18:37:23 -07006453 if (err) {
6454 fdput(f);
6455 return err;
6456 }
6457
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006458 /* store map pointer inside BPF_LD_IMM64 instruction */
6459 insn[0].imm = (u32) (unsigned long) map;
6460 insn[1].imm = ((u64) (unsigned long) map) >> 32;
6461
6462 /* check whether we recorded this map already */
6463 for (j = 0; j < env->used_map_cnt; j++)
6464 if (env->used_maps[j] == map) {
6465 fdput(f);
6466 goto next_insn;
6467 }
6468
6469 if (env->used_map_cnt >= MAX_USED_MAPS) {
6470 fdput(f);
6471 return -E2BIG;
6472 }
6473
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006474 /* hold the map. If the program is rejected by verifier,
6475 * the map will be released by release_maps() or it
6476 * will be used by the valid program until it's unloaded
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07006477 * and all maps are released in free_used_maps()
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006478 */
Alexei Starovoitov92117d82016-04-27 18:56:20 -07006479 map = bpf_map_inc(map, false);
6480 if (IS_ERR(map)) {
6481 fdput(f);
6482 return PTR_ERR(map);
6483 }
6484 env->used_maps[env->used_map_cnt++] = map;
6485
Roman Gushchinb741f162018-09-28 14:45:43 +00006486 if (bpf_map_is_cgroup_storage(map) &&
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006487 bpf_cgroup_storage_assign(env->prog, map)) {
Roman Gushchinb741f162018-09-28 14:45:43 +00006488 verbose(env, "only one cgroup storage of each type is allowed\n");
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006489 fdput(f);
6490 return -EBUSY;
6491 }
6492
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006493 fdput(f);
6494next_insn:
6495 insn++;
6496 i++;
Daniel Borkmann5e581da2018-01-26 23:33:38 +01006497 continue;
6498 }
6499
6500 /* Basic sanity check before we invest more work here. */
6501 if (!bpf_opcode_in_insntable(insn->code)) {
6502 verbose(env, "unknown opcode %02x\n", insn->code);
6503 return -EINVAL;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006504 }
6505 }
6506
6507 /* now all pseudo BPF_LD_IMM64 instructions load valid
6508 * 'struct bpf_map *' into a register instead of user map_fd.
6509 * These pointers will be used later by verifier to validate map access.
6510 */
6511 return 0;
6512}
6513
6514/* drop refcnt of maps used by the rejected program */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006515static void release_maps(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006516{
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006517 enum bpf_cgroup_storage_type stype;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006518 int i;
6519
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006520 for_each_cgroup_storage_type(stype) {
6521 if (!env->prog->aux->cgroup_storage[stype])
6522 continue;
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006523 bpf_cgroup_storage_release(env->prog,
Roman Gushchin8bad74f2018-09-28 14:45:36 +00006524 env->prog->aux->cgroup_storage[stype]);
6525 }
Roman Gushchinde9cbba2018-08-02 14:27:18 -07006526
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006527 for (i = 0; i < env->used_map_cnt; i++)
6528 bpf_map_put(env->used_maps[i]);
6529}
6530
6531/* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006532static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07006533{
6534 struct bpf_insn *insn = env->prog->insnsi;
6535 int insn_cnt = env->prog->len;
6536 int i;
6537
6538 for (i = 0; i < insn_cnt; i++, insn++)
6539 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
6540 insn->src_reg = 0;
6541}
6542
Alexei Starovoitov80419022017-03-15 18:26:41 -07006543/* single env->prog->insni[off] instruction was replaced with the range
6544 * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying
6545 * [0, off) and [off, end) to new locations, so the patched range stays zero
6546 */
6547static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
6548 u32 off, u32 cnt)
6549{
6550 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006551 int i;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006552
6553 if (cnt == 1)
6554 return 0;
Kees Cookfad953c2018-06-12 14:27:37 -07006555 new_data = vzalloc(array_size(prog_len,
6556 sizeof(struct bpf_insn_aux_data)));
Alexei Starovoitov80419022017-03-15 18:26:41 -07006557 if (!new_data)
6558 return -ENOMEM;
6559 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
6560 memcpy(new_data + off + cnt - 1, old_data + off,
6561 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006562 for (i = off; i < off + cnt - 1; i++)
6563 new_data[i].seen = true;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006564 env->insn_aux_data = new_data;
6565 vfree(old_data);
6566 return 0;
6567}
6568
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006569static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
6570{
6571 int i;
6572
6573 if (len == 1)
6574 return;
Jiong Wang4cb3d992018-05-02 16:17:19 -04006575 /* NOTE: fake 'exit' subprog should be updated as well. */
6576 for (i = 0; i <= env->subprog_cnt; i++) {
Edward Creeafd59422018-11-16 12:00:07 +00006577 if (env->subprog_info[i].start <= off)
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006578 continue;
Jiong Wang9c8105b2018-05-02 16:17:18 -04006579 env->subprog_info[i].start += len - 1;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006580 }
6581}
6582
Alexei Starovoitov80419022017-03-15 18:26:41 -07006583static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
6584 const struct bpf_insn *patch, u32 len)
6585{
6586 struct bpf_prog *new_prog;
6587
6588 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
6589 if (!new_prog)
6590 return NULL;
6591 if (adjust_insn_aux_data(env, new_prog->len, off, len))
6592 return NULL;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08006593 adjust_subprog_starts(env, off, len);
Alexei Starovoitov80419022017-03-15 18:26:41 -07006594 return new_prog;
6595}
6596
Jakub Kicinski52875a02019-01-22 22:45:20 -08006597static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
6598 u32 off, u32 cnt)
6599{
6600 int i, j;
6601
6602 /* find first prog starting at or after off (first to remove) */
6603 for (i = 0; i < env->subprog_cnt; i++)
6604 if (env->subprog_info[i].start >= off)
6605 break;
6606 /* find first prog starting at or after off + cnt (first to stay) */
6607 for (j = i; j < env->subprog_cnt; j++)
6608 if (env->subprog_info[j].start >= off + cnt)
6609 break;
6610 /* if j doesn't start exactly at off + cnt, we are just removing
6611 * the front of previous prog
6612 */
6613 if (env->subprog_info[j].start != off + cnt)
6614 j--;
6615
6616 if (j > i) {
6617 struct bpf_prog_aux *aux = env->prog->aux;
6618 int move;
6619
6620 /* move fake 'exit' subprog as well */
6621 move = env->subprog_cnt + 1 - j;
6622
6623 memmove(env->subprog_info + i,
6624 env->subprog_info + j,
6625 sizeof(*env->subprog_info) * move);
6626 env->subprog_cnt -= j - i;
6627
6628 /* remove func_info */
6629 if (aux->func_info) {
6630 move = aux->func_info_cnt - j;
6631
6632 memmove(aux->func_info + i,
6633 aux->func_info + j,
6634 sizeof(*aux->func_info) * move);
6635 aux->func_info_cnt -= j - i;
6636 /* func_info->insn_off is set after all code rewrites,
6637 * in adjust_btf_func() - no need to adjust
6638 */
6639 }
6640 } else {
6641 /* convert i from "first prog to remove" to "first to adjust" */
6642 if (env->subprog_info[i].start == off)
6643 i++;
6644 }
6645
6646 /* update fake 'exit' subprog as well */
6647 for (; i <= env->subprog_cnt; i++)
6648 env->subprog_info[i].start -= cnt;
6649
6650 return 0;
6651}
6652
6653static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
6654 u32 cnt)
6655{
6656 struct bpf_prog *prog = env->prog;
6657 u32 i, l_off, l_cnt, nr_linfo;
6658 struct bpf_line_info *linfo;
6659
6660 nr_linfo = prog->aux->nr_linfo;
6661 if (!nr_linfo)
6662 return 0;
6663
6664 linfo = prog->aux->linfo;
6665
6666 /* find first line info to remove, count lines to be removed */
6667 for (i = 0; i < nr_linfo; i++)
6668 if (linfo[i].insn_off >= off)
6669 break;
6670
6671 l_off = i;
6672 l_cnt = 0;
6673 for (; i < nr_linfo; i++)
6674 if (linfo[i].insn_off < off + cnt)
6675 l_cnt++;
6676 else
6677 break;
6678
6679 /* First live insn doesn't match first live linfo, it needs to "inherit"
6680 * last removed linfo. prog is already modified, so prog->len == off
6681 * means no live instructions after (tail of the program was removed).
6682 */
6683 if (prog->len != off && l_cnt &&
6684 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
6685 l_cnt--;
6686 linfo[--i].insn_off = off + cnt;
6687 }
6688
6689 /* remove the line info which refer to the removed instructions */
6690 if (l_cnt) {
6691 memmove(linfo + l_off, linfo + i,
6692 sizeof(*linfo) * (nr_linfo - i));
6693
6694 prog->aux->nr_linfo -= l_cnt;
6695 nr_linfo = prog->aux->nr_linfo;
6696 }
6697
6698 /* pull all linfo[i].insn_off >= off + cnt in by cnt */
6699 for (i = l_off; i < nr_linfo; i++)
6700 linfo[i].insn_off -= cnt;
6701
6702 /* fix up all subprogs (incl. 'exit') which start >= off */
6703 for (i = 0; i <= env->subprog_cnt; i++)
6704 if (env->subprog_info[i].linfo_idx > l_off) {
6705 /* program may have started in the removed region but
6706 * may not be fully removed
6707 */
6708 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
6709 env->subprog_info[i].linfo_idx -= l_cnt;
6710 else
6711 env->subprog_info[i].linfo_idx = l_off;
6712 }
6713
6714 return 0;
6715}
6716
6717static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
6718{
6719 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
6720 unsigned int orig_prog_len = env->prog->len;
6721 int err;
6722
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08006723 if (bpf_prog_is_dev_bound(env->prog->aux))
6724 bpf_prog_offload_remove_insns(env, off, cnt);
6725
Jakub Kicinski52875a02019-01-22 22:45:20 -08006726 err = bpf_remove_insns(env->prog, off, cnt);
6727 if (err)
6728 return err;
6729
6730 err = adjust_subprog_starts_after_remove(env, off, cnt);
6731 if (err)
6732 return err;
6733
6734 err = bpf_adj_linfo_after_remove(env, off, cnt);
6735 if (err)
6736 return err;
6737
6738 memmove(aux_data + off, aux_data + off + cnt,
6739 sizeof(*aux_data) * (orig_prog_len - off - cnt));
6740
6741 return 0;
6742}
6743
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01006744/* The verifier does more data flow analysis than llvm and will not
6745 * explore branches that are dead at run time. Malicious programs can
6746 * have dead code too. Therefore replace all dead at-run-time code
6747 * with 'ja -1'.
6748 *
6749 * Just nops are not optimal, e.g. if they would sit at the end of the
6750 * program and through another bug we would manage to jump there, then
6751 * we'd execute beyond program memory otherwise. Returning exception
6752 * code also wouldn't work since we can have subprogs where the dead
6753 * code could be located.
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006754 */
6755static void sanitize_dead_code(struct bpf_verifier_env *env)
6756{
6757 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01006758 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006759 struct bpf_insn *insn = env->prog->insnsi;
6760 const int insn_cnt = env->prog->len;
6761 int i;
6762
6763 for (i = 0; i < insn_cnt; i++) {
6764 if (aux_data[i].seen)
6765 continue;
Daniel Borkmann2a5418a2018-01-26 23:33:37 +01006766 memcpy(insn + i, &trap, sizeof(trap));
Alexei Starovoitovc1311872017-11-22 16:42:05 -08006767 }
6768}
6769
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08006770static bool insn_is_cond_jump(u8 code)
6771{
6772 u8 op;
6773
Jiong Wang092ed092019-01-26 12:26:01 -05006774 if (BPF_CLASS(code) == BPF_JMP32)
6775 return true;
6776
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08006777 if (BPF_CLASS(code) != BPF_JMP)
6778 return false;
6779
6780 op = BPF_OP(code);
6781 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
6782}
6783
6784static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
6785{
6786 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
6787 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
6788 struct bpf_insn *insn = env->prog->insnsi;
6789 const int insn_cnt = env->prog->len;
6790 int i;
6791
6792 for (i = 0; i < insn_cnt; i++, insn++) {
6793 if (!insn_is_cond_jump(insn->code))
6794 continue;
6795
6796 if (!aux_data[i + 1].seen)
6797 ja.off = insn->off;
6798 else if (!aux_data[i + 1 + insn->off].seen)
6799 ja.off = 0;
6800 else
6801 continue;
6802
Jakub Kicinski08ca90a2019-01-22 22:45:24 -08006803 if (bpf_prog_is_dev_bound(env->prog->aux))
6804 bpf_prog_offload_replace_insn(env, i, &ja);
6805
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08006806 memcpy(insn, &ja, sizeof(ja));
6807 }
6808}
6809
Jakub Kicinski52875a02019-01-22 22:45:20 -08006810static int opt_remove_dead_code(struct bpf_verifier_env *env)
6811{
6812 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
6813 int insn_cnt = env->prog->len;
6814 int i, err;
6815
6816 for (i = 0; i < insn_cnt; i++) {
6817 int j;
6818
6819 j = 0;
6820 while (i + j < insn_cnt && !aux_data[i + j].seen)
6821 j++;
6822 if (!j)
6823 continue;
6824
6825 err = verifier_remove_insns(env, i, j);
6826 if (err)
6827 return err;
6828 insn_cnt = env->prog->len;
6829 }
6830
6831 return 0;
6832}
6833
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08006834static int opt_remove_nops(struct bpf_verifier_env *env)
6835{
6836 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
6837 struct bpf_insn *insn = env->prog->insnsi;
6838 int insn_cnt = env->prog->len;
6839 int i, err;
6840
6841 for (i = 0; i < insn_cnt; i++) {
6842 if (memcmp(&insn[i], &ja, sizeof(ja)))
6843 continue;
6844
6845 err = verifier_remove_insns(env, i, 1);
6846 if (err)
6847 return err;
6848 insn_cnt--;
6849 i--;
6850 }
6851
6852 return 0;
6853}
6854
Joe Stringerc64b7982018-10-02 13:35:33 -07006855/* convert load instructions that access fields of a context type into a
6856 * sequence of instructions that access fields of the underlying structure:
6857 * struct __sk_buff -> struct sk_buff
6858 * struct bpf_sock_ops -> struct sock
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006859 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01006860static int convert_ctx_accesses(struct bpf_verifier_env *env)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006861{
Jakub Kicinski00176a32017-10-16 16:40:54 -07006862 const struct bpf_verifier_ops *ops = env->ops;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006863 int i, cnt, size, ctx_field_size, delta = 0;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006864 const int insn_cnt = env->prog->len;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006865 struct bpf_insn insn_buf[16], *insn;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006866 u32 target_size, size_default, off;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006867 struct bpf_prog *new_prog;
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006868 enum bpf_access_type type;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006869 bool is_narrower_load;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006870
Daniel Borkmannb09928b2018-10-24 22:05:49 +02006871 if (ops->gen_prologue || env->seen_direct_write) {
6872 if (!ops->gen_prologue) {
6873 verbose(env, "bpf verifier is misconfigured\n");
6874 return -EINVAL;
6875 }
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006876 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
6877 env->prog);
6878 if (cnt >= ARRAY_SIZE(insn_buf)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006879 verbose(env, "bpf verifier is misconfigured\n");
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006880 return -EINVAL;
6881 } else if (cnt) {
Alexei Starovoitov80419022017-03-15 18:26:41 -07006882 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006883 if (!new_prog)
6884 return -ENOMEM;
Alexei Starovoitov80419022017-03-15 18:26:41 -07006885
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006886 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006887 delta += cnt - 1;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006888 }
6889 }
6890
Joe Stringerc64b7982018-10-02 13:35:33 -07006891 if (bpf_prog_is_dev_bound(env->prog->aux))
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006892 return 0;
6893
Jakub Kicinski3df126f2016-09-21 11:43:56 +01006894 insn = env->prog->insnsi + delta;
Daniel Borkmann36bbef52016-09-20 00:26:13 +02006895
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006896 for (i = 0; i < insn_cnt; i++, insn++) {
Joe Stringerc64b7982018-10-02 13:35:33 -07006897 bpf_convert_ctx_access_t convert_ctx_access;
6898
Daniel Borkmann62c79892017-01-12 11:51:33 +01006899 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
6900 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
6901 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07006902 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006903 type = BPF_READ;
Daniel Borkmann62c79892017-01-12 11:51:33 +01006904 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
6905 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
6906 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
Alexei Starovoitovea2e7ce2016-09-01 18:37:21 -07006907 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
Alexei Starovoitovd691f9e2015-06-04 10:11:54 -07006908 type = BPF_WRITE;
6909 else
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006910 continue;
6911
Alexei Starovoitovaf86ca42018-05-15 09:27:05 -07006912 if (type == BPF_WRITE &&
6913 env->insn_aux_data[i + delta].sanitize_stack_off) {
6914 struct bpf_insn patch[] = {
6915 /* Sanitize suspicious stack slot with zero.
6916 * There are no memory dependencies for this store,
6917 * since it's only using frame pointer and immediate
6918 * constant of zero
6919 */
6920 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
6921 env->insn_aux_data[i + delta].sanitize_stack_off,
6922 0),
6923 /* the original STX instruction will immediately
6924 * overwrite the same stack slot with appropriate value
6925 */
6926 *insn,
6927 };
6928
6929 cnt = ARRAY_SIZE(patch);
6930 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
6931 if (!new_prog)
6932 return -ENOMEM;
6933
6934 delta += cnt - 1;
6935 env->prog = new_prog;
6936 insn = new_prog->insnsi + i + delta;
6937 continue;
6938 }
6939
Joe Stringerc64b7982018-10-02 13:35:33 -07006940 switch (env->insn_aux_data[i + delta].ptr_type) {
6941 case PTR_TO_CTX:
6942 if (!ops->convert_ctx_access)
6943 continue;
6944 convert_ctx_access = ops->convert_ctx_access;
6945 break;
6946 case PTR_TO_SOCKET:
6947 convert_ctx_access = bpf_sock_convert_ctx_access;
6948 break;
6949 default:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006950 continue;
Joe Stringerc64b7982018-10-02 13:35:33 -07006951 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006952
Yonghong Song31fd8582017-06-13 15:52:13 -07006953 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006954 size = BPF_LDST_BYTES(insn);
Yonghong Song31fd8582017-06-13 15:52:13 -07006955
6956 /* If the read access is a narrower load of the field,
6957 * convert to a 4/8-byte load, to minimum program type specific
6958 * convert_ctx_access changes. If conversion is successful,
6959 * we will apply proper mask to the result.
6960 */
Daniel Borkmannf96da092017-07-02 02:13:27 +02006961 is_narrower_load = size < ctx_field_size;
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006962 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
6963 off = insn->off;
Yonghong Song31fd8582017-06-13 15:52:13 -07006964 if (is_narrower_load) {
Daniel Borkmannf96da092017-07-02 02:13:27 +02006965 u8 size_code;
Yonghong Song31fd8582017-06-13 15:52:13 -07006966
Daniel Borkmannf96da092017-07-02 02:13:27 +02006967 if (type == BPF_WRITE) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006968 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
Daniel Borkmannf96da092017-07-02 02:13:27 +02006969 return -EINVAL;
6970 }
6971
6972 size_code = BPF_H;
Yonghong Song31fd8582017-06-13 15:52:13 -07006973 if (ctx_field_size == 4)
6974 size_code = BPF_W;
6975 else if (ctx_field_size == 8)
6976 size_code = BPF_DW;
Daniel Borkmannf96da092017-07-02 02:13:27 +02006977
Daniel Borkmannbc231052018-06-02 23:06:39 +02006978 insn->off = off & ~(size_default - 1);
Yonghong Song31fd8582017-06-13 15:52:13 -07006979 insn->code = BPF_LDX | BPF_MEM | size_code;
6980 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02006981
6982 target_size = 0;
Joe Stringerc64b7982018-10-02 13:35:33 -07006983 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
6984 &target_size);
Daniel Borkmannf96da092017-07-02 02:13:27 +02006985 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
6986 (ctx_field_size && !target_size)) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07006987 verbose(env, "bpf verifier is misconfigured\n");
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07006988 return -EINVAL;
6989 }
Daniel Borkmannf96da092017-07-02 02:13:27 +02006990
6991 if (is_narrower_load && size < target_size) {
Andrey Ignatov46f53a62018-11-10 22:15:13 -08006992 u8 shift = (off & (size_default - 1)) * 8;
6993
6994 if (ctx_field_size <= 4) {
6995 if (shift)
6996 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
6997 insn->dst_reg,
6998 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07006999 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007000 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007001 } else {
7002 if (shift)
7003 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
7004 insn->dst_reg,
7005 shift);
Yonghong Song31fd8582017-06-13 15:52:13 -07007006 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
Daniel Borkmannf96da092017-07-02 02:13:27 +02007007 (1 << size * 8) - 1);
Andrey Ignatov46f53a62018-11-10 22:15:13 -08007008 }
Yonghong Song31fd8582017-06-13 15:52:13 -07007009 }
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007010
Alexei Starovoitov80419022017-03-15 18:26:41 -07007011 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007012 if (!new_prog)
7013 return -ENOMEM;
7014
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007015 delta += cnt - 1;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007016
7017 /* keep walking new program and skip insns we just inserted */
7018 env->prog = new_prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007019 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007020 }
7021
7022 return 0;
7023}
7024
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007025static int jit_subprogs(struct bpf_verifier_env *env)
7026{
7027 struct bpf_prog *prog = env->prog, **func, *tmp;
7028 int i, j, subprog_start, subprog_end = 0, len, subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007029 struct bpf_insn *insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007030 void *old_bpf_func;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007031 int err;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007032
Jiong Wangf910cef2018-05-02 16:17:17 -04007033 if (env->subprog_cnt <= 1)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007034 return 0;
7035
Daniel Borkmann7105e822017-12-20 13:42:57 +01007036 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007037 if (insn->code != (BPF_JMP | BPF_CALL) ||
7038 insn->src_reg != BPF_PSEUDO_CALL)
7039 continue;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007040 /* Upon error here we cannot fall back to interpreter but
7041 * need a hard reject of the program. Thus -EFAULT is
7042 * propagated in any case.
7043 */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007044 subprog = find_subprog(env, i + insn->imm + 1);
7045 if (subprog < 0) {
7046 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
7047 i + insn->imm + 1);
7048 return -EFAULT;
7049 }
7050 /* temporarily remember subprog id inside insn instead of
7051 * aux_data, since next loop will split up all insns into funcs
7052 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007053 insn->off = subprog;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007054 /* remember original imm in case JIT fails and fallback
7055 * to interpreter will be needed
7056 */
7057 env->insn_aux_data[i].call_imm = insn->imm;
7058 /* point imm to __bpf_call_base+1 from JITs point of view */
7059 insn->imm = 1;
7060 }
7061
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007062 err = bpf_prog_alloc_jited_linfo(prog);
7063 if (err)
7064 goto out_undo_insn;
7065
7066 err = -ENOMEM;
Kees Cook6396bb22018-06-12 14:03:40 -07007067 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007068 if (!func)
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007069 goto out_undo_insn;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007070
Jiong Wangf910cef2018-05-02 16:17:17 -04007071 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007072 subprog_start = subprog_end;
Jiong Wang4cb3d992018-05-02 16:17:19 -04007073 subprog_end = env->subprog_info[i + 1].start;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007074
7075 len = subprog_end - subprog_start;
7076 func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER);
7077 if (!func[i])
7078 goto out_free;
7079 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
7080 len * sizeof(struct bpf_insn));
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007081 func[i]->type = prog->type;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007082 func[i]->len = len;
Daniel Borkmann4f74d802017-12-20 13:42:56 +01007083 if (bpf_prog_calc_tag(func[i]))
7084 goto out_free;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007085 func[i]->is_func = 1;
Yonghong Songba64e7d2018-11-24 23:20:44 -08007086 func[i]->aux->func_idx = i;
7087 /* the btf and func_info will be freed only at prog->aux */
7088 func[i]->aux->btf = prog->aux->btf;
7089 func[i]->aux->func_info = prog->aux->func_info;
7090
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007091 /* Use bpf_prog_F_tag to indicate functions in stack traces.
7092 * Long term would need debug info to populate names
7093 */
7094 func[i]->aux->name[0] = 'F';
Jiong Wang9c8105b2018-05-02 16:17:18 -04007095 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007096 func[i]->jit_requested = 1;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007097 func[i]->aux->linfo = prog->aux->linfo;
7098 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
7099 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
7100 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007101 func[i] = bpf_int_jit_compile(func[i]);
7102 if (!func[i]->jited) {
7103 err = -ENOTSUPP;
7104 goto out_free;
7105 }
7106 cond_resched();
7107 }
7108 /* at this point all bpf functions were successfully JITed
7109 * now populate all bpf_calls with correct addresses and
7110 * run last pass of JIT
7111 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007112 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007113 insn = func[i]->insnsi;
7114 for (j = 0; j < func[i]->len; j++, insn++) {
7115 if (insn->code != (BPF_JMP | BPF_CALL) ||
7116 insn->src_reg != BPF_PSEUDO_CALL)
7117 continue;
7118 subprog = insn->off;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007119 insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
7120 func[subprog]->bpf_func -
7121 __bpf_call_base;
7122 }
Sandipan Das2162fed2018-05-24 12:26:45 +05307123
7124 /* we use the aux data to keep a list of the start addresses
7125 * of the JITed images for each function in the program
7126 *
7127 * for some architectures, such as powerpc64, the imm field
7128 * might not be large enough to hold the offset of the start
7129 * address of the callee's JITed image from __bpf_call_base
7130 *
7131 * in such cases, we can lookup the start address of a callee
7132 * by using its subprog id, available from the off field of
7133 * the call instruction, as an index for this list
7134 */
7135 func[i]->aux->func = func;
7136 func[i]->aux->func_cnt = env->subprog_cnt;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007137 }
Jiong Wangf910cef2018-05-02 16:17:17 -04007138 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007139 old_bpf_func = func[i]->bpf_func;
7140 tmp = bpf_int_jit_compile(func[i]);
7141 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
7142 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007143 err = -ENOTSUPP;
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007144 goto out_free;
7145 }
7146 cond_resched();
7147 }
7148
7149 /* finally lock prog and jit images for all functions and
7150 * populate kallsysm
7151 */
Jiong Wangf910cef2018-05-02 16:17:17 -04007152 for (i = 0; i < env->subprog_cnt; i++) {
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007153 bpf_prog_lock_ro(func[i]);
7154 bpf_prog_kallsyms_add(func[i]);
7155 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01007156
7157 /* Last step: make now unused interpreter insns from main
7158 * prog consistent for later dump requests, so they can
7159 * later look the same as if they were interpreted only.
7160 */
7161 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01007162 if (insn->code != (BPF_JMP | BPF_CALL) ||
7163 insn->src_reg != BPF_PSEUDO_CALL)
7164 continue;
7165 insn->off = env->insn_aux_data[i].call_imm;
7166 subprog = find_subprog(env, i + insn->off + 1);
Sandipan Dasdbecd732018-05-24 12:26:48 +05307167 insn->imm = subprog;
Daniel Borkmann7105e822017-12-20 13:42:57 +01007168 }
7169
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007170 prog->jited = 1;
7171 prog->bpf_func = func[0]->bpf_func;
7172 prog->aux->func = func;
Jiong Wangf910cef2018-05-02 16:17:17 -04007173 prog->aux->func_cnt = env->subprog_cnt;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007174 bpf_prog_free_unused_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007175 return 0;
7176out_free:
Jiong Wangf910cef2018-05-02 16:17:17 -04007177 for (i = 0; i < env->subprog_cnt; i++)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007178 if (func[i])
7179 bpf_jit_free(func[i]);
7180 kfree(func);
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007181out_undo_insn:
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007182 /* cleanup main prog to be interpreted */
7183 prog->jit_requested = 0;
7184 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7185 if (insn->code != (BPF_JMP | BPF_CALL) ||
7186 insn->src_reg != BPF_PSEUDO_CALL)
7187 continue;
7188 insn->off = 0;
7189 insn->imm = env->insn_aux_data[i].call_imm;
7190 }
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007191 bpf_prog_free_jited_linfo(prog);
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007192 return err;
7193}
7194
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007195static int fixup_call_args(struct bpf_verifier_env *env)
7196{
David S. Miller19d28fb2018-01-11 21:27:54 -05007197#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007198 struct bpf_prog *prog = env->prog;
7199 struct bpf_insn *insn = prog->insnsi;
7200 int i, depth;
David S. Miller19d28fb2018-01-11 21:27:54 -05007201#endif
Quentin Monnete4052d02018-10-07 12:56:58 +01007202 int err = 0;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007203
Quentin Monnete4052d02018-10-07 12:56:58 +01007204 if (env->prog->jit_requested &&
7205 !bpf_prog_is_dev_bound(env->prog->aux)) {
David S. Miller19d28fb2018-01-11 21:27:54 -05007206 err = jit_subprogs(env);
7207 if (err == 0)
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -08007208 return 0;
Daniel Borkmannc7a89782018-07-12 21:44:28 +02007209 if (err == -EFAULT)
7210 return err;
David S. Miller19d28fb2018-01-11 21:27:54 -05007211 }
7212#ifndef CONFIG_BPF_JIT_ALWAYS_ON
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007213 for (i = 0; i < prog->len; i++, insn++) {
7214 if (insn->code != (BPF_JMP | BPF_CALL) ||
7215 insn->src_reg != BPF_PSEUDO_CALL)
7216 continue;
7217 depth = get_callee_stack_depth(env, insn, i);
7218 if (depth < 0)
7219 return depth;
7220 bpf_patch_call_args(insn, depth);
7221 }
David S. Miller19d28fb2018-01-11 21:27:54 -05007222 err = 0;
7223#endif
7224 return err;
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007225}
7226
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007227/* fixup insn->imm field of bpf_call instructions
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007228 * and inline eligible helpers as explicit sequence of BPF instructions
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007229 *
7230 * this function is called after eBPF program passed verification
7231 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007232static int fixup_bpf_calls(struct bpf_verifier_env *env)
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007233{
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007234 struct bpf_prog *prog = env->prog;
7235 struct bpf_insn *insn = prog->insnsi;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007236 const struct bpf_func_proto *fn;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007237 const int insn_cnt = prog->len;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007238 const struct bpf_map_ops *ops;
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007239 struct bpf_insn_aux_data *aux;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007240 struct bpf_insn insn_buf[16];
7241 struct bpf_prog *new_prog;
7242 struct bpf_map *map_ptr;
7243 int i, cnt, delta = 0;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007244
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007245 for (i = 0; i < insn_cnt; i++, insn++) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007246 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
7247 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7248 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007249 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
Daniel Borkmannf6b1b3b2018-01-26 23:33:39 +01007250 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
7251 struct bpf_insn mask_and_div[] = {
7252 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7253 /* Rx div 0 -> 0 */
7254 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
7255 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
7256 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
7257 *insn,
7258 };
7259 struct bpf_insn mask_and_mod[] = {
7260 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7261 /* Rx mod 0 -> Rx */
7262 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
7263 *insn,
7264 };
7265 struct bpf_insn *patchlet;
7266
7267 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7268 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7269 patchlet = mask_and_div + (is64 ? 1 : 0);
7270 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
7271 } else {
7272 patchlet = mask_and_mod + (is64 ? 1 : 0);
7273 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
7274 }
7275
7276 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
Alexei Starovoitov68fda452018-01-12 18:59:52 -08007277 if (!new_prog)
7278 return -ENOMEM;
7279
7280 delta += cnt - 1;
7281 env->prog = prog = new_prog;
7282 insn = new_prog->insnsi + i + delta;
7283 continue;
7284 }
7285
Daniel Borkmanne0cea7c2018-05-04 01:08:14 +02007286 if (BPF_CLASS(insn->code) == BPF_LD &&
7287 (BPF_MODE(insn->code) == BPF_ABS ||
7288 BPF_MODE(insn->code) == BPF_IND)) {
7289 cnt = env->ops->gen_ld_abs(insn, insn_buf);
7290 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7291 verbose(env, "bpf verifier is misconfigured\n");
7292 return -EINVAL;
7293 }
7294
7295 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7296 if (!new_prog)
7297 return -ENOMEM;
7298
7299 delta += cnt - 1;
7300 env->prog = prog = new_prog;
7301 insn = new_prog->insnsi + i + delta;
7302 continue;
7303 }
7304
Daniel Borkmann979d63d2019-01-03 00:58:34 +01007305 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
7306 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
7307 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
7308 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
7309 struct bpf_insn insn_buf[16];
7310 struct bpf_insn *patch = &insn_buf[0];
7311 bool issrc, isneg;
7312 u32 off_reg;
7313
7314 aux = &env->insn_aux_data[i + delta];
7315 if (!aux->alu_state)
7316 continue;
7317
7318 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
7319 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
7320 BPF_ALU_SANITIZE_SRC;
7321
7322 off_reg = issrc ? insn->src_reg : insn->dst_reg;
7323 if (isneg)
7324 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7325 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
7326 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
7327 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
7328 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
7329 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
7330 if (issrc) {
7331 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
7332 off_reg);
7333 insn->src_reg = BPF_REG_AX;
7334 } else {
7335 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
7336 BPF_REG_AX);
7337 }
7338 if (isneg)
7339 insn->code = insn->code == code_add ?
7340 code_sub : code_add;
7341 *patch++ = *insn;
7342 if (issrc && isneg)
7343 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7344 cnt = patch - insn_buf;
7345
7346 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7347 if (!new_prog)
7348 return -ENOMEM;
7349
7350 delta += cnt - 1;
7351 env->prog = prog = new_prog;
7352 insn = new_prog->insnsi + i + delta;
7353 continue;
7354 }
7355
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007356 if (insn->code != (BPF_JMP | BPF_CALL))
7357 continue;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -08007358 if (insn->src_reg == BPF_PSEUDO_CALL)
7359 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007360
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007361 if (insn->imm == BPF_FUNC_get_route_realm)
7362 prog->dst_needed = 1;
7363 if (insn->imm == BPF_FUNC_get_prandom_u32)
7364 bpf_user_rnd_init_once();
Josef Bacik9802d862017-12-11 11:36:48 -05007365 if (insn->imm == BPF_FUNC_override_return)
7366 prog->kprobe_override = 1;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007367 if (insn->imm == BPF_FUNC_tail_call) {
David S. Miller7b9f6da2017-04-20 10:35:33 -04007368 /* If we tail call into other programs, we
7369 * cannot make any assumptions since they can
7370 * be replaced dynamically during runtime in
7371 * the program array.
7372 */
7373 prog->cb_access = 1;
Alexei Starovoitov80a58d02017-05-30 13:31:30 -07007374 env->prog->aux->stack_depth = MAX_BPF_STACK;
Jiong Wange6478152018-11-08 04:08:42 -05007375 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
David S. Miller7b9f6da2017-04-20 10:35:33 -04007376
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007377 /* mark bpf_tail_call as different opcode to avoid
7378 * conditional branch in the interpeter for every normal
7379 * call and to prevent accidental JITing by JIT compiler
7380 * that doesn't support bpf_tail_call yet
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007381 */
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007382 insn->imm = 0;
Alexei Starovoitov71189fa2017-05-30 13:31:27 -07007383 insn->code = BPF_JMP | BPF_TAIL_CALL;
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007384
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007385 aux = &env->insn_aux_data[i + delta];
7386 if (!bpf_map_ptr_unpriv(aux))
7387 continue;
7388
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007389 /* instead of changing every JIT dealing with tail_call
7390 * emit two extra insns:
7391 * if (index >= max_entries) goto out;
7392 * index &= array->index_mask;
7393 * to avoid out-of-bounds cpu speculation
7394 */
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007395 if (bpf_map_ptr_poisoned(aux)) {
Colin Ian King40950342018-01-10 09:20:54 +00007396 verbose(env, "tail_call abusing map_ptr\n");
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007397 return -EINVAL;
7398 }
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007399
7400 map_ptr = BPF_MAP_PTR(aux->map_state);
Alexei Starovoitovb2157392018-01-07 17:33:02 -08007401 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
7402 map_ptr->max_entries, 2);
7403 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
7404 container_of(map_ptr,
7405 struct bpf_array,
7406 map)->index_mask);
7407 insn_buf[2] = *insn;
7408 cnt = 3;
7409 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7410 if (!new_prog)
7411 return -ENOMEM;
7412
7413 delta += cnt - 1;
7414 env->prog = prog = new_prog;
7415 insn = new_prog->insnsi + i + delta;
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007416 continue;
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007417 }
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007418
Daniel Borkmann89c63072017-08-19 03:12:45 +02007419 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
Daniel Borkmann09772d92018-06-02 23:06:35 +02007420 * and other inlining handlers are currently limited to 64 bit
7421 * only.
Daniel Borkmann89c63072017-08-19 03:12:45 +02007422 */
Alexei Starovoitov60b58afc2017-12-14 17:55:14 -08007423 if (prog->jit_requested && BITS_PER_LONG == 64 &&
Daniel Borkmann09772d92018-06-02 23:06:35 +02007424 (insn->imm == BPF_FUNC_map_lookup_elem ||
7425 insn->imm == BPF_FUNC_map_update_elem ||
Daniel Borkmann84430d42018-10-21 02:09:27 +02007426 insn->imm == BPF_FUNC_map_delete_elem ||
7427 insn->imm == BPF_FUNC_map_push_elem ||
7428 insn->imm == BPF_FUNC_map_pop_elem ||
7429 insn->imm == BPF_FUNC_map_peek_elem)) {
Daniel Borkmannc93552c2018-05-24 02:32:53 +02007430 aux = &env->insn_aux_data[i + delta];
7431 if (bpf_map_ptr_poisoned(aux))
7432 goto patch_call_imm;
7433
7434 map_ptr = BPF_MAP_PTR(aux->map_state);
Daniel Borkmann09772d92018-06-02 23:06:35 +02007435 ops = map_ptr->ops;
7436 if (insn->imm == BPF_FUNC_map_lookup_elem &&
7437 ops->map_gen_lookup) {
7438 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
7439 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7440 verbose(env, "bpf verifier is misconfigured\n");
7441 return -EINVAL;
7442 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007443
Daniel Borkmann09772d92018-06-02 23:06:35 +02007444 new_prog = bpf_patch_insn_data(env, i + delta,
7445 insn_buf, cnt);
7446 if (!new_prog)
7447 return -ENOMEM;
7448
7449 delta += cnt - 1;
7450 env->prog = prog = new_prog;
7451 insn = new_prog->insnsi + i + delta;
7452 continue;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007453 }
7454
Daniel Borkmann09772d92018-06-02 23:06:35 +02007455 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
7456 (void *(*)(struct bpf_map *map, void *key))NULL));
7457 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
7458 (int (*)(struct bpf_map *map, void *key))NULL));
7459 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
7460 (int (*)(struct bpf_map *map, void *key, void *value,
7461 u64 flags))NULL));
Daniel Borkmann84430d42018-10-21 02:09:27 +02007462 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
7463 (int (*)(struct bpf_map *map, void *value,
7464 u64 flags))NULL));
7465 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
7466 (int (*)(struct bpf_map *map, void *value))NULL));
7467 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
7468 (int (*)(struct bpf_map *map, void *value))NULL));
7469
Daniel Borkmann09772d92018-06-02 23:06:35 +02007470 switch (insn->imm) {
7471 case BPF_FUNC_map_lookup_elem:
7472 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
7473 __bpf_call_base;
7474 continue;
7475 case BPF_FUNC_map_update_elem:
7476 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
7477 __bpf_call_base;
7478 continue;
7479 case BPF_FUNC_map_delete_elem:
7480 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
7481 __bpf_call_base;
7482 continue;
Daniel Borkmann84430d42018-10-21 02:09:27 +02007483 case BPF_FUNC_map_push_elem:
7484 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
7485 __bpf_call_base;
7486 continue;
7487 case BPF_FUNC_map_pop_elem:
7488 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
7489 __bpf_call_base;
7490 continue;
7491 case BPF_FUNC_map_peek_elem:
7492 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
7493 __bpf_call_base;
7494 continue;
Daniel Borkmann09772d92018-06-02 23:06:35 +02007495 }
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007496
Daniel Borkmann09772d92018-06-02 23:06:35 +02007497 goto patch_call_imm;
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -07007498 }
7499
7500patch_call_imm:
Andrey Ignatov5e43f892018-03-30 15:08:00 -07007501 fn = env->ops->get_func_proto(insn->imm, env->prog);
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007502 /* all functions that have prototype and verifier allowed
7503 * programs to call them, must be real in-kernel functions
7504 */
7505 if (!fn->func) {
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007506 verbose(env,
7507 "kernel subsystem misconfigured func %s#%d\n",
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007508 func_id_name(insn->imm), insn->imm);
7509 return -EFAULT;
7510 }
7511 insn->imm = fn->func - __bpf_call_base;
7512 }
7513
7514 return 0;
7515}
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007516
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007517static void free_states(struct bpf_verifier_env *env)
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007518{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007519 struct bpf_verifier_state_list *sl, *sln;
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007520 int i;
7521
7522 if (!env->explored_states)
7523 return;
7524
7525 for (i = 0; i < env->prog->len; i++) {
7526 sl = env->explored_states[i];
7527
7528 if (sl)
7529 while (sl != STATE_LIST_MARK) {
7530 sln = sl->next;
Alexei Starovoitov1969db42017-11-01 00:08:04 -07007531 free_verifier_state(&sl->state, false);
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007532 kfree(sl);
7533 sl = sln;
7534 }
7535 }
7536
7537 kfree(env->explored_states);
7538}
7539
Yonghong Song838e9692018-11-19 15:29:11 -08007540int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
7541 union bpf_attr __user *uattr)
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007542{
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007543 struct bpf_verifier_env *env;
Martin KaFai Laub9193c12018-03-24 11:44:22 -07007544 struct bpf_verifier_log *log;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007545 int i, len, ret = -EINVAL;
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007546 bool is_priv;
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007547
Arnd Bergmanneba0c922017-11-02 12:05:52 +01007548 /* no program is valid */
7549 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
7550 return -EINVAL;
7551
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007552 /* 'struct bpf_verifier_env' can be global, but since it's not small,
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007553 * allocate/free it every time bpf_check() is called
7554 */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007555 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007556 if (!env)
7557 return -ENOMEM;
Jakub Kicinski61bd5212017-10-09 10:30:11 -07007558 log = &env->log;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007559
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007560 len = (*prog)->len;
Kees Cookfad953c2018-06-12 14:27:37 -07007561 env->insn_aux_data =
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007562 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007563 ret = -ENOMEM;
7564 if (!env->insn_aux_data)
7565 goto err_free_env;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -08007566 for (i = 0; i < len; i++)
7567 env->insn_aux_data[i].orig_idx = i;
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007568 env->prog = *prog;
Jakub Kicinski00176a32017-10-16 16:40:54 -07007569 env->ops = bpf_verifier_ops[env->prog->type];
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007570
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007571 /* grab the mutex to protect few globals used by verifier */
7572 mutex_lock(&bpf_verifier_lock);
7573
7574 if (attr->log_level || attr->log_buf || attr->log_size) {
7575 /* user requested verbose verifier output
7576 * and supplied buffer to store the verification trace
7577 */
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07007578 log->level = attr->log_level;
7579 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
7580 log->len_total = attr->log_size;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007581
7582 ret = -EINVAL;
Jakub Kicinskie7bf8242017-10-09 10:30:10 -07007583 /* log attributes have to be sane */
7584 if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
7585 !log->level || !log->ubuf)
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007586 goto err_unlock;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007587 }
Daniel Borkmann1ad2f582017-05-25 01:05:05 +02007588
7589 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
7590 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
David S. Millere07b98d2017-05-10 11:38:07 -07007591 env->strict_alignment = true;
David Millere9ee9ef2018-11-30 21:08:14 -08007592 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
7593 env->strict_alignment = false;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007594
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007595 is_priv = capable(CAP_SYS_ADMIN);
7596 env->allow_ptr_leaks = is_priv;
7597
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007598 ret = replace_map_fd_with_map_ptr(env);
7599 if (ret < 0)
7600 goto skip_full_check;
7601
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07007602 if (bpf_prog_is_dev_bound(env->prog->aux)) {
Quentin Monneta40a2632018-11-09 13:03:31 +00007603 ret = bpf_prog_offload_verifier_prep(env->prog);
Jakub Kicinskif4e3ec02018-05-03 18:37:11 -07007604 if (ret)
7605 goto skip_full_check;
7606 }
7607
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007608 env->explored_states = kcalloc(env->prog->len,
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01007609 sizeof(struct bpf_verifier_state_list *),
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007610 GFP_USER);
7611 ret = -ENOMEM;
7612 if (!env->explored_states)
7613 goto skip_full_check;
7614
Martin KaFai Laud9762e82018-12-13 10:41:48 -08007615 ret = check_subprogs(env);
Alexei Starovoitov475fb782014-09-26 00:17:05 -07007616 if (ret < 0)
7617 goto skip_full_check;
7618
Martin KaFai Lauc454a462018-12-07 16:42:25 -08007619 ret = check_btf_info(env, attr, uattr);
Yonghong Song838e9692018-11-19 15:29:11 -08007620 if (ret < 0)
7621 goto skip_full_check;
7622
Martin KaFai Laud9762e82018-12-13 10:41:48 -08007623 ret = check_cfg(env);
7624 if (ret < 0)
7625 goto skip_full_check;
7626
Alexei Starovoitov17a52672014-09-26 00:17:06 -07007627 ret = do_check(env);
Craig Gallek8c01c4f2017-11-02 11:18:01 -04007628 if (env->cur_state) {
7629 free_verifier_state(env->cur_state, true);
7630 env->cur_state = NULL;
7631 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007632
Quentin Monnetc941ce92018-10-07 12:56:47 +01007633 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
7634 ret = bpf_prog_offload_finalize(env);
7635
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007636skip_full_check:
Alexei Starovoitov638f5b92017-10-31 18:16:05 -07007637 while (!pop_stack(env, NULL, NULL));
Alexei Starovoitovf1bca822014-09-29 18:50:01 -07007638 free_states(env);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007639
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007640 if (ret == 0)
Alexei Starovoitov70a87ff2017-12-25 13:15:40 -08007641 ret = check_max_stack_depth(env);
7642
Jakub Kicinski9b38c402018-12-19 22:13:06 -08007643 /* instruction rewrites happen after this point */
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007644 if (is_priv) {
7645 if (ret == 0)
7646 opt_hard_wire_dead_code_branches(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08007647 if (ret == 0)
7648 ret = opt_remove_dead_code(env);
Jakub Kicinskia1b14ab2019-01-22 22:45:21 -08007649 if (ret == 0)
7650 ret = opt_remove_nops(env);
Jakub Kicinski52875a02019-01-22 22:45:20 -08007651 } else {
7652 if (ret == 0)
7653 sanitize_dead_code(env);
Jakub Kicinskie2ae4ca2019-01-22 22:45:19 -08007654 }
7655
Jakub Kicinski9b38c402018-12-19 22:13:06 -08007656 if (ret == 0)
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007657 /* program is valid, convert *(u32*)(ctx + off) accesses */
7658 ret = convert_ctx_accesses(env);
7659
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007660 if (ret == 0)
Alexei Starovoitov79741b32017-03-15 18:26:40 -07007661 ret = fixup_bpf_calls(env);
Alexei Starovoitove245c5c62017-03-15 18:26:39 -07007662
Alexei Starovoitov1ea47e02017-12-14 17:55:13 -08007663 if (ret == 0)
7664 ret = fixup_call_args(env);
7665
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007666 if (log->level && bpf_verifier_log_full(log))
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007667 ret = -ENOSPC;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007668 if (log->level && !log->ubuf) {
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007669 ret = -EFAULT;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007670 goto err_release_maps;
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007671 }
7672
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007673 if (ret == 0 && env->used_map_cnt) {
7674 /* if program passed verifier, update used_maps in bpf_prog_info */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007675 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
7676 sizeof(env->used_maps[0]),
7677 GFP_KERNEL);
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007678
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007679 if (!env->prog->aux->used_maps) {
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007680 ret = -ENOMEM;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007681 goto err_release_maps;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007682 }
7683
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007684 memcpy(env->prog->aux->used_maps, env->used_maps,
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007685 sizeof(env->used_maps[0]) * env->used_map_cnt);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007686 env->prog->aux->used_map_cnt = env->used_map_cnt;
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007687
7688 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
7689 * bpf_ld_imm64 instructions
7690 */
7691 convert_pseudo_ld_imm64(env);
7692 }
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007693
Yonghong Songba64e7d2018-11-24 23:20:44 -08007694 if (ret == 0)
7695 adjust_btf_func(env);
7696
Jakub Kicinskia2a7d572017-10-09 10:30:15 -07007697err_release_maps:
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007698 if (!env->prog->aux->used_maps)
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007699 /* if we didn't copy map pointers into bpf_prog_info, release
Jakub Kicinskiab7f5bf2018-05-03 18:37:17 -07007700 * them now. Otherwise free_used_maps() will release them.
Alexei Starovoitov0246e642014-09-26 00:17:04 -07007701 */
7702 release_maps(env);
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -07007703 *prog = env->prog;
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007704err_unlock:
Alexei Starovoitovcbd35702014-09-26 00:17:03 -07007705 mutex_unlock(&bpf_verifier_lock);
Jakub Kicinski3df126f2016-09-21 11:43:56 +01007706 vfree(env->insn_aux_data);
7707err_free_env:
7708 kfree(env);
Alexei Starovoitov51580e72014-09-26 00:17:02 -07007709 return ret;
7710}