ARC: [plat-hsdk]: unify memory apertures configuration
[linux-2.6-microblaze.git] / kernel / bpf / verifier.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3  * Copyright (c) 2016 Facebook
4  * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
5  */
6 #include <uapi/linux/btf.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/bpf.h>
11 #include <linux/btf.h>
12 #include <linux/bpf_verifier.h>
13 #include <linux/filter.h>
14 #include <net/netlink.h>
15 #include <linux/file.h>
16 #include <linux/vmalloc.h>
17 #include <linux/stringify.h>
18 #include <linux/bsearch.h>
19 #include <linux/sort.h>
20 #include <linux/perf_event.h>
21 #include <linux/ctype.h>
22
23 #include "disasm.h"
24
25 static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
26 #define BPF_PROG_TYPE(_id, _name) \
27         [_id] = & _name ## _verifier_ops,
28 #define BPF_MAP_TYPE(_id, _ops)
29 #include <linux/bpf_types.h>
30 #undef BPF_PROG_TYPE
31 #undef BPF_MAP_TYPE
32 };
33
34 /* bpf_check() is a static code analyzer that walks eBPF program
35  * instruction by instruction and updates register/stack state.
36  * All paths of conditional branches are analyzed until 'bpf_exit' insn.
37  *
38  * The first pass is depth-first-search to check that the program is a DAG.
39  * It rejects the following programs:
40  * - larger than BPF_MAXINSNS insns
41  * - if loop is present (detected via back-edge)
42  * - unreachable insns exist (shouldn't be a forest. program = one function)
43  * - out of bounds or malformed jumps
44  * The second pass is all possible path descent from the 1st insn.
45  * Since it's analyzing all pathes through the program, the length of the
46  * analysis is limited to 64k insn, which may be hit even if total number of
47  * insn is less then 4K, but there are too many branches that change stack/regs.
48  * Number of 'branches to be analyzed' is limited to 1k
49  *
50  * On entry to each instruction, each register has a type, and the instruction
51  * changes the types of the registers depending on instruction semantics.
52  * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is
53  * copied to R1.
54  *
55  * All registers are 64-bit.
56  * R0 - return register
57  * R1-R5 argument passing registers
58  * R6-R9 callee saved registers
59  * R10 - frame pointer read-only
60  *
61  * At the start of BPF program the register R1 contains a pointer to bpf_context
62  * and has type PTR_TO_CTX.
63  *
64  * Verifier tracks arithmetic operations on pointers in case:
65  *    BPF_MOV64_REG(BPF_REG_1, BPF_REG_10),
66  *    BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20),
67  * 1st insn copies R10 (which has FRAME_PTR) type into R1
68  * and 2nd arithmetic instruction is pattern matched to recognize
69  * that it wants to construct a pointer to some element within stack.
70  * So after 2nd insn, the register R1 has type PTR_TO_STACK
71  * (and -20 constant is saved for further stack bounds checking).
72  * Meaning that this reg is a pointer to stack plus known immediate constant.
73  *
74  * Most of the time the registers have SCALAR_VALUE type, which
75  * means the register has some value, but it's not a valid pointer.
76  * (like pointer plus pointer becomes SCALAR_VALUE type)
77  *
78  * When verifier sees load or store instructions the type of base register
79  * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are
80  * four pointer types recognized by check_mem_access() function.
81  *
82  * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value'
83  * and the range of [ptr, ptr + map's value_size) is accessible.
84  *
85  * registers used to pass values to function calls are checked against
86  * function argument constraints.
87  *
88  * ARG_PTR_TO_MAP_KEY is one of such argument constraints.
89  * It means that the register type passed to this function must be
90  * PTR_TO_STACK and it will be used inside the function as
91  * 'pointer to map element key'
92  *
93  * For example the argument constraints for bpf_map_lookup_elem():
94  *   .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
95  *   .arg1_type = ARG_CONST_MAP_PTR,
96  *   .arg2_type = ARG_PTR_TO_MAP_KEY,
97  *
98  * ret_type says that this function returns 'pointer to map elem value or null'
99  * function expects 1st argument to be a const pointer to 'struct bpf_map' and
100  * 2nd argument should be a pointer to stack, which will be used inside
101  * the helper function as a pointer to map element key.
102  *
103  * On the kernel side the helper function looks like:
104  * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
105  * {
106  *    struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
107  *    void *key = (void *) (unsigned long) r2;
108  *    void *value;
109  *
110  *    here kernel can access 'key' and 'map' pointers safely, knowing that
111  *    [key, key + map->key_size) bytes are valid and were initialized on
112  *    the stack of eBPF program.
113  * }
114  *
115  * Corresponding eBPF program may look like:
116  *    BPF_MOV64_REG(BPF_REG_2, BPF_REG_10),  // after this insn R2 type is FRAME_PTR
117  *    BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK
118  *    BPF_LD_MAP_FD(BPF_REG_1, map_fd),      // after this insn R1 type is CONST_PTR_TO_MAP
119  *    BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem),
120  * here verifier looks at prototype of map_lookup_elem() and sees:
121  * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok,
122  * Now verifier knows that this map has key of R1->map_ptr->key_size bytes
123  *
124  * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far,
125  * Now verifier checks that [R2, R2 + map's key_size) are within stack limits
126  * and were initialized prior to this call.
127  * If it's ok, then verifier allows this BPF_CALL insn and looks at
128  * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets
129  * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function
130  * returns ether pointer to map value or NULL.
131  *
132  * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off'
133  * insn, the register holding that pointer in the true branch changes state to
134  * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false
135  * branch. See check_cond_jmp_op().
136  *
137  * After the call R0 is set to return type of the function and registers R1-R5
138  * are set to NOT_INIT to indicate that they are no longer readable.
139  *
140  * The following reference types represent a potential reference to a kernel
141  * resource which, after first being allocated, must be checked and freed by
142  * the BPF program:
143  * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET
144  *
145  * When the verifier sees a helper call return a reference type, it allocates a
146  * pointer id for the reference and stores it in the current function state.
147  * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into
148  * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type
149  * passes through a NULL-check conditional. For the branch wherein the state is
150  * changed to CONST_IMM, the verifier releases the reference.
151  *
152  * For each helper function that allocates a reference, such as
153  * bpf_sk_lookup_tcp(), there is a corresponding release function, such as
154  * bpf_sk_release(). When a reference type passes into the release function,
155  * the verifier also releases the reference. If any unchecked or unreleased
156  * reference remains at the end of the program, the verifier rejects it.
157  */
158
159 /* verifier_state + insn_idx are pushed to stack when branch is encountered */
160 struct bpf_verifier_stack_elem {
161         /* verifer state is 'st'
162          * before processing instruction 'insn_idx'
163          * and after processing instruction 'prev_insn_idx'
164          */
165         struct bpf_verifier_state st;
166         int insn_idx;
167         int prev_insn_idx;
168         struct bpf_verifier_stack_elem *next;
169 };
170
171 #define BPF_COMPLEXITY_LIMIT_STACK      1024
172 #define BPF_COMPLEXITY_LIMIT_STATES     64
173
174 #define BPF_MAP_PTR_UNPRIV      1UL
175 #define BPF_MAP_PTR_POISON      ((void *)((0xeB9FUL << 1) +     \
176                                           POISON_POINTER_DELTA))
177 #define BPF_MAP_PTR(X)          ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
178
179 static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
180 {
181         return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
182 }
183
184 static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
185 {
186         return aux->map_state & BPF_MAP_PTR_UNPRIV;
187 }
188
189 static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
190                               const struct bpf_map *map, bool unpriv)
191 {
192         BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
193         unpriv |= bpf_map_ptr_unpriv(aux);
194         aux->map_state = (unsigned long)map |
195                          (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
196 }
197
198 struct bpf_call_arg_meta {
199         struct bpf_map *map_ptr;
200         bool raw_mode;
201         bool pkt_access;
202         int regno;
203         int access_size;
204         s64 msize_smax_value;
205         u64 msize_umax_value;
206         int ref_obj_id;
207         int func_id;
208 };
209
210 static DEFINE_MUTEX(bpf_verifier_lock);
211
212 static const struct bpf_line_info *
213 find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
214 {
215         const struct bpf_line_info *linfo;
216         const struct bpf_prog *prog;
217         u32 i, nr_linfo;
218
219         prog = env->prog;
220         nr_linfo = prog->aux->nr_linfo;
221
222         if (!nr_linfo || insn_off >= prog->len)
223                 return NULL;
224
225         linfo = prog->aux->linfo;
226         for (i = 1; i < nr_linfo; i++)
227                 if (insn_off < linfo[i].insn_off)
228                         break;
229
230         return &linfo[i - 1];
231 }
232
233 void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
234                        va_list args)
235 {
236         unsigned int n;
237
238         n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
239
240         WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
241                   "verifier log line truncated - local buffer too short\n");
242
243         n = min(log->len_total - log->len_used - 1, n);
244         log->kbuf[n] = '\0';
245
246         if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
247                 log->len_used += n;
248         else
249                 log->ubuf = NULL;
250 }
251
252 /* log_level controls verbosity level of eBPF verifier.
253  * bpf_verifier_log_write() is used to dump the verification trace to the log,
254  * so the user can figure out what's wrong with the program
255  */
256 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
257                                            const char *fmt, ...)
258 {
259         va_list args;
260
261         if (!bpf_verifier_log_needed(&env->log))
262                 return;
263
264         va_start(args, fmt);
265         bpf_verifier_vlog(&env->log, fmt, args);
266         va_end(args);
267 }
268 EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
269
270 __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
271 {
272         struct bpf_verifier_env *env = private_data;
273         va_list args;
274
275         if (!bpf_verifier_log_needed(&env->log))
276                 return;
277
278         va_start(args, fmt);
279         bpf_verifier_vlog(&env->log, fmt, args);
280         va_end(args);
281 }
282
283 static const char *ltrim(const char *s)
284 {
285         while (isspace(*s))
286                 s++;
287
288         return s;
289 }
290
291 __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
292                                          u32 insn_off,
293                                          const char *prefix_fmt, ...)
294 {
295         const struct bpf_line_info *linfo;
296
297         if (!bpf_verifier_log_needed(&env->log))
298                 return;
299
300         linfo = find_linfo(env, insn_off);
301         if (!linfo || linfo == env->prev_linfo)
302                 return;
303
304         if (prefix_fmt) {
305                 va_list args;
306
307                 va_start(args, prefix_fmt);
308                 bpf_verifier_vlog(&env->log, prefix_fmt, args);
309                 va_end(args);
310         }
311
312         verbose(env, "%s\n",
313                 ltrim(btf_name_by_offset(env->prog->aux->btf,
314                                          linfo->line_off)));
315
316         env->prev_linfo = linfo;
317 }
318
319 static bool type_is_pkt_pointer(enum bpf_reg_type type)
320 {
321         return type == PTR_TO_PACKET ||
322                type == PTR_TO_PACKET_META;
323 }
324
325 static bool type_is_sk_pointer(enum bpf_reg_type type)
326 {
327         return type == PTR_TO_SOCKET ||
328                 type == PTR_TO_SOCK_COMMON ||
329                 type == PTR_TO_TCP_SOCK;
330 }
331
332 static bool reg_type_may_be_null(enum bpf_reg_type type)
333 {
334         return type == PTR_TO_MAP_VALUE_OR_NULL ||
335                type == PTR_TO_SOCKET_OR_NULL ||
336                type == PTR_TO_SOCK_COMMON_OR_NULL ||
337                type == PTR_TO_TCP_SOCK_OR_NULL;
338 }
339
340 static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
341 {
342         return reg->type == PTR_TO_MAP_VALUE &&
343                 map_value_has_spin_lock(reg->map_ptr);
344 }
345
346 static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
347 {
348         return type == PTR_TO_SOCKET ||
349                 type == PTR_TO_SOCKET_OR_NULL ||
350                 type == PTR_TO_TCP_SOCK ||
351                 type == PTR_TO_TCP_SOCK_OR_NULL;
352 }
353
354 static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
355 {
356         return type == ARG_PTR_TO_SOCK_COMMON;
357 }
358
359 /* Determine whether the function releases some resources allocated by another
360  * function call. The first reference type argument will be assumed to be
361  * released by release_reference().
362  */
363 static bool is_release_function(enum bpf_func_id func_id)
364 {
365         return func_id == BPF_FUNC_sk_release;
366 }
367
368 static bool is_acquire_function(enum bpf_func_id func_id)
369 {
370         return func_id == BPF_FUNC_sk_lookup_tcp ||
371                 func_id == BPF_FUNC_sk_lookup_udp ||
372                 func_id == BPF_FUNC_skc_lookup_tcp;
373 }
374
375 static bool is_ptr_cast_function(enum bpf_func_id func_id)
376 {
377         return func_id == BPF_FUNC_tcp_sock ||
378                 func_id == BPF_FUNC_sk_fullsock;
379 }
380
381 /* string representation of 'enum bpf_reg_type' */
382 static const char * const reg_type_str[] = {
383         [NOT_INIT]              = "?",
384         [SCALAR_VALUE]          = "inv",
385         [PTR_TO_CTX]            = "ctx",
386         [CONST_PTR_TO_MAP]      = "map_ptr",
387         [PTR_TO_MAP_VALUE]      = "map_value",
388         [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
389         [PTR_TO_STACK]          = "fp",
390         [PTR_TO_PACKET]         = "pkt",
391         [PTR_TO_PACKET_META]    = "pkt_meta",
392         [PTR_TO_PACKET_END]     = "pkt_end",
393         [PTR_TO_FLOW_KEYS]      = "flow_keys",
394         [PTR_TO_SOCKET]         = "sock",
395         [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
396         [PTR_TO_SOCK_COMMON]    = "sock_common",
397         [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
398         [PTR_TO_TCP_SOCK]       = "tcp_sock",
399         [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
400         [PTR_TO_TP_BUFFER]      = "tp_buffer",
401 };
402
403 static char slot_type_char[] = {
404         [STACK_INVALID] = '?',
405         [STACK_SPILL]   = 'r',
406         [STACK_MISC]    = 'm',
407         [STACK_ZERO]    = '0',
408 };
409
410 static void print_liveness(struct bpf_verifier_env *env,
411                            enum bpf_reg_liveness live)
412 {
413         if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
414             verbose(env, "_");
415         if (live & REG_LIVE_READ)
416                 verbose(env, "r");
417         if (live & REG_LIVE_WRITTEN)
418                 verbose(env, "w");
419         if (live & REG_LIVE_DONE)
420                 verbose(env, "D");
421 }
422
423 static struct bpf_func_state *func(struct bpf_verifier_env *env,
424                                    const struct bpf_reg_state *reg)
425 {
426         struct bpf_verifier_state *cur = env->cur_state;
427
428         return cur->frame[reg->frameno];
429 }
430
431 static void print_verifier_state(struct bpf_verifier_env *env,
432                                  const struct bpf_func_state *state)
433 {
434         const struct bpf_reg_state *reg;
435         enum bpf_reg_type t;
436         int i;
437
438         if (state->frameno)
439                 verbose(env, " frame%d:", state->frameno);
440         for (i = 0; i < MAX_BPF_REG; i++) {
441                 reg = &state->regs[i];
442                 t = reg->type;
443                 if (t == NOT_INIT)
444                         continue;
445                 verbose(env, " R%d", i);
446                 print_liveness(env, reg->live);
447                 verbose(env, "=%s", reg_type_str[t]);
448                 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
449                     tnum_is_const(reg->var_off)) {
450                         /* reg->off should be 0 for SCALAR_VALUE */
451                         verbose(env, "%lld", reg->var_off.value + reg->off);
452                         if (t == PTR_TO_STACK)
453                                 verbose(env, ",call_%d", func(env, reg)->callsite);
454                 } else {
455                         verbose(env, "(id=%d", reg->id);
456                         if (reg_type_may_be_refcounted_or_null(t))
457                                 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
458                         if (t != SCALAR_VALUE)
459                                 verbose(env, ",off=%d", reg->off);
460                         if (type_is_pkt_pointer(t))
461                                 verbose(env, ",r=%d", reg->range);
462                         else if (t == CONST_PTR_TO_MAP ||
463                                  t == PTR_TO_MAP_VALUE ||
464                                  t == PTR_TO_MAP_VALUE_OR_NULL)
465                                 verbose(env, ",ks=%d,vs=%d",
466                                         reg->map_ptr->key_size,
467                                         reg->map_ptr->value_size);
468                         if (tnum_is_const(reg->var_off)) {
469                                 /* Typically an immediate SCALAR_VALUE, but
470                                  * could be a pointer whose offset is too big
471                                  * for reg->off
472                                  */
473                                 verbose(env, ",imm=%llx", reg->var_off.value);
474                         } else {
475                                 if (reg->smin_value != reg->umin_value &&
476                                     reg->smin_value != S64_MIN)
477                                         verbose(env, ",smin_value=%lld",
478                                                 (long long)reg->smin_value);
479                                 if (reg->smax_value != reg->umax_value &&
480                                     reg->smax_value != S64_MAX)
481                                         verbose(env, ",smax_value=%lld",
482                                                 (long long)reg->smax_value);
483                                 if (reg->umin_value != 0)
484                                         verbose(env, ",umin_value=%llu",
485                                                 (unsigned long long)reg->umin_value);
486                                 if (reg->umax_value != U64_MAX)
487                                         verbose(env, ",umax_value=%llu",
488                                                 (unsigned long long)reg->umax_value);
489                                 if (!tnum_is_unknown(reg->var_off)) {
490                                         char tn_buf[48];
491
492                                         tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
493                                         verbose(env, ",var_off=%s", tn_buf);
494                                 }
495                         }
496                         verbose(env, ")");
497                 }
498         }
499         for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
500                 char types_buf[BPF_REG_SIZE + 1];
501                 bool valid = false;
502                 int j;
503
504                 for (j = 0; j < BPF_REG_SIZE; j++) {
505                         if (state->stack[i].slot_type[j] != STACK_INVALID)
506                                 valid = true;
507                         types_buf[j] = slot_type_char[
508                                         state->stack[i].slot_type[j]];
509                 }
510                 types_buf[BPF_REG_SIZE] = 0;
511                 if (!valid)
512                         continue;
513                 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
514                 print_liveness(env, state->stack[i].spilled_ptr.live);
515                 if (state->stack[i].slot_type[0] == STACK_SPILL)
516                         verbose(env, "=%s",
517                                 reg_type_str[state->stack[i].spilled_ptr.type]);
518                 else
519                         verbose(env, "=%s", types_buf);
520         }
521         if (state->acquired_refs && state->refs[0].id) {
522                 verbose(env, " refs=%d", state->refs[0].id);
523                 for (i = 1; i < state->acquired_refs; i++)
524                         if (state->refs[i].id)
525                                 verbose(env, ",%d", state->refs[i].id);
526         }
527         verbose(env, "\n");
528 }
529
530 #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE)                         \
531 static int copy_##NAME##_state(struct bpf_func_state *dst,              \
532                                const struct bpf_func_state *src)        \
533 {                                                                       \
534         if (!src->FIELD)                                                \
535                 return 0;                                               \
536         if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) {                    \
537                 /* internal bug, make state invalid to reject the program */ \
538                 memset(dst, 0, sizeof(*dst));                           \
539                 return -EFAULT;                                         \
540         }                                                               \
541         memcpy(dst->FIELD, src->FIELD,                                  \
542                sizeof(*src->FIELD) * (src->COUNT / SIZE));              \
543         return 0;                                                       \
544 }
545 /* copy_reference_state() */
546 COPY_STATE_FN(reference, acquired_refs, refs, 1)
547 /* copy_stack_state() */
548 COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
549 #undef COPY_STATE_FN
550
551 #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE)                      \
552 static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
553                                   bool copy_old)                        \
554 {                                                                       \
555         u32 old_size = state->COUNT;                                    \
556         struct bpf_##NAME##_state *new_##FIELD;                         \
557         int slot = size / SIZE;                                         \
558                                                                         \
559         if (size <= old_size || !size) {                                \
560                 if (copy_old)                                           \
561                         return 0;                                       \
562                 state->COUNT = slot * SIZE;                             \
563                 if (!size && old_size) {                                \
564                         kfree(state->FIELD);                            \
565                         state->FIELD = NULL;                            \
566                 }                                                       \
567                 return 0;                                               \
568         }                                                               \
569         new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
570                                     GFP_KERNEL);                        \
571         if (!new_##FIELD)                                               \
572                 return -ENOMEM;                                         \
573         if (copy_old) {                                                 \
574                 if (state->FIELD)                                       \
575                         memcpy(new_##FIELD, state->FIELD,               \
576                                sizeof(*new_##FIELD) * (old_size / SIZE)); \
577                 memset(new_##FIELD + old_size / SIZE, 0,                \
578                        sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
579         }                                                               \
580         state->COUNT = slot * SIZE;                                     \
581         kfree(state->FIELD);                                            \
582         state->FIELD = new_##FIELD;                                     \
583         return 0;                                                       \
584 }
585 /* realloc_reference_state() */
586 REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
587 /* realloc_stack_state() */
588 REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
589 #undef REALLOC_STATE_FN
590
591 /* do_check() starts with zero-sized stack in struct bpf_verifier_state to
592  * make it consume minimal amount of memory. check_stack_write() access from
593  * the program calls into realloc_func_state() to grow the stack size.
594  * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
595  * which realloc_stack_state() copies over. It points to previous
596  * bpf_verifier_state which is never reallocated.
597  */
598 static int realloc_func_state(struct bpf_func_state *state, int stack_size,
599                               int refs_size, bool copy_old)
600 {
601         int err = realloc_reference_state(state, refs_size, copy_old);
602         if (err)
603                 return err;
604         return realloc_stack_state(state, stack_size, copy_old);
605 }
606
607 /* Acquire a pointer id from the env and update the state->refs to include
608  * this new pointer reference.
609  * On success, returns a valid pointer id to associate with the register
610  * On failure, returns a negative errno.
611  */
612 static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
613 {
614         struct bpf_func_state *state = cur_func(env);
615         int new_ofs = state->acquired_refs;
616         int id, err;
617
618         err = realloc_reference_state(state, state->acquired_refs + 1, true);
619         if (err)
620                 return err;
621         id = ++env->id_gen;
622         state->refs[new_ofs].id = id;
623         state->refs[new_ofs].insn_idx = insn_idx;
624
625         return id;
626 }
627
628 /* release function corresponding to acquire_reference_state(). Idempotent. */
629 static int release_reference_state(struct bpf_func_state *state, int ptr_id)
630 {
631         int i, last_idx;
632
633         last_idx = state->acquired_refs - 1;
634         for (i = 0; i < state->acquired_refs; i++) {
635                 if (state->refs[i].id == ptr_id) {
636                         if (last_idx && i != last_idx)
637                                 memcpy(&state->refs[i], &state->refs[last_idx],
638                                        sizeof(*state->refs));
639                         memset(&state->refs[last_idx], 0, sizeof(*state->refs));
640                         state->acquired_refs--;
641                         return 0;
642                 }
643         }
644         return -EINVAL;
645 }
646
647 static int transfer_reference_state(struct bpf_func_state *dst,
648                                     struct bpf_func_state *src)
649 {
650         int err = realloc_reference_state(dst, src->acquired_refs, false);
651         if (err)
652                 return err;
653         err = copy_reference_state(dst, src);
654         if (err)
655                 return err;
656         return 0;
657 }
658
659 static void free_func_state(struct bpf_func_state *state)
660 {
661         if (!state)
662                 return;
663         kfree(state->refs);
664         kfree(state->stack);
665         kfree(state);
666 }
667
668 static void free_verifier_state(struct bpf_verifier_state *state,
669                                 bool free_self)
670 {
671         int i;
672
673         for (i = 0; i <= state->curframe; i++) {
674                 free_func_state(state->frame[i]);
675                 state->frame[i] = NULL;
676         }
677         if (free_self)
678                 kfree(state);
679 }
680
681 /* copy verifier state from src to dst growing dst stack space
682  * when necessary to accommodate larger src stack
683  */
684 static int copy_func_state(struct bpf_func_state *dst,
685                            const struct bpf_func_state *src)
686 {
687         int err;
688
689         err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
690                                  false);
691         if (err)
692                 return err;
693         memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
694         err = copy_reference_state(dst, src);
695         if (err)
696                 return err;
697         return copy_stack_state(dst, src);
698 }
699
700 static int copy_verifier_state(struct bpf_verifier_state *dst_state,
701                                const struct bpf_verifier_state *src)
702 {
703         struct bpf_func_state *dst;
704         int i, err;
705
706         /* if dst has more stack frames then src frame, free them */
707         for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
708                 free_func_state(dst_state->frame[i]);
709                 dst_state->frame[i] = NULL;
710         }
711         dst_state->speculative = src->speculative;
712         dst_state->curframe = src->curframe;
713         dst_state->active_spin_lock = src->active_spin_lock;
714         for (i = 0; i <= src->curframe; i++) {
715                 dst = dst_state->frame[i];
716                 if (!dst) {
717                         dst = kzalloc(sizeof(*dst), GFP_KERNEL);
718                         if (!dst)
719                                 return -ENOMEM;
720                         dst_state->frame[i] = dst;
721                 }
722                 err = copy_func_state(dst, src->frame[i]);
723                 if (err)
724                         return err;
725         }
726         return 0;
727 }
728
729 static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
730                      int *insn_idx)
731 {
732         struct bpf_verifier_state *cur = env->cur_state;
733         struct bpf_verifier_stack_elem *elem, *head = env->head;
734         int err;
735
736         if (env->head == NULL)
737                 return -ENOENT;
738
739         if (cur) {
740                 err = copy_verifier_state(cur, &head->st);
741                 if (err)
742                         return err;
743         }
744         if (insn_idx)
745                 *insn_idx = head->insn_idx;
746         if (prev_insn_idx)
747                 *prev_insn_idx = head->prev_insn_idx;
748         elem = head->next;
749         free_verifier_state(&head->st, false);
750         kfree(head);
751         env->head = elem;
752         env->stack_size--;
753         return 0;
754 }
755
756 static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
757                                              int insn_idx, int prev_insn_idx,
758                                              bool speculative)
759 {
760         struct bpf_verifier_state *cur = env->cur_state;
761         struct bpf_verifier_stack_elem *elem;
762         int err;
763
764         elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
765         if (!elem)
766                 goto err;
767
768         elem->insn_idx = insn_idx;
769         elem->prev_insn_idx = prev_insn_idx;
770         elem->next = env->head;
771         env->head = elem;
772         env->stack_size++;
773         err = copy_verifier_state(&elem->st, cur);
774         if (err)
775                 goto err;
776         elem->st.speculative |= speculative;
777         if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
778                 verbose(env, "BPF program is too complex\n");
779                 goto err;
780         }
781         return &elem->st;
782 err:
783         free_verifier_state(env->cur_state, true);
784         env->cur_state = NULL;
785         /* pop all elements and return */
786         while (!pop_stack(env, NULL, NULL));
787         return NULL;
788 }
789
790 #define CALLER_SAVED_REGS 6
791 static const int caller_saved[CALLER_SAVED_REGS] = {
792         BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
793 };
794
795 static void __mark_reg_not_init(struct bpf_reg_state *reg);
796
797 /* Mark the unknown part of a register (variable offset or scalar value) as
798  * known to have the value @imm.
799  */
800 static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
801 {
802         /* Clear id, off, and union(map_ptr, range) */
803         memset(((u8 *)reg) + sizeof(reg->type), 0,
804                offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
805         reg->var_off = tnum_const(imm);
806         reg->smin_value = (s64)imm;
807         reg->smax_value = (s64)imm;
808         reg->umin_value = imm;
809         reg->umax_value = imm;
810 }
811
812 /* Mark the 'variable offset' part of a register as zero.  This should be
813  * used only on registers holding a pointer type.
814  */
815 static void __mark_reg_known_zero(struct bpf_reg_state *reg)
816 {
817         __mark_reg_known(reg, 0);
818 }
819
820 static void __mark_reg_const_zero(struct bpf_reg_state *reg)
821 {
822         __mark_reg_known(reg, 0);
823         reg->type = SCALAR_VALUE;
824 }
825
826 static void mark_reg_known_zero(struct bpf_verifier_env *env,
827                                 struct bpf_reg_state *regs, u32 regno)
828 {
829         if (WARN_ON(regno >= MAX_BPF_REG)) {
830                 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
831                 /* Something bad happened, let's kill all regs */
832                 for (regno = 0; regno < MAX_BPF_REG; regno++)
833                         __mark_reg_not_init(regs + regno);
834                 return;
835         }
836         __mark_reg_known_zero(regs + regno);
837 }
838
839 static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
840 {
841         return type_is_pkt_pointer(reg->type);
842 }
843
844 static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
845 {
846         return reg_is_pkt_pointer(reg) ||
847                reg->type == PTR_TO_PACKET_END;
848 }
849
850 /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
851 static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
852                                     enum bpf_reg_type which)
853 {
854         /* The register can already have a range from prior markings.
855          * This is fine as long as it hasn't been advanced from its
856          * origin.
857          */
858         return reg->type == which &&
859                reg->id == 0 &&
860                reg->off == 0 &&
861                tnum_equals_const(reg->var_off, 0);
862 }
863
864 /* Attempts to improve min/max values based on var_off information */
865 static void __update_reg_bounds(struct bpf_reg_state *reg)
866 {
867         /* min signed is max(sign bit) | min(other bits) */
868         reg->smin_value = max_t(s64, reg->smin_value,
869                                 reg->var_off.value | (reg->var_off.mask & S64_MIN));
870         /* max signed is min(sign bit) | max(other bits) */
871         reg->smax_value = min_t(s64, reg->smax_value,
872                                 reg->var_off.value | (reg->var_off.mask & S64_MAX));
873         reg->umin_value = max(reg->umin_value, reg->var_off.value);
874         reg->umax_value = min(reg->umax_value,
875                               reg->var_off.value | reg->var_off.mask);
876 }
877
878 /* Uses signed min/max values to inform unsigned, and vice-versa */
879 static void __reg_deduce_bounds(struct bpf_reg_state *reg)
880 {
881         /* Learn sign from signed bounds.
882          * If we cannot cross the sign boundary, then signed and unsigned bounds
883          * are the same, so combine.  This works even in the negative case, e.g.
884          * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
885          */
886         if (reg->smin_value >= 0 || reg->smax_value < 0) {
887                 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
888                                                           reg->umin_value);
889                 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
890                                                           reg->umax_value);
891                 return;
892         }
893         /* Learn sign from unsigned bounds.  Signed bounds cross the sign
894          * boundary, so we must be careful.
895          */
896         if ((s64)reg->umax_value >= 0) {
897                 /* Positive.  We can't learn anything from the smin, but smax
898                  * is positive, hence safe.
899                  */
900                 reg->smin_value = reg->umin_value;
901                 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
902                                                           reg->umax_value);
903         } else if ((s64)reg->umin_value < 0) {
904                 /* Negative.  We can't learn anything from the smax, but smin
905                  * is negative, hence safe.
906                  */
907                 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
908                                                           reg->umin_value);
909                 reg->smax_value = reg->umax_value;
910         }
911 }
912
913 /* Attempts to improve var_off based on unsigned min/max information */
914 static void __reg_bound_offset(struct bpf_reg_state *reg)
915 {
916         reg->var_off = tnum_intersect(reg->var_off,
917                                       tnum_range(reg->umin_value,
918                                                  reg->umax_value));
919 }
920
921 /* Reset the min/max bounds of a register */
922 static void __mark_reg_unbounded(struct bpf_reg_state *reg)
923 {
924         reg->smin_value = S64_MIN;
925         reg->smax_value = S64_MAX;
926         reg->umin_value = 0;
927         reg->umax_value = U64_MAX;
928 }
929
930 /* Mark a register as having a completely unknown (scalar) value. */
931 static void __mark_reg_unknown(struct bpf_reg_state *reg)
932 {
933         /*
934          * Clear type, id, off, and union(map_ptr, range) and
935          * padding between 'type' and union
936          */
937         memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
938         reg->type = SCALAR_VALUE;
939         reg->var_off = tnum_unknown;
940         reg->frameno = 0;
941         __mark_reg_unbounded(reg);
942 }
943
944 static void mark_reg_unknown(struct bpf_verifier_env *env,
945                              struct bpf_reg_state *regs, u32 regno)
946 {
947         if (WARN_ON(regno >= MAX_BPF_REG)) {
948                 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
949                 /* Something bad happened, let's kill all regs except FP */
950                 for (regno = 0; regno < BPF_REG_FP; regno++)
951                         __mark_reg_not_init(regs + regno);
952                 return;
953         }
954         __mark_reg_unknown(regs + regno);
955 }
956
957 static void __mark_reg_not_init(struct bpf_reg_state *reg)
958 {
959         __mark_reg_unknown(reg);
960         reg->type = NOT_INIT;
961 }
962
963 static void mark_reg_not_init(struct bpf_verifier_env *env,
964                               struct bpf_reg_state *regs, u32 regno)
965 {
966         if (WARN_ON(regno >= MAX_BPF_REG)) {
967                 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
968                 /* Something bad happened, let's kill all regs except FP */
969                 for (regno = 0; regno < BPF_REG_FP; regno++)
970                         __mark_reg_not_init(regs + regno);
971                 return;
972         }
973         __mark_reg_not_init(regs + regno);
974 }
975
976 static void init_reg_state(struct bpf_verifier_env *env,
977                            struct bpf_func_state *state)
978 {
979         struct bpf_reg_state *regs = state->regs;
980         int i;
981
982         for (i = 0; i < MAX_BPF_REG; i++) {
983                 mark_reg_not_init(env, regs, i);
984                 regs[i].live = REG_LIVE_NONE;
985                 regs[i].parent = NULL;
986         }
987
988         /* frame pointer */
989         regs[BPF_REG_FP].type = PTR_TO_STACK;
990         mark_reg_known_zero(env, regs, BPF_REG_FP);
991         regs[BPF_REG_FP].frameno = state->frameno;
992
993         /* 1st arg to a function */
994         regs[BPF_REG_1].type = PTR_TO_CTX;
995         mark_reg_known_zero(env, regs, BPF_REG_1);
996 }
997
998 #define BPF_MAIN_FUNC (-1)
999 static void init_func_state(struct bpf_verifier_env *env,
1000                             struct bpf_func_state *state,
1001                             int callsite, int frameno, int subprogno)
1002 {
1003         state->callsite = callsite;
1004         state->frameno = frameno;
1005         state->subprogno = subprogno;
1006         init_reg_state(env, state);
1007 }
1008
1009 enum reg_arg_type {
1010         SRC_OP,         /* register is used as source operand */
1011         DST_OP,         /* register is used as destination operand */
1012         DST_OP_NO_MARK  /* same as above, check only, don't mark */
1013 };
1014
1015 static int cmp_subprogs(const void *a, const void *b)
1016 {
1017         return ((struct bpf_subprog_info *)a)->start -
1018                ((struct bpf_subprog_info *)b)->start;
1019 }
1020
1021 static int find_subprog(struct bpf_verifier_env *env, int off)
1022 {
1023         struct bpf_subprog_info *p;
1024
1025         p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1026                     sizeof(env->subprog_info[0]), cmp_subprogs);
1027         if (!p)
1028                 return -ENOENT;
1029         return p - env->subprog_info;
1030
1031 }
1032
1033 static int add_subprog(struct bpf_verifier_env *env, int off)
1034 {
1035         int insn_cnt = env->prog->len;
1036         int ret;
1037
1038         if (off >= insn_cnt || off < 0) {
1039                 verbose(env, "call to invalid destination\n");
1040                 return -EINVAL;
1041         }
1042         ret = find_subprog(env, off);
1043         if (ret >= 0)
1044                 return 0;
1045         if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
1046                 verbose(env, "too many subprograms\n");
1047                 return -E2BIG;
1048         }
1049         env->subprog_info[env->subprog_cnt++].start = off;
1050         sort(env->subprog_info, env->subprog_cnt,
1051              sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
1052         return 0;
1053 }
1054
1055 static int check_subprogs(struct bpf_verifier_env *env)
1056 {
1057         int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
1058         struct bpf_subprog_info *subprog = env->subprog_info;
1059         struct bpf_insn *insn = env->prog->insnsi;
1060         int insn_cnt = env->prog->len;
1061
1062         /* Add entry function. */
1063         ret = add_subprog(env, 0);
1064         if (ret < 0)
1065                 return ret;
1066
1067         /* determine subprog starts. The end is one before the next starts */
1068         for (i = 0; i < insn_cnt; i++) {
1069                 if (insn[i].code != (BPF_JMP | BPF_CALL))
1070                         continue;
1071                 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1072                         continue;
1073                 if (!env->allow_ptr_leaks) {
1074                         verbose(env, "function calls to other bpf functions are allowed for root only\n");
1075                         return -EPERM;
1076                 }
1077                 ret = add_subprog(env, i + insn[i].imm + 1);
1078                 if (ret < 0)
1079                         return ret;
1080         }
1081
1082         /* Add a fake 'exit' subprog which could simplify subprog iteration
1083          * logic. 'subprog_cnt' should not be increased.
1084          */
1085         subprog[env->subprog_cnt].start = insn_cnt;
1086
1087         if (env->log.level & BPF_LOG_LEVEL2)
1088                 for (i = 0; i < env->subprog_cnt; i++)
1089                         verbose(env, "func#%d @%d\n", i, subprog[i].start);
1090
1091         /* now check that all jumps are within the same subprog */
1092         subprog_start = subprog[cur_subprog].start;
1093         subprog_end = subprog[cur_subprog + 1].start;
1094         for (i = 0; i < insn_cnt; i++) {
1095                 u8 code = insn[i].code;
1096
1097                 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
1098                         goto next;
1099                 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1100                         goto next;
1101                 off = i + insn[i].off + 1;
1102                 if (off < subprog_start || off >= subprog_end) {
1103                         verbose(env, "jump out of range from insn %d to %d\n", i, off);
1104                         return -EINVAL;
1105                 }
1106 next:
1107                 if (i == subprog_end - 1) {
1108                         /* to avoid fall-through from one subprog into another
1109                          * the last insn of the subprog should be either exit
1110                          * or unconditional jump back
1111                          */
1112                         if (code != (BPF_JMP | BPF_EXIT) &&
1113                             code != (BPF_JMP | BPF_JA)) {
1114                                 verbose(env, "last insn is not an exit or jmp\n");
1115                                 return -EINVAL;
1116                         }
1117                         subprog_start = subprog_end;
1118                         cur_subprog++;
1119                         if (cur_subprog < env->subprog_cnt)
1120                                 subprog_end = subprog[cur_subprog + 1].start;
1121                 }
1122         }
1123         return 0;
1124 }
1125
1126 /* Parentage chain of this register (or stack slot) should take care of all
1127  * issues like callee-saved registers, stack slot allocation time, etc.
1128  */
1129 static int mark_reg_read(struct bpf_verifier_env *env,
1130                          const struct bpf_reg_state *state,
1131                          struct bpf_reg_state *parent)
1132 {
1133         bool writes = parent == state->parent; /* Observe write marks */
1134         int cnt = 0;
1135
1136         while (parent) {
1137                 /* if read wasn't screened by an earlier write ... */
1138                 if (writes && state->live & REG_LIVE_WRITTEN)
1139                         break;
1140                 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                 }
1146                 if (parent->live & REG_LIVE_READ)
1147                         /* The parentage chain never changes and
1148                          * this parent was already marked as LIVE_READ.
1149                          * There is no need to keep walking the chain again and
1150                          * keep re-marking all parents as LIVE_READ.
1151                          * This case happens when the same register is read
1152                          * multiple times without writes into it in-between.
1153                          */
1154                         break;
1155                 /* ... then we depend on parent's value */
1156                 parent->live |= REG_LIVE_READ;
1157                 state = parent;
1158                 parent = state->parent;
1159                 writes = true;
1160                 cnt++;
1161         }
1162
1163         if (env->longest_mark_read_walk < cnt)
1164                 env->longest_mark_read_walk = cnt;
1165         return 0;
1166 }
1167
1168 static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
1169                          enum reg_arg_type t)
1170 {
1171         struct bpf_verifier_state *vstate = env->cur_state;
1172         struct bpf_func_state *state = vstate->frame[vstate->curframe];
1173         struct bpf_reg_state *reg, *regs = state->regs;
1174
1175         if (regno >= MAX_BPF_REG) {
1176                 verbose(env, "R%d is invalid\n", regno);
1177                 return -EINVAL;
1178         }
1179
1180         reg = &regs[regno];
1181         if (t == SRC_OP) {
1182                 /* check whether register used as source operand can be read */
1183                 if (reg->type == NOT_INIT) {
1184                         verbose(env, "R%d !read_ok\n", regno);
1185                         return -EACCES;
1186                 }
1187                 /* We don't need to worry about FP liveness because it's read-only */
1188                 if (regno == BPF_REG_FP)
1189                         return 0;
1190
1191                 return mark_reg_read(env, reg, reg->parent);
1192         } else {
1193                 /* check whether register used as dest operand can be written to */
1194                 if (regno == BPF_REG_FP) {
1195                         verbose(env, "frame pointer is read only\n");
1196                         return -EACCES;
1197                 }
1198                 reg->live |= REG_LIVE_WRITTEN;
1199                 if (t == DST_OP)
1200                         mark_reg_unknown(env, regs, regno);
1201         }
1202         return 0;
1203 }
1204
1205 static bool is_spillable_regtype(enum bpf_reg_type type)
1206 {
1207         switch (type) {
1208         case PTR_TO_MAP_VALUE:
1209         case PTR_TO_MAP_VALUE_OR_NULL:
1210         case PTR_TO_STACK:
1211         case PTR_TO_CTX:
1212         case PTR_TO_PACKET:
1213         case PTR_TO_PACKET_META:
1214         case PTR_TO_PACKET_END:
1215         case PTR_TO_FLOW_KEYS:
1216         case CONST_PTR_TO_MAP:
1217         case PTR_TO_SOCKET:
1218         case PTR_TO_SOCKET_OR_NULL:
1219         case PTR_TO_SOCK_COMMON:
1220         case PTR_TO_SOCK_COMMON_OR_NULL:
1221         case PTR_TO_TCP_SOCK:
1222         case PTR_TO_TCP_SOCK_OR_NULL:
1223                 return true;
1224         default:
1225                 return false;
1226         }
1227 }
1228
1229 /* Does this register contain a constant zero? */
1230 static bool register_is_null(struct bpf_reg_state *reg)
1231 {
1232         return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1233 }
1234
1235 /* check_stack_read/write functions track spill/fill of registers,
1236  * stack boundary and alignment are checked in check_mem_access()
1237  */
1238 static int check_stack_write(struct bpf_verifier_env *env,
1239                              struct bpf_func_state *state, /* func where register points to */
1240                              int off, int size, int value_regno, int insn_idx)
1241 {
1242         struct bpf_func_state *cur; /* state of the current function */
1243         int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
1244         enum bpf_reg_type type;
1245
1246         err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
1247                                  state->acquired_refs, true);
1248         if (err)
1249                 return err;
1250         /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
1251          * so it's aligned access and [off, off + size) are within stack limits
1252          */
1253         if (!env->allow_ptr_leaks &&
1254             state->stack[spi].slot_type[0] == STACK_SPILL &&
1255             size != BPF_REG_SIZE) {
1256                 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1257                 return -EACCES;
1258         }
1259
1260         cur = env->cur_state->frame[env->cur_state->curframe];
1261         if (value_regno >= 0 &&
1262             is_spillable_regtype((type = cur->regs[value_regno].type))) {
1263
1264                 /* register containing pointer is being spilled into stack */
1265                 if (size != BPF_REG_SIZE) {
1266                         verbose(env, "invalid size of register spill\n");
1267                         return -EACCES;
1268                 }
1269
1270                 if (state != cur && type == PTR_TO_STACK) {
1271                         verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1272                         return -EINVAL;
1273                 }
1274
1275                 /* save register state */
1276                 state->stack[spi].spilled_ptr = cur->regs[value_regno];
1277                 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1278
1279                 for (i = 0; i < BPF_REG_SIZE; i++) {
1280                         if (state->stack[spi].slot_type[i] == STACK_MISC &&
1281                             !env->allow_ptr_leaks) {
1282                                 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1283                                 int soff = (-spi - 1) * BPF_REG_SIZE;
1284
1285                                 /* detected reuse of integer stack slot with a pointer
1286                                  * which means either llvm is reusing stack slot or
1287                                  * an attacker is trying to exploit CVE-2018-3639
1288                                  * (speculative store bypass)
1289                                  * Have to sanitize that slot with preemptive
1290                                  * store of zero.
1291                                  */
1292                                 if (*poff && *poff != soff) {
1293                                         /* disallow programs where single insn stores
1294                                          * into two different stack slots, since verifier
1295                                          * cannot sanitize them
1296                                          */
1297                                         verbose(env,
1298                                                 "insn %d cannot access two stack slots fp%d and fp%d",
1299                                                 insn_idx, *poff, soff);
1300                                         return -EINVAL;
1301                                 }
1302                                 *poff = soff;
1303                         }
1304                         state->stack[spi].slot_type[i] = STACK_SPILL;
1305                 }
1306         } else {
1307                 u8 type = STACK_MISC;
1308
1309                 /* regular write of data into stack destroys any spilled ptr */
1310                 state->stack[spi].spilled_ptr.type = NOT_INIT;
1311                 /* Mark slots as STACK_MISC if they belonged to spilled ptr. */
1312                 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1313                         for (i = 0; i < BPF_REG_SIZE; i++)
1314                                 state->stack[spi].slot_type[i] = STACK_MISC;
1315
1316                 /* only mark the slot as written if all 8 bytes were written
1317                  * otherwise read propagation may incorrectly stop too soon
1318                  * when stack slots are partially written.
1319                  * This heuristic means that read propagation will be
1320                  * conservative, since it will add reg_live_read marks
1321                  * to stack slots all the way to first state when programs
1322                  * writes+reads less than 8 bytes
1323                  */
1324                 if (size == BPF_REG_SIZE)
1325                         state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1326
1327                 /* when we zero initialize stack slots mark them as such */
1328                 if (value_regno >= 0 &&
1329                     register_is_null(&cur->regs[value_regno]))
1330                         type = STACK_ZERO;
1331
1332                 /* Mark slots affected by this stack write. */
1333                 for (i = 0; i < size; i++)
1334                         state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
1335                                 type;
1336         }
1337         return 0;
1338 }
1339
1340 static int check_stack_read(struct bpf_verifier_env *env,
1341                             struct bpf_func_state *reg_state /* func where register points to */,
1342                             int off, int size, int value_regno)
1343 {
1344         struct bpf_verifier_state *vstate = env->cur_state;
1345         struct bpf_func_state *state = vstate->frame[vstate->curframe];
1346         int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
1347         u8 *stype;
1348
1349         if (reg_state->allocated_stack <= slot) {
1350                 verbose(env, "invalid read from stack off %d+0 size %d\n",
1351                         off, size);
1352                 return -EACCES;
1353         }
1354         stype = reg_state->stack[spi].slot_type;
1355
1356         if (stype[0] == STACK_SPILL) {
1357                 if (size != BPF_REG_SIZE) {
1358                         verbose(env, "invalid size of register spill\n");
1359                         return -EACCES;
1360                 }
1361                 for (i = 1; i < BPF_REG_SIZE; i++) {
1362                         if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
1363                                 verbose(env, "corrupted spill memory\n");
1364                                 return -EACCES;
1365                         }
1366                 }
1367
1368                 if (value_regno >= 0) {
1369                         /* restore register state from stack */
1370                         state->regs[value_regno] = reg_state->stack[spi].spilled_ptr;
1371                         /* mark reg as written since spilled pointer state likely
1372                          * has its liveness marks cleared by is_state_visited()
1373                          * which resets stack/reg liveness for state transitions
1374                          */
1375                         state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1376                 }
1377                 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1378                               reg_state->stack[spi].spilled_ptr.parent);
1379                 return 0;
1380         } else {
1381                 int zeros = 0;
1382
1383                 for (i = 0; i < size; i++) {
1384                         if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
1385                                 continue;
1386                         if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
1387                                 zeros++;
1388                                 continue;
1389                         }
1390                         verbose(env, "invalid read from stack off %d+%d size %d\n",
1391                                 off, i, size);
1392                         return -EACCES;
1393                 }
1394                 mark_reg_read(env, &reg_state->stack[spi].spilled_ptr,
1395                               reg_state->stack[spi].spilled_ptr.parent);
1396                 if (value_regno >= 0) {
1397                         if (zeros == size) {
1398                                 /* any size read into register is zero extended,
1399                                  * so the whole register == const_zero
1400                                  */
1401                                 __mark_reg_const_zero(&state->regs[value_regno]);
1402                         } else {
1403                                 /* have read misc data from the stack */
1404                                 mark_reg_unknown(env, state->regs, value_regno);
1405                         }
1406                         state->regs[value_regno].live |= REG_LIVE_WRITTEN;
1407                 }
1408                 return 0;
1409         }
1410 }
1411
1412 static int check_stack_access(struct bpf_verifier_env *env,
1413                               const struct bpf_reg_state *reg,
1414                               int off, int size)
1415 {
1416         /* Stack accesses must be at a fixed offset, so that we
1417          * can determine what type of data were returned. See
1418          * check_stack_read().
1419          */
1420         if (!tnum_is_const(reg->var_off)) {
1421                 char tn_buf[48];
1422
1423                 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1424                 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
1425                         tn_buf, off, size);
1426                 return -EACCES;
1427         }
1428
1429         if (off >= 0 || off < -MAX_BPF_STACK) {
1430                 verbose(env, "invalid stack off=%d size=%d\n", off, size);
1431                 return -EACCES;
1432         }
1433
1434         return 0;
1435 }
1436
1437 static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
1438                                  int off, int size, enum bpf_access_type type)
1439 {
1440         struct bpf_reg_state *regs = cur_regs(env);
1441         struct bpf_map *map = regs[regno].map_ptr;
1442         u32 cap = bpf_map_flags_to_cap(map);
1443
1444         if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
1445                 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
1446                         map->value_size, off, size);
1447                 return -EACCES;
1448         }
1449
1450         if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
1451                 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
1452                         map->value_size, off, size);
1453                 return -EACCES;
1454         }
1455
1456         return 0;
1457 }
1458
1459 /* check read/write into map element returned by bpf_map_lookup_elem() */
1460 static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
1461                               int size, bool zero_size_allowed)
1462 {
1463         struct bpf_reg_state *regs = cur_regs(env);
1464         struct bpf_map *map = regs[regno].map_ptr;
1465
1466         if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1467             off + size > map->value_size) {
1468                 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
1469                         map->value_size, off, size);
1470                 return -EACCES;
1471         }
1472         return 0;
1473 }
1474
1475 /* check read/write into a map element with possible variable offset */
1476 static int check_map_access(struct bpf_verifier_env *env, u32 regno,
1477                             int off, int size, bool zero_size_allowed)
1478 {
1479         struct bpf_verifier_state *vstate = env->cur_state;
1480         struct bpf_func_state *state = vstate->frame[vstate->curframe];
1481         struct bpf_reg_state *reg = &state->regs[regno];
1482         int err;
1483
1484         /* We may have adjusted the register to this map value, so we
1485          * need to try adding each of min_value and max_value to off
1486          * to make sure our theoretical access will be safe.
1487          */
1488         if (env->log.level & BPF_LOG_LEVEL)
1489                 print_verifier_state(env, state);
1490
1491         /* The minimum value is only important with signed
1492          * comparisons where we can't assume the floor of a
1493          * value is 0.  If we are using signed variables for our
1494          * index'es we need to make sure that whatever we use
1495          * will have a set floor within our range.
1496          */
1497         if (reg->smin_value < 0 &&
1498             (reg->smin_value == S64_MIN ||
1499              (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
1500               reg->smin_value + off < 0)) {
1501                 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1502                         regno);
1503                 return -EACCES;
1504         }
1505         err = __check_map_access(env, regno, reg->smin_value + off, size,
1506                                  zero_size_allowed);
1507         if (err) {
1508                 verbose(env, "R%d min value is outside of the array range\n",
1509                         regno);
1510                 return err;
1511         }
1512
1513         /* If we haven't set a max value then we need to bail since we can't be
1514          * sure we won't do bad things.
1515          * If reg->umax_value + off could overflow, treat that as unbounded too.
1516          */
1517         if (reg->umax_value >= BPF_MAX_VAR_OFF) {
1518                 verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
1519                         regno);
1520                 return -EACCES;
1521         }
1522         err = __check_map_access(env, regno, reg->umax_value + off, size,
1523                                  zero_size_allowed);
1524         if (err)
1525                 verbose(env, "R%d max value is outside of the array range\n",
1526                         regno);
1527
1528         if (map_value_has_spin_lock(reg->map_ptr)) {
1529                 u32 lock = reg->map_ptr->spin_lock_off;
1530
1531                 /* if any part of struct bpf_spin_lock can be touched by
1532                  * load/store reject this program.
1533                  * To check that [x1, x2) overlaps with [y1, y2)
1534                  * it is sufficient to check x1 < y2 && y1 < x2.
1535                  */
1536                 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
1537                      lock < reg->umax_value + off + size) {
1538                         verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
1539                         return -EACCES;
1540                 }
1541         }
1542         return err;
1543 }
1544
1545 #define MAX_PACKET_OFF 0xffff
1546
1547 static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
1548                                        const struct bpf_call_arg_meta *meta,
1549                                        enum bpf_access_type t)
1550 {
1551         switch (env->prog->type) {
1552         /* Program types only with direct read access go here! */
1553         case BPF_PROG_TYPE_LWT_IN:
1554         case BPF_PROG_TYPE_LWT_OUT:
1555         case BPF_PROG_TYPE_LWT_SEG6LOCAL:
1556         case BPF_PROG_TYPE_SK_REUSEPORT:
1557         case BPF_PROG_TYPE_FLOW_DISSECTOR:
1558         case BPF_PROG_TYPE_CGROUP_SKB:
1559                 if (t == BPF_WRITE)
1560                         return false;
1561                 /* fallthrough */
1562
1563         /* Program types with direct read + write access go here! */
1564         case BPF_PROG_TYPE_SCHED_CLS:
1565         case BPF_PROG_TYPE_SCHED_ACT:
1566         case BPF_PROG_TYPE_XDP:
1567         case BPF_PROG_TYPE_LWT_XMIT:
1568         case BPF_PROG_TYPE_SK_SKB:
1569         case BPF_PROG_TYPE_SK_MSG:
1570                 if (meta)
1571                         return meta->pkt_access;
1572
1573                 env->seen_direct_write = true;
1574                 return true;
1575         default:
1576                 return false;
1577         }
1578 }
1579
1580 static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
1581                                  int off, int size, bool zero_size_allowed)
1582 {
1583         struct bpf_reg_state *regs = cur_regs(env);
1584         struct bpf_reg_state *reg = &regs[regno];
1585
1586         if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
1587             (u64)off + size > reg->range) {
1588                 verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
1589                         off, size, regno, reg->id, reg->off, reg->range);
1590                 return -EACCES;
1591         }
1592         return 0;
1593 }
1594
1595 static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
1596                                int size, bool zero_size_allowed)
1597 {
1598         struct bpf_reg_state *regs = cur_regs(env);
1599         struct bpf_reg_state *reg = &regs[regno];
1600         int err;
1601
1602         /* We may have added a variable offset to the packet pointer; but any
1603          * reg->range we have comes after that.  We are only checking the fixed
1604          * offset.
1605          */
1606
1607         /* We don't allow negative numbers, because we aren't tracking enough
1608          * detail to prove they're safe.
1609          */
1610         if (reg->smin_value < 0) {
1611                 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1612                         regno);
1613                 return -EACCES;
1614         }
1615         err = __check_packet_access(env, regno, off, size, zero_size_allowed);
1616         if (err) {
1617                 verbose(env, "R%d offset is outside of the packet\n", regno);
1618                 return err;
1619         }
1620
1621         /* __check_packet_access has made sure "off + size - 1" is within u16.
1622          * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff,
1623          * otherwise find_good_pkt_pointers would have refused to set range info
1624          * that __check_packet_access would have rejected this pkt access.
1625          * Therefore, "off + reg->umax_value + size - 1" won't overflow u32.
1626          */
1627         env->prog->aux->max_pkt_offset =
1628                 max_t(u32, env->prog->aux->max_pkt_offset,
1629                       off + reg->umax_value + size - 1);
1630
1631         return err;
1632 }
1633
1634 /* check access to 'struct bpf_context' fields.  Supports fixed offsets only */
1635 static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
1636                             enum bpf_access_type t, enum bpf_reg_type *reg_type)
1637 {
1638         struct bpf_insn_access_aux info = {
1639                 .reg_type = *reg_type,
1640         };
1641
1642         if (env->ops->is_valid_access &&
1643             env->ops->is_valid_access(off, size, t, env->prog, &info)) {
1644                 /* A non zero info.ctx_field_size indicates that this field is a
1645                  * candidate for later verifier transformation to load the whole
1646                  * field and then apply a mask when accessed with a narrower
1647                  * access than actual ctx access size. A zero info.ctx_field_size
1648                  * will only allow for whole field access and rejects any other
1649                  * type of narrower access.
1650                  */
1651                 *reg_type = info.reg_type;
1652
1653                 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
1654                 /* remember the offset of last byte accessed in ctx */
1655                 if (env->prog->aux->max_ctx_offset < off + size)
1656                         env->prog->aux->max_ctx_offset = off + size;
1657                 return 0;
1658         }
1659
1660         verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
1661         return -EACCES;
1662 }
1663
1664 static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
1665                                   int size)
1666 {
1667         if (size < 0 || off < 0 ||
1668             (u64)off + size > sizeof(struct bpf_flow_keys)) {
1669                 verbose(env, "invalid access to flow keys off=%d size=%d\n",
1670                         off, size);
1671                 return -EACCES;
1672         }
1673         return 0;
1674 }
1675
1676 static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
1677                              u32 regno, int off, int size,
1678                              enum bpf_access_type t)
1679 {
1680         struct bpf_reg_state *regs = cur_regs(env);
1681         struct bpf_reg_state *reg = &regs[regno];
1682         struct bpf_insn_access_aux info = {};
1683         bool valid;
1684
1685         if (reg->smin_value < 0) {
1686                 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
1687                         regno);
1688                 return -EACCES;
1689         }
1690
1691         switch (reg->type) {
1692         case PTR_TO_SOCK_COMMON:
1693                 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
1694                 break;
1695         case PTR_TO_SOCKET:
1696                 valid = bpf_sock_is_valid_access(off, size, t, &info);
1697                 break;
1698         case PTR_TO_TCP_SOCK:
1699                 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
1700                 break;
1701         default:
1702                 valid = false;
1703         }
1704
1705
1706         if (valid) {
1707                 env->insn_aux_data[insn_idx].ctx_field_size =
1708                         info.ctx_field_size;
1709                 return 0;
1710         }
1711
1712         verbose(env, "R%d invalid %s access off=%d size=%d\n",
1713                 regno, reg_type_str[reg->type], off, size);
1714
1715         return -EACCES;
1716 }
1717
1718 static bool __is_pointer_value(bool allow_ptr_leaks,
1719                                const struct bpf_reg_state *reg)
1720 {
1721         if (allow_ptr_leaks)
1722                 return false;
1723
1724         return reg->type != SCALAR_VALUE;
1725 }
1726
1727 static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
1728 {
1729         return cur_regs(env) + regno;
1730 }
1731
1732 static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
1733 {
1734         return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
1735 }
1736
1737 static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
1738 {
1739         const struct bpf_reg_state *reg = reg_state(env, regno);
1740
1741         return reg->type == PTR_TO_CTX;
1742 }
1743
1744 static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
1745 {
1746         const struct bpf_reg_state *reg = reg_state(env, regno);
1747
1748         return type_is_sk_pointer(reg->type);
1749 }
1750
1751 static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
1752 {
1753         const struct bpf_reg_state *reg = reg_state(env, regno);
1754
1755         return type_is_pkt_pointer(reg->type);
1756 }
1757
1758 static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
1759 {
1760         const struct bpf_reg_state *reg = reg_state(env, regno);
1761
1762         /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */
1763         return reg->type == PTR_TO_FLOW_KEYS;
1764 }
1765
1766 static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
1767                                    const struct bpf_reg_state *reg,
1768                                    int off, int size, bool strict)
1769 {
1770         struct tnum reg_off;
1771         int ip_align;
1772
1773         /* Byte size accesses are always allowed. */
1774         if (!strict || size == 1)
1775                 return 0;
1776
1777         /* For platforms that do not have a Kconfig enabling
1778          * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of
1779          * NET_IP_ALIGN is universally set to '2'.  And on platforms
1780          * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get
1781          * to this code only in strict mode where we want to emulate
1782          * the NET_IP_ALIGN==2 checking.  Therefore use an
1783          * unconditional IP align value of '2'.
1784          */
1785         ip_align = 2;
1786
1787         reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
1788         if (!tnum_is_aligned(reg_off, size)) {
1789                 char tn_buf[48];
1790
1791                 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1792                 verbose(env,
1793                         "misaligned packet access off %d+%s+%d+%d size %d\n",
1794                         ip_align, tn_buf, reg->off, off, size);
1795                 return -EACCES;
1796         }
1797
1798         return 0;
1799 }
1800
1801 static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
1802                                        const struct bpf_reg_state *reg,
1803                                        const char *pointer_desc,
1804                                        int off, int size, bool strict)
1805 {
1806         struct tnum reg_off;
1807
1808         /* Byte size accesses are always allowed. */
1809         if (!strict || size == 1)
1810                 return 0;
1811
1812         reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
1813         if (!tnum_is_aligned(reg_off, size)) {
1814                 char tn_buf[48];
1815
1816                 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1817                 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
1818                         pointer_desc, tn_buf, reg->off, off, size);
1819                 return -EACCES;
1820         }
1821
1822         return 0;
1823 }
1824
1825 static int check_ptr_alignment(struct bpf_verifier_env *env,
1826                                const struct bpf_reg_state *reg, int off,
1827                                int size, bool strict_alignment_once)
1828 {
1829         bool strict = env->strict_alignment || strict_alignment_once;
1830         const char *pointer_desc = "";
1831
1832         switch (reg->type) {
1833         case PTR_TO_PACKET:
1834         case PTR_TO_PACKET_META:
1835                 /* Special case, because of NET_IP_ALIGN. Given metadata sits
1836                  * right in front, treat it the very same way.
1837                  */
1838                 return check_pkt_ptr_alignment(env, reg, off, size, strict);
1839         case PTR_TO_FLOW_KEYS:
1840                 pointer_desc = "flow keys ";
1841                 break;
1842         case PTR_TO_MAP_VALUE:
1843                 pointer_desc = "value ";
1844                 break;
1845         case PTR_TO_CTX:
1846                 pointer_desc = "context ";
1847                 break;
1848         case PTR_TO_STACK:
1849                 pointer_desc = "stack ";
1850                 /* The stack spill tracking logic in check_stack_write()
1851                  * and check_stack_read() relies on stack accesses being
1852                  * aligned.
1853                  */
1854                 strict = true;
1855                 break;
1856         case PTR_TO_SOCKET:
1857                 pointer_desc = "sock ";
1858                 break;
1859         case PTR_TO_SOCK_COMMON:
1860                 pointer_desc = "sock_common ";
1861                 break;
1862         case PTR_TO_TCP_SOCK:
1863                 pointer_desc = "tcp_sock ";
1864                 break;
1865         default:
1866                 break;
1867         }
1868         return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
1869                                            strict);
1870 }
1871
1872 static int update_stack_depth(struct bpf_verifier_env *env,
1873                               const struct bpf_func_state *func,
1874                               int off)
1875 {
1876         u16 stack = env->subprog_info[func->subprogno].stack_depth;
1877
1878         if (stack >= -off)
1879                 return 0;
1880
1881         /* update known max for given subprogram */
1882         env->subprog_info[func->subprogno].stack_depth = -off;
1883         return 0;
1884 }
1885
1886 /* starting from main bpf function walk all instructions of the function
1887  * and recursively walk all callees that given function can call.
1888  * Ignore jump and exit insns.
1889  * Since recursion is prevented by check_cfg() this algorithm
1890  * only needs a local stack of MAX_CALL_FRAMES to remember callsites
1891  */
1892 static int check_max_stack_depth(struct bpf_verifier_env *env)
1893 {
1894         int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
1895         struct bpf_subprog_info *subprog = env->subprog_info;
1896         struct bpf_insn *insn = env->prog->insnsi;
1897         int ret_insn[MAX_CALL_FRAMES];
1898         int ret_prog[MAX_CALL_FRAMES];
1899
1900 process_func:
1901         /* round up to 32-bytes, since this is granularity
1902          * of interpreter stack size
1903          */
1904         depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
1905         if (depth > MAX_BPF_STACK) {
1906                 verbose(env, "combined stack size of %d calls is %d. Too large\n",
1907                         frame + 1, depth);
1908                 return -EACCES;
1909         }
1910 continue_func:
1911         subprog_end = subprog[idx + 1].start;
1912         for (; i < subprog_end; i++) {
1913                 if (insn[i].code != (BPF_JMP | BPF_CALL))
1914                         continue;
1915                 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1916                         continue;
1917                 /* remember insn and function to return to */
1918                 ret_insn[frame] = i + 1;
1919                 ret_prog[frame] = idx;
1920
1921                 /* find the callee */
1922                 i = i + insn[i].imm + 1;
1923                 idx = find_subprog(env, i);
1924                 if (idx < 0) {
1925                         WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1926                                   i);
1927                         return -EFAULT;
1928                 }
1929                 frame++;
1930                 if (frame >= MAX_CALL_FRAMES) {
1931                         verbose(env, "the call stack of %d frames is too deep !\n",
1932                                 frame);
1933                         return -E2BIG;
1934                 }
1935                 goto process_func;
1936         }
1937         /* end of for() loop means the last insn of the 'subprog'
1938          * was reached. Doesn't matter whether it was JA or EXIT
1939          */
1940         if (frame == 0)
1941                 return 0;
1942         depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
1943         frame--;
1944         i = ret_insn[frame];
1945         idx = ret_prog[frame];
1946         goto continue_func;
1947 }
1948
1949 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
1950 static int get_callee_stack_depth(struct bpf_verifier_env *env,
1951                                   const struct bpf_insn *insn, int idx)
1952 {
1953         int start = idx + insn->imm + 1, subprog;
1954
1955         subprog = find_subprog(env, start);
1956         if (subprog < 0) {
1957                 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
1958                           start);
1959                 return -EFAULT;
1960         }
1961         return env->subprog_info[subprog].stack_depth;
1962 }
1963 #endif
1964
1965 static int check_ctx_reg(struct bpf_verifier_env *env,
1966                          const struct bpf_reg_state *reg, int regno)
1967 {
1968         /* Access to ctx or passing it to a helper is only allowed in
1969          * its original, unmodified form.
1970          */
1971
1972         if (reg->off) {
1973                 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
1974                         regno, reg->off);
1975                 return -EACCES;
1976         }
1977
1978         if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
1979                 char tn_buf[48];
1980
1981                 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
1982                 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
1983                 return -EACCES;
1984         }
1985
1986         return 0;
1987 }
1988
1989 static int check_tp_buffer_access(struct bpf_verifier_env *env,
1990                                   const struct bpf_reg_state *reg,
1991                                   int regno, int off, int size)
1992 {
1993         if (off < 0) {
1994                 verbose(env,
1995                         "R%d invalid tracepoint buffer access: off=%d, size=%d",
1996                         regno, off, size);
1997                 return -EACCES;
1998         }
1999         if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2000                 char tn_buf[48];
2001
2002                 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2003                 verbose(env,
2004                         "R%d invalid variable buffer offset: off=%d, var_off=%s",
2005                         regno, off, tn_buf);
2006                 return -EACCES;
2007         }
2008         if (off + size > env->prog->aux->max_tp_access)
2009                 env->prog->aux->max_tp_access = off + size;
2010
2011         return 0;
2012 }
2013
2014
2015 /* truncate register to smaller size (in bytes)
2016  * must be called with size < BPF_REG_SIZE
2017  */
2018 static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2019 {
2020         u64 mask;
2021
2022         /* clear high bits in bit representation */
2023         reg->var_off = tnum_cast(reg->var_off, size);
2024
2025         /* fix arithmetic bounds */
2026         mask = ((u64)1 << (size * 8)) - 1;
2027         if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2028                 reg->umin_value &= mask;
2029                 reg->umax_value &= mask;
2030         } else {
2031                 reg->umin_value = 0;
2032                 reg->umax_value = mask;
2033         }
2034         reg->smin_value = reg->umin_value;
2035         reg->smax_value = reg->umax_value;
2036 }
2037
2038 /* check whether memory at (regno + off) is accessible for t = (read | write)
2039  * if t==write, value_regno is a register which value is stored into memory
2040  * if t==read, value_regno is a register which will receive the value from memory
2041  * if t==write && value_regno==-1, some unknown value is stored into memory
2042  * if t==read && value_regno==-1, don't care what we read from memory
2043  */
2044 static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2045                             int off, int bpf_size, enum bpf_access_type t,
2046                             int value_regno, bool strict_alignment_once)
2047 {
2048         struct bpf_reg_state *regs = cur_regs(env);
2049         struct bpf_reg_state *reg = regs + regno;
2050         struct bpf_func_state *state;
2051         int size, err = 0;
2052
2053         size = bpf_size_to_bytes(bpf_size);
2054         if (size < 0)
2055                 return size;
2056
2057         /* alignment checks will add in reg->off themselves */
2058         err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
2059         if (err)
2060                 return err;
2061
2062         /* for access checks, reg->off is just part of off */
2063         off += reg->off;
2064
2065         if (reg->type == PTR_TO_MAP_VALUE) {
2066                 if (t == BPF_WRITE && value_regno >= 0 &&
2067                     is_pointer_value(env, value_regno)) {
2068                         verbose(env, "R%d leaks addr into map\n", value_regno);
2069                         return -EACCES;
2070                 }
2071                 err = check_map_access_type(env, regno, off, size, t);
2072                 if (err)
2073                         return err;
2074                 err = check_map_access(env, regno, off, size, false);
2075                 if (!err && t == BPF_READ && value_regno >= 0)
2076                         mark_reg_unknown(env, regs, value_regno);
2077
2078         } else if (reg->type == PTR_TO_CTX) {
2079                 enum bpf_reg_type reg_type = SCALAR_VALUE;
2080
2081                 if (t == BPF_WRITE && value_regno >= 0 &&
2082                     is_pointer_value(env, value_regno)) {
2083                         verbose(env, "R%d leaks addr into ctx\n", value_regno);
2084                         return -EACCES;
2085                 }
2086
2087                 err = check_ctx_reg(env, reg, regno);
2088                 if (err < 0)
2089                         return err;
2090
2091                 err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
2092                 if (!err && t == BPF_READ && value_regno >= 0) {
2093                         /* ctx access returns either a scalar, or a
2094                          * PTR_TO_PACKET[_META,_END]. In the latter
2095                          * case, we know the offset is zero.
2096                          */
2097                         if (reg_type == SCALAR_VALUE) {
2098                                 mark_reg_unknown(env, regs, value_regno);
2099                         } else {
2100                                 mark_reg_known_zero(env, regs,
2101                                                     value_regno);
2102                                 if (reg_type_may_be_null(reg_type))
2103                                         regs[value_regno].id = ++env->id_gen;
2104                         }
2105                         regs[value_regno].type = reg_type;
2106                 }
2107
2108         } else if (reg->type == PTR_TO_STACK) {
2109                 off += reg->var_off.value;
2110                 err = check_stack_access(env, reg, off, size);
2111                 if (err)
2112                         return err;
2113
2114                 state = func(env, reg);
2115                 err = update_stack_depth(env, state, off);
2116                 if (err)
2117                         return err;
2118
2119                 if (t == BPF_WRITE)
2120                         err = check_stack_write(env, state, off, size,
2121                                                 value_regno, insn_idx);
2122                 else
2123                         err = check_stack_read(env, state, off, size,
2124                                                value_regno);
2125         } else if (reg_is_pkt_pointer(reg)) {
2126                 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
2127                         verbose(env, "cannot write into packet\n");
2128                         return -EACCES;
2129                 }
2130                 if (t == BPF_WRITE && value_regno >= 0 &&
2131                     is_pointer_value(env, value_regno)) {
2132                         verbose(env, "R%d leaks addr into packet\n",
2133                                 value_regno);
2134                         return -EACCES;
2135                 }
2136                 err = check_packet_access(env, regno, off, size, false);
2137                 if (!err && t == BPF_READ && value_regno >= 0)
2138                         mark_reg_unknown(env, regs, value_regno);
2139         } else if (reg->type == PTR_TO_FLOW_KEYS) {
2140                 if (t == BPF_WRITE && value_regno >= 0 &&
2141                     is_pointer_value(env, value_regno)) {
2142                         verbose(env, "R%d leaks addr into flow keys\n",
2143                                 value_regno);
2144                         return -EACCES;
2145                 }
2146
2147                 err = check_flow_keys_access(env, off, size);
2148                 if (!err && t == BPF_READ && value_regno >= 0)
2149                         mark_reg_unknown(env, regs, value_regno);
2150         } else if (type_is_sk_pointer(reg->type)) {
2151                 if (t == BPF_WRITE) {
2152                         verbose(env, "R%d cannot write into %s\n",
2153                                 regno, reg_type_str[reg->type]);
2154                         return -EACCES;
2155                 }
2156                 err = check_sock_access(env, insn_idx, regno, off, size, t);
2157                 if (!err && value_regno >= 0)
2158                         mark_reg_unknown(env, regs, value_regno);
2159         } else if (reg->type == PTR_TO_TP_BUFFER) {
2160                 err = check_tp_buffer_access(env, reg, regno, off, size);
2161                 if (!err && t == BPF_READ && value_regno >= 0)
2162                         mark_reg_unknown(env, regs, value_regno);
2163         } else {
2164                 verbose(env, "R%d invalid mem access '%s'\n", regno,
2165                         reg_type_str[reg->type]);
2166                 return -EACCES;
2167         }
2168
2169         if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
2170             regs[value_regno].type == SCALAR_VALUE) {
2171                 /* b/h/w load zero-extends, mark upper bits as known 0 */
2172                 coerce_reg_to_size(&regs[value_regno], size);
2173         }
2174         return err;
2175 }
2176
2177 static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
2178 {
2179         int err;
2180
2181         if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2182             insn->imm != 0) {
2183                 verbose(env, "BPF_XADD uses reserved fields\n");
2184                 return -EINVAL;
2185         }
2186
2187         /* check src1 operand */
2188         err = check_reg_arg(env, insn->src_reg, SRC_OP);
2189         if (err)
2190                 return err;
2191
2192         /* check src2 operand */
2193         err = check_reg_arg(env, insn->dst_reg, SRC_OP);
2194         if (err)
2195                 return err;
2196
2197         if (is_pointer_value(env, insn->src_reg)) {
2198                 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
2199                 return -EACCES;
2200         }
2201
2202         if (is_ctx_reg(env, insn->dst_reg) ||
2203             is_pkt_reg(env, insn->dst_reg) ||
2204             is_flow_key_reg(env, insn->dst_reg) ||
2205             is_sk_reg(env, insn->dst_reg)) {
2206                 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
2207                         insn->dst_reg,
2208                         reg_type_str[reg_state(env, insn->dst_reg)->type]);
2209                 return -EACCES;
2210         }
2211
2212         /* check whether atomic_add can read the memory */
2213         err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
2214                                BPF_SIZE(insn->code), BPF_READ, -1, true);
2215         if (err)
2216                 return err;
2217
2218         /* check whether atomic_add can write into the same memory */
2219         return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
2220                                 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
2221 }
2222
2223 static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2224                                   int off, int access_size,
2225                                   bool zero_size_allowed)
2226 {
2227         struct bpf_reg_state *reg = reg_state(env, regno);
2228
2229         if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2230             access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2231                 if (tnum_is_const(reg->var_off)) {
2232                         verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2233                                 regno, off, access_size);
2234                 } else {
2235                         char tn_buf[48];
2236
2237                         tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2238                         verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2239                                 regno, tn_buf, access_size);
2240                 }
2241                 return -EACCES;
2242         }
2243         return 0;
2244 }
2245
2246 /* when register 'regno' is passed into function that will read 'access_size'
2247  * bytes from that pointer, make sure that it's within stack boundary
2248  * and all elements of stack are initialized.
2249  * Unlike most pointer bounds-checking functions, this one doesn't take an
2250  * 'off' argument, so it has to add in reg->off itself.
2251  */
2252 static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
2253                                 int access_size, bool zero_size_allowed,
2254                                 struct bpf_call_arg_meta *meta)
2255 {
2256         struct bpf_reg_state *reg = reg_state(env, regno);
2257         struct bpf_func_state *state = func(env, reg);
2258         int err, min_off, max_off, i, slot, spi;
2259
2260         if (reg->type != PTR_TO_STACK) {
2261                 /* Allow zero-byte read from NULL, regardless of pointer type */
2262                 if (zero_size_allowed && access_size == 0 &&
2263                     register_is_null(reg))
2264                         return 0;
2265
2266                 verbose(env, "R%d type=%s expected=%s\n", regno,
2267                         reg_type_str[reg->type],
2268                         reg_type_str[PTR_TO_STACK]);
2269                 return -EACCES;
2270         }
2271
2272         if (tnum_is_const(reg->var_off)) {
2273                 min_off = max_off = reg->var_off.value + reg->off;
2274                 err = __check_stack_boundary(env, regno, min_off, access_size,
2275                                              zero_size_allowed);
2276                 if (err)
2277                         return err;
2278         } else {
2279                 /* Variable offset is prohibited for unprivileged mode for
2280                  * simplicity since it requires corresponding support in
2281                  * Spectre masking for stack ALU.
2282                  * See also retrieve_ptr_limit().
2283                  */
2284                 if (!env->allow_ptr_leaks) {
2285                         char tn_buf[48];
2286
2287                         tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2288                         verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
2289                                 regno, tn_buf);
2290                         return -EACCES;
2291                 }
2292                 /* Only initialized buffer on stack is allowed to be accessed
2293                  * with variable offset. With uninitialized buffer it's hard to
2294                  * guarantee that whole memory is marked as initialized on
2295                  * helper return since specific bounds are unknown what may
2296                  * cause uninitialized stack leaking.
2297                  */
2298                 if (meta && meta->raw_mode)
2299                         meta = NULL;
2300
2301                 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
2302                     reg->smax_value <= -BPF_MAX_VAR_OFF) {
2303                         verbose(env, "R%d unbounded indirect variable offset stack access\n",
2304                                 regno);
2305                         return -EACCES;
2306                 }
2307                 min_off = reg->smin_value + reg->off;
2308                 max_off = reg->smax_value + reg->off;
2309                 err = __check_stack_boundary(env, regno, min_off, access_size,
2310                                              zero_size_allowed);
2311                 if (err) {
2312                         verbose(env, "R%d min value is outside of stack bound\n",
2313                                 regno);
2314                         return err;
2315                 }
2316                 err = __check_stack_boundary(env, regno, max_off, access_size,
2317                                              zero_size_allowed);
2318                 if (err) {
2319                         verbose(env, "R%d max value is outside of stack bound\n",
2320                                 regno);
2321                         return err;
2322                 }
2323         }
2324
2325         if (meta && meta->raw_mode) {
2326                 meta->access_size = access_size;
2327                 meta->regno = regno;
2328                 return 0;
2329         }
2330
2331         for (i = min_off; i < max_off + access_size; i++) {
2332                 u8 *stype;
2333
2334                 slot = -i - 1;
2335                 spi = slot / BPF_REG_SIZE;
2336                 if (state->allocated_stack <= slot)
2337                         goto err;
2338                 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2339                 if (*stype == STACK_MISC)
2340                         goto mark;
2341                 if (*stype == STACK_ZERO) {
2342                         /* helper can write anything into the stack */
2343                         *stype = STACK_MISC;
2344                         goto mark;
2345                 }
2346 err:
2347                 if (tnum_is_const(reg->var_off)) {
2348                         verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
2349                                 min_off, i - min_off, access_size);
2350                 } else {
2351                         char tn_buf[48];
2352
2353                         tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2354                         verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
2355                                 tn_buf, i - min_off, access_size);
2356                 }
2357                 return -EACCES;
2358 mark:
2359                 /* reading any byte out of 8-byte 'spill_slot' will cause
2360                  * the whole slot to be marked as 'read'
2361                  */
2362                 mark_reg_read(env, &state->stack[spi].spilled_ptr,
2363                               state->stack[spi].spilled_ptr.parent);
2364         }
2365         return update_stack_depth(env, state, min_off);
2366 }
2367
2368 static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
2369                                    int access_size, bool zero_size_allowed,
2370                                    struct bpf_call_arg_meta *meta)
2371 {
2372         struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2373
2374         switch (reg->type) {
2375         case PTR_TO_PACKET:
2376         case PTR_TO_PACKET_META:
2377                 return check_packet_access(env, regno, reg->off, access_size,
2378                                            zero_size_allowed);
2379         case PTR_TO_MAP_VALUE:
2380                 if (check_map_access_type(env, regno, reg->off, access_size,
2381                                           meta && meta->raw_mode ? BPF_WRITE :
2382                                           BPF_READ))
2383                         return -EACCES;
2384                 return check_map_access(env, regno, reg->off, access_size,
2385                                         zero_size_allowed);
2386         default: /* scalar_value|ptr_to_stack or invalid ptr */
2387                 return check_stack_boundary(env, regno, access_size,
2388                                             zero_size_allowed, meta);
2389         }
2390 }
2391
2392 /* Implementation details:
2393  * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL
2394  * Two bpf_map_lookups (even with the same key) will have different reg->id.
2395  * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after
2396  * value_or_null->value transition, since the verifier only cares about
2397  * the range of access to valid map value pointer and doesn't care about actual
2398  * address of the map element.
2399  * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps
2400  * reg->id > 0 after value_or_null->value transition. By doing so
2401  * two bpf_map_lookups will be considered two different pointers that
2402  * point to different bpf_spin_locks.
2403  * The verifier allows taking only one bpf_spin_lock at a time to avoid
2404  * dead-locks.
2405  * Since only one bpf_spin_lock is allowed the checks are simpler than
2406  * reg_is_refcounted() logic. The verifier needs to remember only
2407  * one spin_lock instead of array of acquired_refs.
2408  * cur_state->active_spin_lock remembers which map value element got locked
2409  * and clears it after bpf_spin_unlock.
2410  */
2411 static int process_spin_lock(struct bpf_verifier_env *env, int regno,
2412                              bool is_lock)
2413 {
2414         struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2415         struct bpf_verifier_state *cur = env->cur_state;
2416         bool is_const = tnum_is_const(reg->var_off);
2417         struct bpf_map *map = reg->map_ptr;
2418         u64 val = reg->var_off.value;
2419
2420         if (reg->type != PTR_TO_MAP_VALUE) {
2421                 verbose(env, "R%d is not a pointer to map_value\n", regno);
2422                 return -EINVAL;
2423         }
2424         if (!is_const) {
2425                 verbose(env,
2426                         "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
2427                         regno);
2428                 return -EINVAL;
2429         }
2430         if (!map->btf) {
2431                 verbose(env,
2432                         "map '%s' has to have BTF in order to use bpf_spin_lock\n",
2433                         map->name);
2434                 return -EINVAL;
2435         }
2436         if (!map_value_has_spin_lock(map)) {
2437                 if (map->spin_lock_off == -E2BIG)
2438                         verbose(env,
2439                                 "map '%s' has more than one 'struct bpf_spin_lock'\n",
2440                                 map->name);
2441                 else if (map->spin_lock_off == -ENOENT)
2442                         verbose(env,
2443                                 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
2444                                 map->name);
2445                 else
2446                         verbose(env,
2447                                 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
2448                                 map->name);
2449                 return -EINVAL;
2450         }
2451         if (map->spin_lock_off != val + reg->off) {
2452                 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
2453                         val + reg->off);
2454                 return -EINVAL;
2455         }
2456         if (is_lock) {
2457                 if (cur->active_spin_lock) {
2458                         verbose(env,
2459                                 "Locking two bpf_spin_locks are not allowed\n");
2460                         return -EINVAL;
2461                 }
2462                 cur->active_spin_lock = reg->id;
2463         } else {
2464                 if (!cur->active_spin_lock) {
2465                         verbose(env, "bpf_spin_unlock without taking a lock\n");
2466                         return -EINVAL;
2467                 }
2468                 if (cur->active_spin_lock != reg->id) {
2469                         verbose(env, "bpf_spin_unlock of different lock\n");
2470                         return -EINVAL;
2471                 }
2472                 cur->active_spin_lock = 0;
2473         }
2474         return 0;
2475 }
2476
2477 static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
2478 {
2479         return type == ARG_PTR_TO_MEM ||
2480                type == ARG_PTR_TO_MEM_OR_NULL ||
2481                type == ARG_PTR_TO_UNINIT_MEM;
2482 }
2483
2484 static bool arg_type_is_mem_size(enum bpf_arg_type type)
2485 {
2486         return type == ARG_CONST_SIZE ||
2487                type == ARG_CONST_SIZE_OR_ZERO;
2488 }
2489
2490 static bool arg_type_is_int_ptr(enum bpf_arg_type type)
2491 {
2492         return type == ARG_PTR_TO_INT ||
2493                type == ARG_PTR_TO_LONG;
2494 }
2495
2496 static int int_ptr_type_to_size(enum bpf_arg_type type)
2497 {
2498         if (type == ARG_PTR_TO_INT)
2499                 return sizeof(u32);
2500         else if (type == ARG_PTR_TO_LONG)
2501                 return sizeof(u64);
2502
2503         return -EINVAL;
2504 }
2505
2506 static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
2507                           enum bpf_arg_type arg_type,
2508                           struct bpf_call_arg_meta *meta)
2509 {
2510         struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
2511         enum bpf_reg_type expected_type, type = reg->type;
2512         int err = 0;
2513
2514         if (arg_type == ARG_DONTCARE)
2515                 return 0;
2516
2517         err = check_reg_arg(env, regno, SRC_OP);
2518         if (err)
2519                 return err;
2520
2521         if (arg_type == ARG_ANYTHING) {
2522                 if (is_pointer_value(env, regno)) {
2523                         verbose(env, "R%d leaks addr into helper function\n",
2524                                 regno);
2525                         return -EACCES;
2526                 }
2527                 return 0;
2528         }
2529
2530         if (type_is_pkt_pointer(type) &&
2531             !may_access_direct_pkt_data(env, meta, BPF_READ)) {
2532                 verbose(env, "helper access to the packet is not allowed\n");
2533                 return -EACCES;
2534         }
2535
2536         if (arg_type == ARG_PTR_TO_MAP_KEY ||
2537             arg_type == ARG_PTR_TO_MAP_VALUE ||
2538             arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
2539             arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
2540                 expected_type = PTR_TO_STACK;
2541                 if (register_is_null(reg) &&
2542                     arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
2543                         /* final test in check_stack_boundary() */;
2544                 else if (!type_is_pkt_pointer(type) &&
2545                          type != PTR_TO_MAP_VALUE &&
2546                          type != expected_type)
2547                         goto err_type;
2548         } else if (arg_type == ARG_CONST_SIZE ||
2549                    arg_type == ARG_CONST_SIZE_OR_ZERO) {
2550                 expected_type = SCALAR_VALUE;
2551                 if (type != expected_type)
2552                         goto err_type;
2553         } else if (arg_type == ARG_CONST_MAP_PTR) {
2554                 expected_type = CONST_PTR_TO_MAP;
2555                 if (type != expected_type)
2556                         goto err_type;
2557         } else if (arg_type == ARG_PTR_TO_CTX) {
2558                 expected_type = PTR_TO_CTX;
2559                 if (type != expected_type)
2560                         goto err_type;
2561                 err = check_ctx_reg(env, reg, regno);
2562                 if (err < 0)
2563                         return err;
2564         } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
2565                 expected_type = PTR_TO_SOCK_COMMON;
2566                 /* Any sk pointer can be ARG_PTR_TO_SOCK_COMMON */
2567                 if (!type_is_sk_pointer(type))
2568                         goto err_type;
2569                 if (reg->ref_obj_id) {
2570                         if (meta->ref_obj_id) {
2571                                 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
2572                                         regno, reg->ref_obj_id,
2573                                         meta->ref_obj_id);
2574                                 return -EFAULT;
2575                         }
2576                         meta->ref_obj_id = reg->ref_obj_id;
2577                 }
2578         } else if (arg_type == ARG_PTR_TO_SOCKET) {
2579                 expected_type = PTR_TO_SOCKET;
2580                 if (type != expected_type)
2581                         goto err_type;
2582         } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
2583                 if (meta->func_id == BPF_FUNC_spin_lock) {
2584                         if (process_spin_lock(env, regno, true))
2585                                 return -EACCES;
2586                 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
2587                         if (process_spin_lock(env, regno, false))
2588                                 return -EACCES;
2589                 } else {
2590                         verbose(env, "verifier internal error\n");
2591                         return -EFAULT;
2592                 }
2593         } else if (arg_type_is_mem_ptr(arg_type)) {
2594                 expected_type = PTR_TO_STACK;
2595                 /* One exception here. In case function allows for NULL to be
2596                  * passed in as argument, it's a SCALAR_VALUE type. Final test
2597                  * happens during stack boundary checking.
2598                  */
2599                 if (register_is_null(reg) &&
2600                     arg_type == ARG_PTR_TO_MEM_OR_NULL)
2601                         /* final test in check_stack_boundary() */;
2602                 else if (!type_is_pkt_pointer(type) &&
2603                          type != PTR_TO_MAP_VALUE &&
2604                          type != expected_type)
2605                         goto err_type;
2606                 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
2607         } else if (arg_type_is_int_ptr(arg_type)) {
2608                 expected_type = PTR_TO_STACK;
2609                 if (!type_is_pkt_pointer(type) &&
2610                     type != PTR_TO_MAP_VALUE &&
2611                     type != expected_type)
2612                         goto err_type;
2613         } else {
2614                 verbose(env, "unsupported arg_type %d\n", arg_type);
2615                 return -EFAULT;
2616         }
2617
2618         if (arg_type == ARG_CONST_MAP_PTR) {
2619                 /* bpf_map_xxx(map_ptr) call: remember that map_ptr */
2620                 meta->map_ptr = reg->map_ptr;
2621         } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
2622                 /* bpf_map_xxx(..., map_ptr, ..., key) call:
2623                  * check that [key, key + map->key_size) are within
2624                  * stack limits and initialized
2625                  */
2626                 if (!meta->map_ptr) {
2627                         /* in function declaration map_ptr must come before
2628                          * map_key, so that it's verified and known before
2629                          * we have to check map_key here. Otherwise it means
2630                          * that kernel subsystem misconfigured verifier
2631                          */
2632                         verbose(env, "invalid map_ptr to access map->key\n");
2633                         return -EACCES;
2634                 }
2635                 err = check_helper_mem_access(env, regno,
2636                                               meta->map_ptr->key_size, false,
2637                                               NULL);
2638         } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
2639                    (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
2640                     !register_is_null(reg)) ||
2641                    arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
2642                 /* bpf_map_xxx(..., map_ptr, ..., value) call:
2643                  * check [value, value + map->value_size) validity
2644                  */
2645                 if (!meta->map_ptr) {
2646                         /* kernel subsystem misconfigured verifier */
2647                         verbose(env, "invalid map_ptr to access map->value\n");
2648                         return -EACCES;
2649                 }
2650                 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
2651                 err = check_helper_mem_access(env, regno,
2652                                               meta->map_ptr->value_size, false,
2653                                               meta);
2654         } else if (arg_type_is_mem_size(arg_type)) {
2655                 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
2656
2657                 /* remember the mem_size which may be used later
2658                  * to refine return values.
2659                  */
2660                 meta->msize_smax_value = reg->smax_value;
2661                 meta->msize_umax_value = reg->umax_value;
2662
2663                 /* The register is SCALAR_VALUE; the access check
2664                  * happens using its boundaries.
2665                  */
2666                 if (!tnum_is_const(reg->var_off))
2667                         /* For unprivileged variable accesses, disable raw
2668                          * mode so that the program is required to
2669                          * initialize all the memory that the helper could
2670                          * just partially fill up.
2671                          */
2672                         meta = NULL;
2673
2674                 if (reg->smin_value < 0) {
2675                         verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
2676                                 regno);
2677                         return -EACCES;
2678                 }
2679
2680                 if (reg->umin_value == 0) {
2681                         err = check_helper_mem_access(env, regno - 1, 0,
2682                                                       zero_size_allowed,
2683                                                       meta);
2684                         if (err)
2685                                 return err;
2686                 }
2687
2688                 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
2689                         verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
2690                                 regno);
2691                         return -EACCES;
2692                 }
2693                 err = check_helper_mem_access(env, regno - 1,
2694                                               reg->umax_value,
2695                                               zero_size_allowed, meta);
2696         } else if (arg_type_is_int_ptr(arg_type)) {
2697                 int size = int_ptr_type_to_size(arg_type);
2698
2699                 err = check_helper_mem_access(env, regno, size, false, meta);
2700                 if (err)
2701                         return err;
2702                 err = check_ptr_alignment(env, reg, 0, size, true);
2703         }
2704
2705         return err;
2706 err_type:
2707         verbose(env, "R%d type=%s expected=%s\n", regno,
2708                 reg_type_str[type], reg_type_str[expected_type]);
2709         return -EACCES;
2710 }
2711
2712 static int check_map_func_compatibility(struct bpf_verifier_env *env,
2713                                         struct bpf_map *map, int func_id)
2714 {
2715         if (!map)
2716                 return 0;
2717
2718         /* We need a two way check, first is from map perspective ... */
2719         switch (map->map_type) {
2720         case BPF_MAP_TYPE_PROG_ARRAY:
2721                 if (func_id != BPF_FUNC_tail_call)
2722                         goto error;
2723                 break;
2724         case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
2725                 if (func_id != BPF_FUNC_perf_event_read &&
2726                     func_id != BPF_FUNC_perf_event_output &&
2727                     func_id != BPF_FUNC_perf_event_read_value)
2728                         goto error;
2729                 break;
2730         case BPF_MAP_TYPE_STACK_TRACE:
2731                 if (func_id != BPF_FUNC_get_stackid)
2732                         goto error;
2733                 break;
2734         case BPF_MAP_TYPE_CGROUP_ARRAY:
2735                 if (func_id != BPF_FUNC_skb_under_cgroup &&
2736                     func_id != BPF_FUNC_current_task_under_cgroup)
2737                         goto error;
2738                 break;
2739         case BPF_MAP_TYPE_CGROUP_STORAGE:
2740         case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
2741                 if (func_id != BPF_FUNC_get_local_storage)
2742                         goto error;
2743                 break;
2744         /* devmap returns a pointer to a live net_device ifindex that we cannot
2745          * allow to be modified from bpf side. So do not allow lookup elements
2746          * for now.
2747          */
2748         case BPF_MAP_TYPE_DEVMAP:
2749                 if (func_id != BPF_FUNC_redirect_map)
2750                         goto error;
2751                 break;
2752         /* Restrict bpf side of cpumap and xskmap, open when use-cases
2753          * appear.
2754          */
2755         case BPF_MAP_TYPE_CPUMAP:
2756         case BPF_MAP_TYPE_XSKMAP:
2757                 if (func_id != BPF_FUNC_redirect_map)
2758                         goto error;
2759                 break;
2760         case BPF_MAP_TYPE_ARRAY_OF_MAPS:
2761         case BPF_MAP_TYPE_HASH_OF_MAPS:
2762                 if (func_id != BPF_FUNC_map_lookup_elem)
2763                         goto error;
2764                 break;
2765         case BPF_MAP_TYPE_SOCKMAP:
2766                 if (func_id != BPF_FUNC_sk_redirect_map &&
2767                     func_id != BPF_FUNC_sock_map_update &&
2768                     func_id != BPF_FUNC_map_delete_elem &&
2769                     func_id != BPF_FUNC_msg_redirect_map)
2770                         goto error;
2771                 break;
2772         case BPF_MAP_TYPE_SOCKHASH:
2773                 if (func_id != BPF_FUNC_sk_redirect_hash &&
2774                     func_id != BPF_FUNC_sock_hash_update &&
2775                     func_id != BPF_FUNC_map_delete_elem &&
2776                     func_id != BPF_FUNC_msg_redirect_hash)
2777                         goto error;
2778                 break;
2779         case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
2780                 if (func_id != BPF_FUNC_sk_select_reuseport)
2781                         goto error;
2782                 break;
2783         case BPF_MAP_TYPE_QUEUE:
2784         case BPF_MAP_TYPE_STACK:
2785                 if (func_id != BPF_FUNC_map_peek_elem &&
2786                     func_id != BPF_FUNC_map_pop_elem &&
2787                     func_id != BPF_FUNC_map_push_elem)
2788                         goto error;
2789                 break;
2790         case BPF_MAP_TYPE_SK_STORAGE:
2791                 if (func_id != BPF_FUNC_sk_storage_get &&
2792                     func_id != BPF_FUNC_sk_storage_delete)
2793                         goto error;
2794                 break;
2795         default:
2796                 break;
2797         }
2798
2799         /* ... and second from the function itself. */
2800         switch (func_id) {
2801         case BPF_FUNC_tail_call:
2802                 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
2803                         goto error;
2804                 if (env->subprog_cnt > 1) {
2805                         verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
2806                         return -EINVAL;
2807                 }
2808                 break;
2809         case BPF_FUNC_perf_event_read:
2810         case BPF_FUNC_perf_event_output:
2811         case BPF_FUNC_perf_event_read_value:
2812                 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
2813                         goto error;
2814                 break;
2815         case BPF_FUNC_get_stackid:
2816                 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
2817                         goto error;
2818                 break;
2819         case BPF_FUNC_current_task_under_cgroup:
2820         case BPF_FUNC_skb_under_cgroup:
2821                 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
2822                         goto error;
2823                 break;
2824         case BPF_FUNC_redirect_map:
2825                 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
2826                     map->map_type != BPF_MAP_TYPE_CPUMAP &&
2827                     map->map_type != BPF_MAP_TYPE_XSKMAP)
2828                         goto error;
2829                 break;
2830         case BPF_FUNC_sk_redirect_map:
2831         case BPF_FUNC_msg_redirect_map:
2832         case BPF_FUNC_sock_map_update:
2833                 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
2834                         goto error;
2835                 break;
2836         case BPF_FUNC_sk_redirect_hash:
2837         case BPF_FUNC_msg_redirect_hash:
2838         case BPF_FUNC_sock_hash_update:
2839                 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
2840                         goto error;
2841                 break;
2842         case BPF_FUNC_get_local_storage:
2843                 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
2844                     map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
2845                         goto error;
2846                 break;
2847         case BPF_FUNC_sk_select_reuseport:
2848                 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
2849                         goto error;
2850                 break;
2851         case BPF_FUNC_map_peek_elem:
2852         case BPF_FUNC_map_pop_elem:
2853         case BPF_FUNC_map_push_elem:
2854                 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
2855                     map->map_type != BPF_MAP_TYPE_STACK)
2856                         goto error;
2857                 break;
2858         case BPF_FUNC_sk_storage_get:
2859         case BPF_FUNC_sk_storage_delete:
2860                 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
2861                         goto error;
2862                 break;
2863         default:
2864                 break;
2865         }
2866
2867         return 0;
2868 error:
2869         verbose(env, "cannot pass map_type %d into func %s#%d\n",
2870                 map->map_type, func_id_name(func_id), func_id);
2871         return -EINVAL;
2872 }
2873
2874 static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
2875 {
2876         int count = 0;
2877
2878         if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
2879                 count++;
2880         if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
2881                 count++;
2882         if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
2883                 count++;
2884         if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
2885                 count++;
2886         if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
2887                 count++;
2888
2889         /* We only support one arg being in raw mode at the moment,
2890          * which is sufficient for the helper functions we have
2891          * right now.
2892          */
2893         return count <= 1;
2894 }
2895
2896 static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
2897                                     enum bpf_arg_type arg_next)
2898 {
2899         return (arg_type_is_mem_ptr(arg_curr) &&
2900                 !arg_type_is_mem_size(arg_next)) ||
2901                (!arg_type_is_mem_ptr(arg_curr) &&
2902                 arg_type_is_mem_size(arg_next));
2903 }
2904
2905 static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
2906 {
2907         /* bpf_xxx(..., buf, len) call will access 'len'
2908          * bytes from memory 'buf'. Both arg types need
2909          * to be paired, so make sure there's no buggy
2910          * helper function specification.
2911          */
2912         if (arg_type_is_mem_size(fn->arg1_type) ||
2913             arg_type_is_mem_ptr(fn->arg5_type)  ||
2914             check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
2915             check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
2916             check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
2917             check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
2918                 return false;
2919
2920         return true;
2921 }
2922
2923 static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
2924 {
2925         int count = 0;
2926
2927         if (arg_type_may_be_refcounted(fn->arg1_type))
2928                 count++;
2929         if (arg_type_may_be_refcounted(fn->arg2_type))
2930                 count++;
2931         if (arg_type_may_be_refcounted(fn->arg3_type))
2932                 count++;
2933         if (arg_type_may_be_refcounted(fn->arg4_type))
2934                 count++;
2935         if (arg_type_may_be_refcounted(fn->arg5_type))
2936                 count++;
2937
2938         /* A reference acquiring function cannot acquire
2939          * another refcounted ptr.
2940          */
2941         if (is_acquire_function(func_id) && count)
2942                 return false;
2943
2944         /* We only support one arg being unreferenced at the moment,
2945          * which is sufficient for the helper functions we have right now.
2946          */
2947         return count <= 1;
2948 }
2949
2950 static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
2951 {
2952         return check_raw_mode_ok(fn) &&
2953                check_arg_pair_ok(fn) &&
2954                check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
2955 }
2956
2957 /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
2958  * are now invalid, so turn them into unknown SCALAR_VALUE.
2959  */
2960 static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
2961                                      struct bpf_func_state *state)
2962 {
2963         struct bpf_reg_state *regs = state->regs, *reg;
2964         int i;
2965
2966         for (i = 0; i < MAX_BPF_REG; i++)
2967                 if (reg_is_pkt_pointer_any(&regs[i]))
2968                         mark_reg_unknown(env, regs, i);
2969
2970         bpf_for_each_spilled_reg(i, state, reg) {
2971                 if (!reg)
2972                         continue;
2973                 if (reg_is_pkt_pointer_any(reg))
2974                         __mark_reg_unknown(reg);
2975         }
2976 }
2977
2978 static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
2979 {
2980         struct bpf_verifier_state *vstate = env->cur_state;
2981         int i;
2982
2983         for (i = 0; i <= vstate->curframe; i++)
2984                 __clear_all_pkt_pointers(env, vstate->frame[i]);
2985 }
2986
2987 static void release_reg_references(struct bpf_verifier_env *env,
2988                                    struct bpf_func_state *state,
2989                                    int ref_obj_id)
2990 {
2991         struct bpf_reg_state *regs = state->regs, *reg;
2992         int i;
2993
2994         for (i = 0; i < MAX_BPF_REG; i++)
2995                 if (regs[i].ref_obj_id == ref_obj_id)
2996                         mark_reg_unknown(env, regs, i);
2997
2998         bpf_for_each_spilled_reg(i, state, reg) {
2999                 if (!reg)
3000                         continue;
3001                 if (reg->ref_obj_id == ref_obj_id)
3002                         __mark_reg_unknown(reg);
3003         }
3004 }
3005
3006 /* The pointer with the specified id has released its reference to kernel
3007  * resources. Identify all copies of the same pointer and clear the reference.
3008  */
3009 static int release_reference(struct bpf_verifier_env *env,
3010                              int ref_obj_id)
3011 {
3012         struct bpf_verifier_state *vstate = env->cur_state;
3013         int err;
3014         int i;
3015
3016         err = release_reference_state(cur_func(env), ref_obj_id);
3017         if (err)
3018                 return err;
3019
3020         for (i = 0; i <= vstate->curframe; i++)
3021                 release_reg_references(env, vstate->frame[i], ref_obj_id);
3022
3023         return 0;
3024 }
3025
3026 static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
3027                            int *insn_idx)
3028 {
3029         struct bpf_verifier_state *state = env->cur_state;
3030         struct bpf_func_state *caller, *callee;
3031         int i, err, subprog, target_insn;
3032
3033         if (state->curframe + 1 >= MAX_CALL_FRAMES) {
3034                 verbose(env, "the call stack of %d frames is too deep\n",
3035                         state->curframe + 2);
3036                 return -E2BIG;
3037         }
3038
3039         target_insn = *insn_idx + insn->imm;
3040         subprog = find_subprog(env, target_insn + 1);
3041         if (subprog < 0) {
3042                 verbose(env, "verifier bug. No program starts at insn %d\n",
3043                         target_insn + 1);
3044                 return -EFAULT;
3045         }
3046
3047         caller = state->frame[state->curframe];
3048         if (state->frame[state->curframe + 1]) {
3049                 verbose(env, "verifier bug. Frame %d already allocated\n",
3050                         state->curframe + 1);
3051                 return -EFAULT;
3052         }
3053
3054         callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3055         if (!callee)
3056                 return -ENOMEM;
3057         state->frame[state->curframe + 1] = callee;
3058
3059         /* callee cannot access r0, r6 - r9 for reading and has to write
3060          * into its own stack before reading from it.
3061          * callee can read/write into caller's stack
3062          */
3063         init_func_state(env, callee,
3064                         /* remember the callsite, it will be used by bpf_exit */
3065                         *insn_idx /* callsite */,
3066                         state->curframe + 1 /* frameno within this callchain */,
3067                         subprog /* subprog number within this prog */);
3068
3069         /* Transfer references to the callee */
3070         err = transfer_reference_state(callee, caller);
3071         if (err)
3072                 return err;
3073
3074         /* copy r1 - r5 args that callee can access.  The copy includes parent
3075          * pointers, which connects us up to the liveness chain
3076          */
3077         for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3078                 callee->regs[i] = caller->regs[i];
3079
3080         /* after the call registers r0 - r5 were scratched */
3081         for (i = 0; i < CALLER_SAVED_REGS; i++) {
3082                 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3083                 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3084         }
3085
3086         /* only increment it after check_reg_arg() finished */
3087         state->curframe++;
3088
3089         /* and go analyze first insn of the callee */
3090         *insn_idx = target_insn;
3091
3092         if (env->log.level & BPF_LOG_LEVEL) {
3093                 verbose(env, "caller:\n");
3094                 print_verifier_state(env, caller);
3095                 verbose(env, "callee:\n");
3096                 print_verifier_state(env, callee);
3097         }
3098         return 0;
3099 }
3100
3101 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3102 {
3103         struct bpf_verifier_state *state = env->cur_state;
3104         struct bpf_func_state *caller, *callee;
3105         struct bpf_reg_state *r0;
3106         int err;
3107
3108         callee = state->frame[state->curframe];
3109         r0 = &callee->regs[BPF_REG_0];
3110         if (r0->type == PTR_TO_STACK) {
3111                 /* technically it's ok to return caller's stack pointer
3112                  * (or caller's caller's pointer) back to the caller,
3113                  * since these pointers are valid. Only current stack
3114                  * pointer will be invalid as soon as function exits,
3115                  * but let's be conservative
3116                  */
3117                 verbose(env, "cannot return stack pointer to the caller\n");
3118                 return -EINVAL;
3119         }
3120
3121         state->curframe--;
3122         caller = state->frame[state->curframe];
3123         /* return to the caller whatever r0 had in the callee */
3124         caller->regs[BPF_REG_0] = *r0;
3125
3126         /* Transfer references to the caller */
3127         err = transfer_reference_state(caller, callee);
3128         if (err)
3129                 return err;
3130
3131         *insn_idx = callee->callsite + 1;
3132         if (env->log.level & BPF_LOG_LEVEL) {
3133                 verbose(env, "returning from callee:\n");
3134                 print_verifier_state(env, callee);
3135                 verbose(env, "to caller at %d:\n", *insn_idx);
3136                 print_verifier_state(env, caller);
3137         }
3138         /* clear everything in the callee */
3139         free_func_state(callee);
3140         state->frame[state->curframe + 1] = NULL;
3141         return 0;
3142 }
3143
3144 static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type,
3145                                    int func_id,
3146                                    struct bpf_call_arg_meta *meta)
3147 {
3148         struct bpf_reg_state *ret_reg = &regs[BPF_REG_0];
3149
3150         if (ret_type != RET_INTEGER ||
3151             (func_id != BPF_FUNC_get_stack &&
3152              func_id != BPF_FUNC_probe_read_str))
3153                 return;
3154
3155         ret_reg->smax_value = meta->msize_smax_value;
3156         ret_reg->umax_value = meta->msize_umax_value;
3157         __reg_deduce_bounds(ret_reg);
3158         __reg_bound_offset(ret_reg);
3159 }
3160
3161 static int
3162 record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3163                 int func_id, int insn_idx)
3164 {
3165         struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
3166         struct bpf_map *map = meta->map_ptr;
3167
3168         if (func_id != BPF_FUNC_tail_call &&
3169             func_id != BPF_FUNC_map_lookup_elem &&
3170             func_id != BPF_FUNC_map_update_elem &&
3171             func_id != BPF_FUNC_map_delete_elem &&
3172             func_id != BPF_FUNC_map_push_elem &&
3173             func_id != BPF_FUNC_map_pop_elem &&
3174             func_id != BPF_FUNC_map_peek_elem)
3175                 return 0;
3176
3177         if (map == NULL) {
3178                 verbose(env, "kernel subsystem misconfigured verifier\n");
3179                 return -EINVAL;
3180         }
3181
3182         /* In case of read-only, some additional restrictions
3183          * need to be applied in order to prevent altering the
3184          * state of the map from program side.
3185          */
3186         if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3187             (func_id == BPF_FUNC_map_delete_elem ||
3188              func_id == BPF_FUNC_map_update_elem ||
3189              func_id == BPF_FUNC_map_push_elem ||
3190              func_id == BPF_FUNC_map_pop_elem)) {
3191                 verbose(env, "write into map forbidden\n");
3192                 return -EACCES;
3193         }
3194
3195         if (!BPF_MAP_PTR(aux->map_state))
3196                 bpf_map_ptr_store(aux, meta->map_ptr,
3197                                   meta->map_ptr->unpriv_array);
3198         else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3199                 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3200                                   meta->map_ptr->unpriv_array);
3201         return 0;
3202 }
3203
3204 static int check_reference_leak(struct bpf_verifier_env *env)
3205 {
3206         struct bpf_func_state *state = cur_func(env);
3207         int i;
3208
3209         for (i = 0; i < state->acquired_refs; i++) {
3210                 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3211                         state->refs[i].id, state->refs[i].insn_idx);
3212         }
3213         return state->acquired_refs ? -EINVAL : 0;
3214 }
3215
3216 static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
3217 {
3218         const struct bpf_func_proto *fn = NULL;
3219         struct bpf_reg_state *regs;
3220         struct bpf_call_arg_meta meta;
3221         bool changes_data;
3222         int i, err;
3223
3224         /* find function prototype */
3225         if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
3226                 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3227                         func_id);
3228                 return -EINVAL;
3229         }
3230
3231         if (env->ops->get_func_proto)
3232                 fn = env->ops->get_func_proto(func_id, env->prog);
3233         if (!fn) {
3234                 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3235                         func_id);
3236                 return -EINVAL;
3237         }
3238
3239         /* eBPF programs must be GPL compatible to use GPL-ed functions */
3240         if (!env->prog->gpl_compatible && fn->gpl_only) {
3241                 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
3242                 return -EINVAL;
3243         }
3244
3245         /* With LD_ABS/IND some JITs save/restore skb from r1. */
3246         changes_data = bpf_helper_changes_pkt_data(fn->func);
3247         if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
3248                 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
3249                         func_id_name(func_id), func_id);
3250                 return -EINVAL;
3251         }
3252
3253         memset(&meta, 0, sizeof(meta));
3254         meta.pkt_access = fn->pkt_access;
3255
3256         err = check_func_proto(fn, func_id);
3257         if (err) {
3258                 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
3259                         func_id_name(func_id), func_id);
3260                 return err;
3261         }
3262
3263         meta.func_id = func_id;
3264         /* check args */
3265         err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
3266         if (err)
3267                 return err;
3268         err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
3269         if (err)
3270                 return err;
3271         err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
3272         if (err)
3273                 return err;
3274         err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
3275         if (err)
3276                 return err;
3277         err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
3278         if (err)
3279                 return err;
3280
3281         err = record_func_map(env, &meta, func_id, insn_idx);
3282         if (err)
3283                 return err;
3284
3285         /* Mark slots with STACK_MISC in case of raw mode, stack offset
3286          * is inferred from register state.
3287          */
3288         for (i = 0; i < meta.access_size; i++) {
3289                 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
3290                                        BPF_WRITE, -1, false);
3291                 if (err)
3292                         return err;
3293         }
3294
3295         if (func_id == BPF_FUNC_tail_call) {
3296                 err = check_reference_leak(env);
3297                 if (err) {
3298                         verbose(env, "tail_call would lead to reference leak\n");
3299                         return err;
3300                 }
3301         } else if (is_release_function(func_id)) {
3302                 err = release_reference(env, meta.ref_obj_id);
3303                 if (err) {
3304                         verbose(env, "func %s#%d reference has not been acquired before\n",
3305                                 func_id_name(func_id), func_id);
3306                         return err;
3307                 }
3308         }
3309
3310         regs = cur_regs(env);
3311
3312         /* check that flags argument in get_local_storage(map, flags) is 0,
3313          * this is required because get_local_storage() can't return an error.
3314          */
3315         if (func_id == BPF_FUNC_get_local_storage &&
3316             !register_is_null(&regs[BPF_REG_2])) {
3317                 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
3318                 return -EINVAL;
3319         }
3320
3321         /* reset caller saved regs */
3322         for (i = 0; i < CALLER_SAVED_REGS; i++) {
3323                 mark_reg_not_init(env, regs, caller_saved[i]);
3324                 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3325         }
3326
3327         /* update return register (already marked as written above) */
3328         if (fn->ret_type == RET_INTEGER) {
3329                 /* sets type to SCALAR_VALUE */
3330                 mark_reg_unknown(env, regs, BPF_REG_0);
3331         } else if (fn->ret_type == RET_VOID) {
3332                 regs[BPF_REG_0].type = NOT_INIT;
3333         } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
3334                    fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3335                 /* There is no offset yet applied, variable or fixed */
3336                 mark_reg_known_zero(env, regs, BPF_REG_0);
3337                 /* remember map_ptr, so that check_map_access()
3338                  * can check 'value_size' boundary of memory access
3339                  * to map element returned from bpf_map_lookup_elem()
3340                  */
3341                 if (meta.map_ptr == NULL) {
3342                         verbose(env,
3343                                 "kernel subsystem misconfigured verifier\n");
3344                         return -EINVAL;
3345                 }
3346                 regs[BPF_REG_0].map_ptr = meta.map_ptr;
3347                 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
3348                         regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
3349                         if (map_value_has_spin_lock(meta.map_ptr))
3350                                 regs[BPF_REG_0].id = ++env->id_gen;
3351                 } else {
3352                         regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
3353                         regs[BPF_REG_0].id = ++env->id_gen;
3354                 }
3355         } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
3356                 mark_reg_known_zero(env, regs, BPF_REG_0);
3357                 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
3358                 regs[BPF_REG_0].id = ++env->id_gen;
3359         } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
3360                 mark_reg_known_zero(env, regs, BPF_REG_0);
3361                 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
3362                 regs[BPF_REG_0].id = ++env->id_gen;
3363         } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
3364                 mark_reg_known_zero(env, regs, BPF_REG_0);
3365                 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
3366                 regs[BPF_REG_0].id = ++env->id_gen;
3367         } else {
3368                 verbose(env, "unknown return type %d of func %s#%d\n",
3369                         fn->ret_type, func_id_name(func_id), func_id);
3370                 return -EINVAL;
3371         }
3372
3373         if (is_ptr_cast_function(func_id)) {
3374                 /* For release_reference() */
3375                 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
3376         } else if (is_acquire_function(func_id)) {
3377                 int id = acquire_reference_state(env, insn_idx);
3378
3379                 if (id < 0)
3380                         return id;
3381                 /* For mark_ptr_or_null_reg() */
3382                 regs[BPF_REG_0].id = id;
3383                 /* For release_reference() */
3384                 regs[BPF_REG_0].ref_obj_id = id;
3385         }
3386
3387         do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
3388
3389         err = check_map_func_compatibility(env, meta.map_ptr, func_id);
3390         if (err)
3391                 return err;
3392
3393         if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
3394                 const char *err_str;
3395
3396 #ifdef CONFIG_PERF_EVENTS
3397                 err = get_callchain_buffers(sysctl_perf_event_max_stack);
3398                 err_str = "cannot get callchain buffer for func %s#%d\n";
3399 #else
3400                 err = -ENOTSUPP;
3401                 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
3402 #endif
3403                 if (err) {
3404                         verbose(env, err_str, func_id_name(func_id), func_id);
3405                         return err;
3406                 }
3407
3408                 env->prog->has_callchain_buf = true;
3409         }
3410
3411         if (changes_data)
3412                 clear_all_pkt_pointers(env);
3413         return 0;
3414 }
3415
3416 static bool signed_add_overflows(s64 a, s64 b)
3417 {
3418         /* Do the add in u64, where overflow is well-defined */
3419         s64 res = (s64)((u64)a + (u64)b);
3420
3421         if (b < 0)
3422                 return res > a;
3423         return res < a;
3424 }
3425
3426 static bool signed_sub_overflows(s64 a, s64 b)
3427 {
3428         /* Do the sub in u64, where overflow is well-defined */
3429         s64 res = (s64)((u64)a - (u64)b);
3430
3431         if (b < 0)
3432                 return res < a;
3433         return res > a;
3434 }
3435
3436 static bool check_reg_sane_offset(struct bpf_verifier_env *env,
3437                                   const struct bpf_reg_state *reg,
3438                                   enum bpf_reg_type type)
3439 {
3440         bool known = tnum_is_const(reg->var_off);
3441         s64 val = reg->var_off.value;
3442         s64 smin = reg->smin_value;
3443
3444         if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
3445                 verbose(env, "math between %s pointer and %lld is not allowed\n",
3446                         reg_type_str[type], val);
3447                 return false;
3448         }
3449
3450         if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
3451                 verbose(env, "%s pointer offset %d is not allowed\n",
3452                         reg_type_str[type], reg->off);
3453                 return false;
3454         }
3455
3456         if (smin == S64_MIN) {
3457                 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
3458                         reg_type_str[type]);
3459                 return false;
3460         }
3461
3462         if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
3463                 verbose(env, "value %lld makes %s pointer be out of bounds\n",
3464                         smin, reg_type_str[type]);
3465                 return false;
3466         }
3467
3468         return true;
3469 }
3470
3471 static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
3472 {
3473         return &env->insn_aux_data[env->insn_idx];
3474 }
3475
3476 static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
3477                               u32 *ptr_limit, u8 opcode, bool off_is_neg)
3478 {
3479         bool mask_to_left = (opcode == BPF_ADD &&  off_is_neg) ||
3480                             (opcode == BPF_SUB && !off_is_neg);
3481         u32 off;
3482
3483         switch (ptr_reg->type) {
3484         case PTR_TO_STACK:
3485                 /* Indirect variable offset stack access is prohibited in
3486                  * unprivileged mode so it's not handled here.
3487                  */
3488                 off = ptr_reg->off + ptr_reg->var_off.value;
3489                 if (mask_to_left)
3490                         *ptr_limit = MAX_BPF_STACK + off;
3491                 else
3492                         *ptr_limit = -off;
3493                 return 0;
3494         case PTR_TO_MAP_VALUE:
3495                 if (mask_to_left) {
3496                         *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
3497                 } else {
3498                         off = ptr_reg->smin_value + ptr_reg->off;
3499                         *ptr_limit = ptr_reg->map_ptr->value_size - off;
3500                 }
3501                 return 0;
3502         default:
3503                 return -EINVAL;
3504         }
3505 }
3506
3507 static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
3508                                     const struct bpf_insn *insn)
3509 {
3510         return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
3511 }
3512
3513 static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
3514                                        u32 alu_state, u32 alu_limit)
3515 {
3516         /* If we arrived here from different branches with different
3517          * state or limits to sanitize, then this won't work.
3518          */
3519         if (aux->alu_state &&
3520             (aux->alu_state != alu_state ||
3521              aux->alu_limit != alu_limit))
3522                 return -EACCES;
3523
3524         /* Corresponding fixup done in fixup_bpf_calls(). */
3525         aux->alu_state = alu_state;
3526         aux->alu_limit = alu_limit;
3527         return 0;
3528 }
3529
3530 static int sanitize_val_alu(struct bpf_verifier_env *env,
3531                             struct bpf_insn *insn)
3532 {
3533         struct bpf_insn_aux_data *aux = cur_aux(env);
3534
3535         if (can_skip_alu_sanitation(env, insn))
3536                 return 0;
3537
3538         return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
3539 }
3540
3541 static int sanitize_ptr_alu(struct bpf_verifier_env *env,
3542                             struct bpf_insn *insn,
3543                             const struct bpf_reg_state *ptr_reg,
3544                             struct bpf_reg_state *dst_reg,
3545                             bool off_is_neg)
3546 {
3547         struct bpf_verifier_state *vstate = env->cur_state;
3548         struct bpf_insn_aux_data *aux = cur_aux(env);
3549         bool ptr_is_dst_reg = ptr_reg == dst_reg;
3550         u8 opcode = BPF_OP(insn->code);
3551         u32 alu_state, alu_limit;
3552         struct bpf_reg_state tmp;
3553         bool ret;
3554
3555         if (can_skip_alu_sanitation(env, insn))
3556                 return 0;
3557
3558         /* We already marked aux for masking from non-speculative
3559          * paths, thus we got here in the first place. We only care
3560          * to explore bad access from here.
3561          */
3562         if (vstate->speculative)
3563                 goto do_sim;
3564
3565         alu_state  = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
3566         alu_state |= ptr_is_dst_reg ?
3567                      BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
3568
3569         if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
3570                 return 0;
3571         if (update_alu_sanitation_state(aux, alu_state, alu_limit))
3572                 return -EACCES;
3573 do_sim:
3574         /* Simulate and find potential out-of-bounds access under
3575          * speculative execution from truncation as a result of
3576          * masking when off was not within expected range. If off
3577          * sits in dst, then we temporarily need to move ptr there
3578          * to simulate dst (== 0) +/-= ptr. Needed, for example,
3579          * for cases where we use K-based arithmetic in one direction
3580          * and truncated reg-based in the other in order to explore
3581          * bad access.
3582          */
3583         if (!ptr_is_dst_reg) {
3584                 tmp = *dst_reg;
3585                 *dst_reg = *ptr_reg;
3586         }
3587         ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
3588         if (!ptr_is_dst_reg && ret)
3589                 *dst_reg = tmp;
3590         return !ret ? -EFAULT : 0;
3591 }
3592
3593 /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
3594  * Caller should also handle BPF_MOV case separately.
3595  * If we return -EACCES, caller may want to try again treating pointer as a
3596  * scalar.  So we only emit a diagnostic if !env->allow_ptr_leaks.
3597  */
3598 static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
3599                                    struct bpf_insn *insn,
3600                                    const struct bpf_reg_state *ptr_reg,
3601                                    const struct bpf_reg_state *off_reg)
3602 {
3603         struct bpf_verifier_state *vstate = env->cur_state;
3604         struct bpf_func_state *state = vstate->frame[vstate->curframe];
3605         struct bpf_reg_state *regs = state->regs, *dst_reg;
3606         bool known = tnum_is_const(off_reg->var_off);
3607         s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
3608             smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
3609         u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
3610             umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
3611         u32 dst = insn->dst_reg, src = insn->src_reg;
3612         u8 opcode = BPF_OP(insn->code);
3613         int ret;
3614
3615         dst_reg = &regs[dst];
3616
3617         if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
3618             smin_val > smax_val || umin_val > umax_val) {
3619                 /* Taint dst register if offset had invalid bounds derived from
3620                  * e.g. dead branches.
3621                  */
3622                 __mark_reg_unknown(dst_reg);
3623                 return 0;
3624         }
3625
3626         if (BPF_CLASS(insn->code) != BPF_ALU64) {
3627                 /* 32-bit ALU ops on pointers produce (meaningless) scalars */
3628                 verbose(env,
3629                         "R%d 32-bit pointer arithmetic prohibited\n",
3630                         dst);
3631                 return -EACCES;
3632         }
3633
3634         switch (ptr_reg->type) {
3635         case PTR_TO_MAP_VALUE_OR_NULL:
3636                 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
3637                         dst, reg_type_str[ptr_reg->type]);
3638                 return -EACCES;
3639         case CONST_PTR_TO_MAP:
3640         case PTR_TO_PACKET_END:
3641         case PTR_TO_SOCKET:
3642         case PTR_TO_SOCKET_OR_NULL:
3643         case PTR_TO_SOCK_COMMON:
3644         case PTR_TO_SOCK_COMMON_OR_NULL:
3645         case PTR_TO_TCP_SOCK:
3646         case PTR_TO_TCP_SOCK_OR_NULL:
3647                 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
3648                         dst, reg_type_str[ptr_reg->type]);
3649                 return -EACCES;
3650         case PTR_TO_MAP_VALUE:
3651                 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
3652                         verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
3653                                 off_reg == dst_reg ? dst : src);
3654                         return -EACCES;
3655                 }
3656                 /* fall-through */
3657         default:
3658                 break;
3659         }
3660
3661         /* In case of 'scalar += pointer', dst_reg inherits pointer type and id.
3662          * The id may be overwritten later if we create a new variable offset.
3663          */
3664         dst_reg->type = ptr_reg->type;
3665         dst_reg->id = ptr_reg->id;
3666
3667         if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
3668             !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
3669                 return -EINVAL;
3670
3671         switch (opcode) {
3672         case BPF_ADD:
3673                 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3674                 if (ret < 0) {
3675                         verbose(env, "R%d tried to add from different maps or paths\n", dst);
3676                         return ret;
3677                 }
3678                 /* We can take a fixed offset as long as it doesn't overflow
3679                  * the s32 'off' field
3680                  */
3681                 if (known && (ptr_reg->off + smin_val ==
3682                               (s64)(s32)(ptr_reg->off + smin_val))) {
3683                         /* pointer += K.  Accumulate it into fixed offset */
3684                         dst_reg->smin_value = smin_ptr;
3685                         dst_reg->smax_value = smax_ptr;
3686                         dst_reg->umin_value = umin_ptr;
3687                         dst_reg->umax_value = umax_ptr;
3688                         dst_reg->var_off = ptr_reg->var_off;
3689                         dst_reg->off = ptr_reg->off + smin_val;
3690                         dst_reg->raw = ptr_reg->raw;
3691                         break;
3692                 }
3693                 /* A new variable offset is created.  Note that off_reg->off
3694                  * == 0, since it's a scalar.
3695                  * dst_reg gets the pointer type and since some positive
3696                  * integer value was added to the pointer, give it a new 'id'
3697                  * if it's a PTR_TO_PACKET.
3698                  * this creates a new 'base' pointer, off_reg (variable) gets
3699                  * added into the variable offset, and we copy the fixed offset
3700                  * from ptr_reg.
3701                  */
3702                 if (signed_add_overflows(smin_ptr, smin_val) ||
3703                     signed_add_overflows(smax_ptr, smax_val)) {
3704                         dst_reg->smin_value = S64_MIN;
3705                         dst_reg->smax_value = S64_MAX;
3706                 } else {
3707                         dst_reg->smin_value = smin_ptr + smin_val;
3708                         dst_reg->smax_value = smax_ptr + smax_val;
3709                 }
3710                 if (umin_ptr + umin_val < umin_ptr ||
3711                     umax_ptr + umax_val < umax_ptr) {
3712                         dst_reg->umin_value = 0;
3713                         dst_reg->umax_value = U64_MAX;
3714                 } else {
3715                         dst_reg->umin_value = umin_ptr + umin_val;
3716                         dst_reg->umax_value = umax_ptr + umax_val;
3717                 }
3718                 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
3719                 dst_reg->off = ptr_reg->off;
3720                 dst_reg->raw = ptr_reg->raw;
3721                 if (reg_is_pkt_pointer(ptr_reg)) {
3722                         dst_reg->id = ++env->id_gen;
3723                         /* something was added to pkt_ptr, set range to zero */
3724                         dst_reg->raw = 0;
3725                 }
3726                 break;
3727         case BPF_SUB:
3728                 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
3729                 if (ret < 0) {
3730                         verbose(env, "R%d tried to sub from different maps or paths\n", dst);
3731                         return ret;
3732                 }
3733                 if (dst_reg == off_reg) {
3734                         /* scalar -= pointer.  Creates an unknown scalar */
3735                         verbose(env, "R%d tried to subtract pointer from scalar\n",
3736                                 dst);
3737                         return -EACCES;
3738                 }
3739                 /* We don't allow subtraction from FP, because (according to
3740                  * test_verifier.c test "invalid fp arithmetic", JITs might not
3741                  * be able to deal with it.
3742                  */
3743                 if (ptr_reg->type == PTR_TO_STACK) {
3744                         verbose(env, "R%d subtraction from stack pointer prohibited\n",
3745                                 dst);
3746                         return -EACCES;
3747                 }
3748                 if (known && (ptr_reg->off - smin_val ==
3749                               (s64)(s32)(ptr_reg->off - smin_val))) {
3750                         /* pointer -= K.  Subtract it from fixed offset */
3751                         dst_reg->smin_value = smin_ptr;
3752                         dst_reg->smax_value = smax_ptr;
3753                         dst_reg->umin_value = umin_ptr;
3754                         dst_reg->umax_value = umax_ptr;
3755                         dst_reg->var_off = ptr_reg->var_off;
3756                         dst_reg->id = ptr_reg->id;
3757                         dst_reg->off = ptr_reg->off - smin_val;
3758                         dst_reg->raw = ptr_reg->raw;
3759                         break;
3760                 }
3761                 /* A new variable offset is created.  If the subtrahend is known
3762                  * nonnegative, then any reg->range we had before is still good.
3763                  */
3764                 if (signed_sub_overflows(smin_ptr, smax_val) ||
3765                     signed_sub_overflows(smax_ptr, smin_val)) {
3766                         /* Overflow possible, we know nothing */
3767                         dst_reg->smin_value = S64_MIN;
3768                         dst_reg->smax_value = S64_MAX;
3769                 } else {
3770                         dst_reg->smin_value = smin_ptr - smax_val;
3771                         dst_reg->smax_value = smax_ptr - smin_val;
3772                 }
3773                 if (umin_ptr < umax_val) {
3774                         /* Overflow possible, we know nothing */
3775                         dst_reg->umin_value = 0;
3776                         dst_reg->umax_value = U64_MAX;
3777                 } else {
3778                         /* Cannot overflow (as long as bounds are consistent) */
3779                         dst_reg->umin_value = umin_ptr - umax_val;
3780                         dst_reg->umax_value = umax_ptr - umin_val;
3781                 }
3782                 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
3783                 dst_reg->off = ptr_reg->off;
3784                 dst_reg->raw = ptr_reg->raw;
3785                 if (reg_is_pkt_pointer(ptr_reg)) {
3786                         dst_reg->id = ++env->id_gen;
3787                         /* something was added to pkt_ptr, set range to zero */
3788                         if (smin_val < 0)
3789                                 dst_reg->raw = 0;
3790                 }
3791                 break;
3792         case BPF_AND:
3793         case BPF_OR:
3794         case BPF_XOR:
3795                 /* bitwise ops on pointers are troublesome, prohibit. */
3796                 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
3797                         dst, bpf_alu_string[opcode >> 4]);
3798                 return -EACCES;
3799         default:
3800                 /* other operators (e.g. MUL,LSH) produce non-pointer results */
3801                 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
3802                         dst, bpf_alu_string[opcode >> 4]);
3803                 return -EACCES;
3804         }
3805
3806         if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
3807                 return -EINVAL;
3808
3809         __update_reg_bounds(dst_reg);
3810         __reg_deduce_bounds(dst_reg);
3811         __reg_bound_offset(dst_reg);
3812
3813         /* For unprivileged we require that resulting offset must be in bounds
3814          * in order to be able to sanitize access later on.
3815          */
3816         if (!env->allow_ptr_leaks) {
3817                 if (dst_reg->type == PTR_TO_MAP_VALUE &&
3818                     check_map_access(env, dst, dst_reg->off, 1, false)) {
3819                         verbose(env, "R%d pointer arithmetic of map value goes out of range, "
3820                                 "prohibited for !root\n", dst);
3821                         return -EACCES;
3822                 } else if (dst_reg->type == PTR_TO_STACK &&
3823                            check_stack_access(env, dst_reg, dst_reg->off +
3824                                               dst_reg->var_off.value, 1)) {
3825                         verbose(env, "R%d stack pointer arithmetic goes out of range, "
3826                                 "prohibited for !root\n", dst);
3827                         return -EACCES;
3828                 }
3829         }
3830
3831         return 0;
3832 }
3833
3834 /* WARNING: This function does calculations on 64-bit values, but the actual
3835  * execution may occur on 32-bit values. Therefore, things like bitshifts
3836  * need extra checks in the 32-bit case.
3837  */
3838 static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
3839                                       struct bpf_insn *insn,
3840                                       struct bpf_reg_state *dst_reg,
3841                                       struct bpf_reg_state src_reg)
3842 {
3843         struct bpf_reg_state *regs = cur_regs(env);
3844         u8 opcode = BPF_OP(insn->code);
3845         bool src_known, dst_known;
3846         s64 smin_val, smax_val;
3847         u64 umin_val, umax_val;
3848         u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
3849         u32 dst = insn->dst_reg;
3850         int ret;
3851
3852         if (insn_bitness == 32) {
3853                 /* Relevant for 32-bit RSH: Information can propagate towards
3854                  * LSB, so it isn't sufficient to only truncate the output to
3855                  * 32 bits.
3856                  */
3857                 coerce_reg_to_size(dst_reg, 4);
3858                 coerce_reg_to_size(&src_reg, 4);
3859         }
3860
3861         smin_val = src_reg.smin_value;
3862         smax_val = src_reg.smax_value;
3863         umin_val = src_reg.umin_value;
3864         umax_val = src_reg.umax_value;
3865         src_known = tnum_is_const(src_reg.var_off);
3866         dst_known = tnum_is_const(dst_reg->var_off);
3867
3868         if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
3869             smin_val > smax_val || umin_val > umax_val) {
3870                 /* Taint dst register if offset had invalid bounds derived from
3871                  * e.g. dead branches.
3872                  */
3873                 __mark_reg_unknown(dst_reg);
3874                 return 0;
3875         }
3876
3877         if (!src_known &&
3878             opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
3879                 __mark_reg_unknown(dst_reg);
3880                 return 0;
3881         }
3882
3883         switch (opcode) {
3884         case BPF_ADD:
3885                 ret = sanitize_val_alu(env, insn);
3886                 if (ret < 0) {
3887                         verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
3888                         return ret;
3889                 }
3890                 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
3891                     signed_add_overflows(dst_reg->smax_value, smax_val)) {
3892                         dst_reg->smin_value = S64_MIN;
3893                         dst_reg->smax_value = S64_MAX;
3894                 } else {
3895                         dst_reg->smin_value += smin_val;
3896                         dst_reg->smax_value += smax_val;
3897                 }
3898                 if (dst_reg->umin_value + umin_val < umin_val ||
3899                     dst_reg->umax_value + umax_val < umax_val) {
3900                         dst_reg->umin_value = 0;
3901                         dst_reg->umax_value = U64_MAX;
3902                 } else {
3903                         dst_reg->umin_value += umin_val;
3904                         dst_reg->umax_value += umax_val;
3905                 }
3906                 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
3907                 break;
3908         case BPF_SUB:
3909                 ret = sanitize_val_alu(env, insn);
3910                 if (ret < 0) {
3911                         verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
3912                         return ret;
3913                 }
3914                 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
3915                     signed_sub_overflows(dst_reg->smax_value, smin_val)) {
3916                         /* Overflow possible, we know nothing */
3917                         dst_reg->smin_value = S64_MIN;
3918                         dst_reg->smax_value = S64_MAX;
3919                 } else {
3920                         dst_reg->smin_value -= smax_val;
3921                         dst_reg->smax_value -= smin_val;
3922                 }
3923                 if (dst_reg->umin_value < umax_val) {
3924                         /* Overflow possible, we know nothing */
3925                         dst_reg->umin_value = 0;
3926                         dst_reg->umax_value = U64_MAX;
3927                 } else {
3928                         /* Cannot overflow (as long as bounds are consistent) */
3929                         dst_reg->umin_value -= umax_val;
3930                         dst_reg->umax_value -= umin_val;
3931                 }
3932                 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
3933                 break;
3934         case BPF_MUL:
3935                 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
3936                 if (smin_val < 0 || dst_reg->smin_value < 0) {
3937                         /* Ain't nobody got time to multiply that sign */
3938                         __mark_reg_unbounded(dst_reg);
3939                         __update_reg_bounds(dst_reg);
3940                         break;
3941                 }
3942                 /* Both values are positive, so we can work with unsigned and
3943                  * copy the result to signed (unless it exceeds S64_MAX).
3944                  */
3945                 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
3946                         /* Potential overflow, we know nothing */
3947                         __mark_reg_unbounded(dst_reg);
3948                         /* (except what we can learn from the var_off) */
3949                         __update_reg_bounds(dst_reg);
3950                         break;
3951                 }
3952                 dst_reg->umin_value *= umin_val;
3953                 dst_reg->umax_value *= umax_val;
3954                 if (dst_reg->umax_value > S64_MAX) {
3955                         /* Overflow possible, we know nothing */
3956                         dst_reg->smin_value = S64_MIN;
3957                         dst_reg->smax_value = S64_MAX;
3958                 } else {
3959                         dst_reg->smin_value = dst_reg->umin_value;
3960                         dst_reg->smax_value = dst_reg->umax_value;
3961                 }
3962                 break;
3963         case BPF_AND:
3964                 if (src_known && dst_known) {
3965                         __mark_reg_known(dst_reg, dst_reg->var_off.value &
3966                                                   src_reg.var_off.value);
3967                         break;
3968                 }
3969                 /* We get our minimum from the var_off, since that's inherently
3970                  * bitwise.  Our maximum is the minimum of the operands' maxima.
3971                  */
3972                 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
3973                 dst_reg->umin_value = dst_reg->var_off.value;
3974                 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
3975                 if (dst_reg->smin_value < 0 || smin_val < 0) {
3976                         /* Lose signed bounds when ANDing negative numbers,
3977                          * ain't nobody got time for that.
3978                          */
3979                         dst_reg->smin_value = S64_MIN;
3980                         dst_reg->smax_value = S64_MAX;
3981                 } else {
3982                         /* ANDing two positives gives a positive, so safe to
3983                          * cast result into s64.
3984                          */
3985                         dst_reg->smin_value = dst_reg->umin_value;
3986                         dst_reg->smax_value = dst_reg->umax_value;
3987                 }
3988                 /* We may learn something more from the var_off */
3989                 __update_reg_bounds(dst_reg);
3990                 break;
3991         case BPF_OR:
3992                 if (src_known && dst_known) {
3993                         __mark_reg_known(dst_reg, dst_reg->var_off.value |
3994                                                   src_reg.var_off.value);
3995                         break;
3996                 }
3997                 /* We get our maximum from the var_off, and our minimum is the
3998                  * maximum of the operands' minima
3999                  */
4000                 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
4001                 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4002                 dst_reg->umax_value = dst_reg->var_off.value |
4003                                       dst_reg->var_off.mask;
4004                 if (dst_reg->smin_value < 0 || smin_val < 0) {
4005                         /* Lose signed bounds when ORing negative numbers,
4006                          * ain't nobody got time for that.
4007                          */
4008                         dst_reg->smin_value = S64_MIN;
4009                         dst_reg->smax_value = S64_MAX;
4010                 } else {
4011                         /* ORing two positives gives a positive, so safe to
4012                          * cast result into s64.
4013                          */
4014                         dst_reg->smin_value = dst_reg->umin_value;
4015                         dst_reg->smax_value = dst_reg->umax_value;
4016                 }
4017                 /* We may learn something more from the var_off */
4018                 __update_reg_bounds(dst_reg);
4019                 break;
4020         case BPF_LSH:
4021                 if (umax_val >= insn_bitness) {
4022                         /* Shifts greater than 31 or 63 are undefined.
4023                          * This includes shifts by a negative number.
4024                          */
4025                         mark_reg_unknown(env, regs, insn->dst_reg);
4026                         break;
4027                 }
4028                 /* We lose all sign bit information (except what we can pick
4029                  * up from var_off)
4030                  */
4031                 dst_reg->smin_value = S64_MIN;
4032                 dst_reg->smax_value = S64_MAX;
4033                 /* If we might shift our top bit out, then we know nothing */
4034                 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
4035                         dst_reg->umin_value = 0;
4036                         dst_reg->umax_value = U64_MAX;
4037                 } else {
4038                         dst_reg->umin_value <<= umin_val;
4039                         dst_reg->umax_value <<= umax_val;
4040                 }
4041                 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
4042                 /* We may learn something more from the var_off */
4043                 __update_reg_bounds(dst_reg);
4044                 break;
4045         case BPF_RSH:
4046                 if (umax_val >= insn_bitness) {
4047                         /* Shifts greater than 31 or 63 are undefined.
4048                          * This includes shifts by a negative number.
4049                          */
4050                         mark_reg_unknown(env, regs, insn->dst_reg);
4051                         break;
4052                 }
4053                 /* BPF_RSH is an unsigned shift.  If the value in dst_reg might
4054                  * be negative, then either:
4055                  * 1) src_reg might be zero, so the sign bit of the result is
4056                  *    unknown, so we lose our signed bounds
4057                  * 2) it's known negative, thus the unsigned bounds capture the
4058                  *    signed bounds
4059                  * 3) the signed bounds cross zero, so they tell us nothing
4060                  *    about the result
4061                  * If the value in dst_reg is known nonnegative, then again the
4062                  * unsigned bounts capture the signed bounds.
4063                  * Thus, in all cases it suffices to blow away our signed bounds
4064                  * and rely on inferring new ones from the unsigned bounds and
4065                  * var_off of the result.
4066                  */
4067                 dst_reg->smin_value = S64_MIN;
4068                 dst_reg->smax_value = S64_MAX;
4069                 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
4070                 dst_reg->umin_value >>= umax_val;
4071                 dst_reg->umax_value >>= umin_val;
4072                 /* We may learn something more from the var_off */
4073                 __update_reg_bounds(dst_reg);
4074                 break;
4075         case BPF_ARSH:
4076                 if (umax_val >= insn_bitness) {
4077                         /* Shifts greater than 31 or 63 are undefined.
4078                          * This includes shifts by a negative number.
4079                          */
4080                         mark_reg_unknown(env, regs, insn->dst_reg);
4081                         break;
4082                 }
4083
4084                 /* Upon reaching here, src_known is true and
4085                  * umax_val is equal to umin_val.
4086                  */
4087                 dst_reg->smin_value >>= umin_val;
4088                 dst_reg->smax_value >>= umin_val;
4089                 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val);
4090
4091                 /* blow away the dst_reg umin_value/umax_value and rely on
4092                  * dst_reg var_off to refine the result.
4093                  */
4094                 dst_reg->umin_value = 0;
4095                 dst_reg->umax_value = U64_MAX;
4096                 __update_reg_bounds(dst_reg);
4097                 break;
4098         default:
4099                 mark_reg_unknown(env, regs, insn->dst_reg);
4100                 break;
4101         }
4102
4103         if (BPF_CLASS(insn->code) != BPF_ALU64) {
4104                 /* 32-bit ALU ops are (32,32)->32 */
4105                 coerce_reg_to_size(dst_reg, 4);
4106         }
4107
4108         __reg_deduce_bounds(dst_reg);
4109         __reg_bound_offset(dst_reg);
4110         return 0;
4111 }
4112
4113 /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max
4114  * and var_off.
4115  */
4116 static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4117                                    struct bpf_insn *insn)
4118 {
4119         struct bpf_verifier_state *vstate = env->cur_state;
4120         struct bpf_func_state *state = vstate->frame[vstate->curframe];
4121         struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
4122         struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4123         u8 opcode = BPF_OP(insn->code);
4124
4125         dst_reg = &regs[insn->dst_reg];
4126         src_reg = NULL;
4127         if (dst_reg->type != SCALAR_VALUE)
4128                 ptr_reg = dst_reg;
4129         if (BPF_SRC(insn->code) == BPF_X) {
4130                 src_reg = &regs[insn->src_reg];
4131                 if (src_reg->type != SCALAR_VALUE) {
4132                         if (dst_reg->type != SCALAR_VALUE) {
4133                                 /* Combining two pointers by any ALU op yields
4134                                  * an arbitrary scalar. Disallow all math except
4135                                  * pointer subtraction
4136                                  */
4137                                 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
4138                                         mark_reg_unknown(env, regs, insn->dst_reg);
4139                                         return 0;
4140                                 }
4141                                 verbose(env, "R%d pointer %s pointer prohibited\n",
4142                                         insn->dst_reg,
4143                                         bpf_alu_string[opcode >> 4]);
4144                                 return -EACCES;
4145                         } else {
4146                                 /* scalar += pointer
4147                                  * This is legal, but we have to reverse our
4148                                  * src/dest handling in computing the range
4149                                  */
4150                                 return adjust_ptr_min_max_vals(env, insn,
4151                                                                src_reg, dst_reg);
4152                         }
4153                 } else if (ptr_reg) {
4154                         /* pointer += scalar */
4155                         return adjust_ptr_min_max_vals(env, insn,
4156                                                        dst_reg, src_reg);
4157                 }
4158         } else {
4159                 /* Pretend the src is a reg with a known value, since we only
4160                  * need to be able to read from this state.
4161                  */
4162                 off_reg.type = SCALAR_VALUE;
4163                 __mark_reg_known(&off_reg, insn->imm);
4164                 src_reg = &off_reg;
4165                 if (ptr_reg) /* pointer += K */
4166                         return adjust_ptr_min_max_vals(env, insn,
4167                                                        ptr_reg, src_reg);
4168         }
4169
4170         /* Got here implies adding two SCALAR_VALUEs */
4171         if (WARN_ON_ONCE(ptr_reg)) {
4172                 print_verifier_state(env, state);
4173                 verbose(env, "verifier internal error: unexpected ptr_reg\n");
4174                 return -EINVAL;
4175         }
4176         if (WARN_ON(!src_reg)) {
4177                 print_verifier_state(env, state);
4178                 verbose(env, "verifier internal error: no src_reg\n");
4179                 return -EINVAL;
4180         }
4181         return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
4182 }
4183
4184 /* check validity of 32-bit and 64-bit arithmetic operations */
4185 static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
4186 {
4187         struct bpf_reg_state *regs = cur_regs(env);
4188         u8 opcode = BPF_OP(insn->code);
4189         int err;
4190
4191         if (opcode == BPF_END || opcode == BPF_NEG) {
4192                 if (opcode == BPF_NEG) {
4193                         if (BPF_SRC(insn->code) != 0 ||
4194                             insn->src_reg != BPF_REG_0 ||
4195                             insn->off != 0 || insn->imm != 0) {
4196                                 verbose(env, "BPF_NEG uses reserved fields\n");
4197                                 return -EINVAL;
4198                         }
4199                 } else {
4200                         if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
4201                             (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4202                             BPF_CLASS(insn->code) == BPF_ALU64) {
4203                                 verbose(env, "BPF_END uses reserved fields\n");
4204                                 return -EINVAL;
4205                         }
4206                 }
4207
4208                 /* check src operand */
4209                 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
4210                 if (err)
4211                         return err;
4212
4213                 if (is_pointer_value(env, insn->dst_reg)) {
4214                         verbose(env, "R%d pointer arithmetic prohibited\n",
4215                                 insn->dst_reg);
4216                         return -EACCES;
4217                 }
4218
4219                 /* check dest operand */
4220                 err = check_reg_arg(env, insn->dst_reg, DST_OP);
4221                 if (err)
4222                         return err;
4223
4224         } else if (opcode == BPF_MOV) {
4225
4226                 if (BPF_SRC(insn->code) == BPF_X) {
4227                         if (insn->imm != 0 || insn->off != 0) {
4228                                 verbose(env, "BPF_MOV uses reserved fields\n");
4229                                 return -EINVAL;
4230                         }
4231
4232                         /* check src operand */
4233                         err = check_reg_arg(env, insn->src_reg, SRC_OP);
4234                         if (err)
4235                                 return err;
4236                 } else {
4237                         if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
4238                                 verbose(env, "BPF_MOV uses reserved fields\n");
4239                                 return -EINVAL;
4240                         }
4241                 }
4242
4243                 /* check dest operand, mark as required later */
4244                 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
4245                 if (err)
4246                         return err;
4247
4248                 if (BPF_SRC(insn->code) == BPF_X) {
4249                         struct bpf_reg_state *src_reg = regs + insn->src_reg;
4250                         struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
4251
4252                         if (BPF_CLASS(insn->code) == BPF_ALU64) {
4253                                 /* case: R1 = R2
4254                                  * copy register state to dest reg
4255                                  */
4256                                 *dst_reg = *src_reg;
4257                                 dst_reg->live |= REG_LIVE_WRITTEN;
4258                         } else {
4259                                 /* R1 = (u32) R2 */
4260                                 if (is_pointer_value(env, insn->src_reg)) {
4261                                         verbose(env,
4262                                                 "R%d partial copy of pointer\n",
4263                                                 insn->src_reg);
4264                                         return -EACCES;
4265                                 } else if (src_reg->type == SCALAR_VALUE) {
4266                                         *dst_reg = *src_reg;
4267                                         dst_reg->live |= REG_LIVE_WRITTEN;
4268                                 } else {
4269                                         mark_reg_unknown(env, regs,
4270                                                          insn->dst_reg);
4271                                 }
4272                                 coerce_reg_to_size(dst_reg, 4);
4273                         }
4274                 } else {
4275                         /* case: R = imm
4276                          * remember the value we stored into this reg
4277                          */
4278                         /* clear any state __mark_reg_known doesn't set */
4279                         mark_reg_unknown(env, regs, insn->dst_reg);
4280                         regs[insn->dst_reg].type = SCALAR_VALUE;
4281                         if (BPF_CLASS(insn->code) == BPF_ALU64) {
4282                                 __mark_reg_known(regs + insn->dst_reg,
4283                                                  insn->imm);
4284                         } else {
4285                                 __mark_reg_known(regs + insn->dst_reg,
4286                                                  (u32)insn->imm);
4287                         }
4288                 }
4289
4290         } else if (opcode > BPF_END) {
4291                 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
4292                 return -EINVAL;
4293
4294         } else {        /* all other ALU ops: and, sub, xor, add, ... */
4295
4296                 if (BPF_SRC(insn->code) == BPF_X) {
4297                         if (insn->imm != 0 || insn->off != 0) {
4298                                 verbose(env, "BPF_ALU uses reserved fields\n");
4299                                 return -EINVAL;
4300                         }
4301                         /* check src1 operand */
4302                         err = check_reg_arg(env, insn->src_reg, SRC_OP);
4303                         if (err)
4304                                 return err;
4305                 } else {
4306                         if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
4307                                 verbose(env, "BPF_ALU uses reserved fields\n");
4308                                 return -EINVAL;
4309                         }
4310                 }
4311
4312                 /* check src2 operand */
4313                 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
4314                 if (err)
4315                         return err;
4316
4317                 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
4318                     BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
4319                         verbose(env, "div by zero\n");
4320                         return -EINVAL;
4321                 }
4322
4323                 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
4324                      opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
4325                         int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
4326
4327                         if (insn->imm < 0 || insn->imm >= size) {
4328                                 verbose(env, "invalid shift %d\n", insn->imm);
4329                                 return -EINVAL;
4330                         }
4331                 }
4332
4333                 /* check dest operand */
4334                 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
4335                 if (err)
4336                         return err;
4337
4338                 return adjust_reg_min_max_vals(env, insn);
4339         }
4340
4341         return 0;
4342 }
4343
4344 static void __find_good_pkt_pointers(struct bpf_func_state *state,
4345                                      struct bpf_reg_state *dst_reg,
4346                                      enum bpf_reg_type type, u16 new_range)
4347 {
4348         struct bpf_reg_state *reg;
4349         int i;
4350
4351         for (i = 0; i < MAX_BPF_REG; i++) {
4352                 reg = &state->regs[i];
4353                 if (reg->type == type && reg->id == dst_reg->id)
4354                         /* keep the maximum range already checked */
4355                         reg->range = max(reg->range, new_range);
4356         }
4357
4358         bpf_for_each_spilled_reg(i, state, reg) {
4359                 if (!reg)
4360                         continue;
4361                 if (reg->type == type && reg->id == dst_reg->id)
4362                         reg->range = max(reg->range, new_range);
4363         }
4364 }
4365
4366 static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
4367                                    struct bpf_reg_state *dst_reg,
4368                                    enum bpf_reg_type type,
4369                                    bool range_right_open)
4370 {
4371         u16 new_range;
4372         int i;
4373
4374         if (dst_reg->off < 0 ||
4375             (dst_reg->off == 0 && range_right_open))
4376                 /* This doesn't give us any range */
4377                 return;
4378
4379         if (dst_reg->umax_value > MAX_PACKET_OFF ||
4380             dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
4381                 /* Risk of overflow.  For instance, ptr + (1<<63) may be less
4382                  * than pkt_end, but that's because it's also less than pkt.
4383                  */
4384                 return;
4385
4386         new_range = dst_reg->off;
4387         if (range_right_open)
4388                 new_range--;
4389
4390         /* Examples for register markings:
4391          *
4392          * pkt_data in dst register:
4393          *
4394          *   r2 = r3;
4395          *   r2 += 8;
4396          *   if (r2 > pkt_end) goto <handle exception>
4397          *   <access okay>
4398          *
4399          *   r2 = r3;
4400          *   r2 += 8;
4401          *   if (r2 < pkt_end) goto <access okay>
4402          *   <handle exception>
4403          *
4404          *   Where:
4405          *     r2 == dst_reg, pkt_end == src_reg
4406          *     r2=pkt(id=n,off=8,r=0)
4407          *     r3=pkt(id=n,off=0,r=0)
4408          *
4409          * pkt_data in src register:
4410          *
4411          *   r2 = r3;
4412          *   r2 += 8;
4413          *   if (pkt_end >= r2) goto <access okay>
4414          *   <handle exception>
4415          *
4416          *   r2 = r3;
4417          *   r2 += 8;
4418          *   if (pkt_end <= r2) goto <handle exception>
4419          *   <access okay>
4420          *
4421          *   Where:
4422          *     pkt_end == dst_reg, r2 == src_reg
4423          *     r2=pkt(id=n,off=8,r=0)
4424          *     r3=pkt(id=n,off=0,r=0)
4425          *
4426          * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8)
4427          * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8)
4428          * and [r3, r3 + 8-1) respectively is safe to access depending on
4429          * the check.
4430          */
4431
4432         /* If our ids match, then we must have the same max_value.  And we
4433          * don't care about the other reg's fixed offset, since if it's too big
4434          * the range won't allow anything.
4435          * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
4436          */
4437         for (i = 0; i <= vstate->curframe; i++)
4438                 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
4439                                          new_range);
4440 }
4441
4442 /* compute branch direction of the expression "if (reg opcode val) goto target;"
4443  * and return:
4444  *  1 - branch will be taken and "goto target" will be executed
4445  *  0 - branch will not be taken and fall-through to next insn
4446  * -1 - unknown. Example: "if (reg < 5)" is unknown when register value range [0,10]
4447  */
4448 static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
4449                            bool is_jmp32)
4450 {
4451         struct bpf_reg_state reg_lo;
4452         s64 sval;
4453
4454         if (__is_pointer_value(false, reg))
4455                 return -1;
4456
4457         if (is_jmp32) {
4458                 reg_lo = *reg;
4459                 reg = &reg_lo;
4460                 /* For JMP32, only low 32 bits are compared, coerce_reg_to_size
4461                  * could truncate high bits and update umin/umax according to
4462                  * information of low bits.
4463                  */
4464                 coerce_reg_to_size(reg, 4);
4465                 /* smin/smax need special handling. For example, after coerce,
4466                  * if smin_value is 0x00000000ffffffffLL, the value is -1 when
4467                  * used as operand to JMP32. It is a negative number from s32's
4468                  * point of view, while it is a positive number when seen as
4469                  * s64. The smin/smax are kept as s64, therefore, when used with
4470                  * JMP32, they need to be transformed into s32, then sign
4471                  * extended back to s64.
4472                  *
4473                  * Also, smin/smax were copied from umin/umax. If umin/umax has
4474                  * different sign bit, then min/max relationship doesn't
4475                  * maintain after casting into s32, for this case, set smin/smax
4476                  * to safest range.
4477                  */
4478                 if ((reg->umax_value ^ reg->umin_value) &
4479                     (1ULL << 31)) {
4480                         reg->smin_value = S32_MIN;
4481                         reg->smax_value = S32_MAX;
4482                 }
4483                 reg->smin_value = (s64)(s32)reg->smin_value;
4484                 reg->smax_value = (s64)(s32)reg->smax_value;
4485
4486                 val = (u32)val;
4487                 sval = (s64)(s32)val;
4488         } else {
4489                 sval = (s64)val;
4490         }
4491
4492         switch (opcode) {
4493         case BPF_JEQ:
4494                 if (tnum_is_const(reg->var_off))
4495                         return !!tnum_equals_const(reg->var_off, val);
4496                 break;
4497         case BPF_JNE:
4498                 if (tnum_is_const(reg->var_off))
4499                         return !tnum_equals_const(reg->var_off, val);
4500                 break;
4501         case BPF_JSET:
4502                 if ((~reg->var_off.mask & reg->var_off.value) & val)
4503                         return 1;
4504                 if (!((reg->var_off.mask | reg->var_off.value) & val))
4505                         return 0;
4506                 break;
4507         case BPF_JGT:
4508                 if (reg->umin_value > val)
4509                         return 1;
4510                 else if (reg->umax_value <= val)
4511                         return 0;
4512                 break;
4513         case BPF_JSGT:
4514                 if (reg->smin_value > sval)
4515                         return 1;
4516                 else if (reg->smax_value < sval)
4517                         return 0;
4518                 break;
4519         case BPF_JLT:
4520                 if (reg->umax_value < val)
4521                         return 1;
4522                 else if (reg->umin_value >= val)
4523                         return 0;
4524                 break;
4525         case BPF_JSLT:
4526                 if (reg->smax_value < sval)
4527                         return 1;
4528                 else if (reg->smin_value >= sval)
4529                         return 0;
4530                 break;
4531         case BPF_JGE:
4532                 if (reg->umin_value >= val)
4533                         return 1;
4534                 else if (reg->umax_value < val)
4535                         return 0;
4536                 break;
4537         case BPF_JSGE:
4538                 if (reg->smin_value >= sval)
4539                         return 1;
4540                 else if (reg->smax_value < sval)
4541                         return 0;
4542                 break;
4543         case BPF_JLE:
4544                 if (reg->umax_value <= val)
4545                         return 1;
4546                 else if (reg->umin_value > val)
4547                         return 0;
4548                 break;
4549         case BPF_JSLE:
4550                 if (reg->smax_value <= sval)
4551                         return 1;
4552                 else if (reg->smin_value > sval)
4553                         return 0;
4554                 break;
4555         }
4556
4557         return -1;
4558 }
4559
4560 /* Generate min value of the high 32-bit from TNUM info. */
4561 static u64 gen_hi_min(struct tnum var)
4562 {
4563         return var.value & ~0xffffffffULL;
4564 }
4565
4566 /* Generate max value of the high 32-bit from TNUM info. */
4567 static u64 gen_hi_max(struct tnum var)
4568 {
4569         return (var.value | var.mask) & ~0xffffffffULL;
4570 }
4571
4572 /* Return true if VAL is compared with a s64 sign extended from s32, and they
4573  * are with the same signedness.
4574  */
4575 static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
4576 {
4577         return ((s32)sval >= 0 &&
4578                 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
4579                ((s32)sval < 0 &&
4580                 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
4581 }
4582
4583 /* Adjusts the register min/max values in the case that the dst_reg is the
4584  * variable register that we are working on, and src_reg is a constant or we're
4585  * simply doing a BPF_K check.
4586  * In JEQ/JNE cases we also adjust the var_off values.
4587  */
4588 static void reg_set_min_max(struct bpf_reg_state *true_reg,
4589                             struct bpf_reg_state *false_reg, u64 val,
4590                             u8 opcode, bool is_jmp32)
4591 {
4592         s64 sval;
4593
4594         /* If the dst_reg is a pointer, we can't learn anything about its
4595          * variable offset from the compare (unless src_reg were a pointer into
4596          * the same object, but we don't bother with that.
4597          * Since false_reg and true_reg have the same type by construction, we
4598          * only need to check one of them for pointerness.
4599          */
4600         if (__is_pointer_value(false, false_reg))
4601                 return;
4602
4603         val = is_jmp32 ? (u32)val : val;
4604         sval = is_jmp32 ? (s64)(s32)val : (s64)val;
4605
4606         switch (opcode) {
4607         case BPF_JEQ:
4608         case BPF_JNE:
4609         {
4610                 struct bpf_reg_state *reg =
4611                         opcode == BPF_JEQ ? true_reg : false_reg;
4612
4613                 /* For BPF_JEQ, if this is false we know nothing Jon Snow, but
4614                  * if it is true we know the value for sure. Likewise for
4615                  * BPF_JNE.
4616                  */
4617                 if (is_jmp32) {
4618                         u64 old_v = reg->var_off.value;
4619                         u64 hi_mask = ~0xffffffffULL;
4620
4621                         reg->var_off.value = (old_v & hi_mask) | val;
4622                         reg->var_off.mask &= hi_mask;
4623                 } else {
4624                         __mark_reg_known(reg, val);
4625                 }
4626                 break;
4627         }
4628         case BPF_JSET:
4629                 false_reg->var_off = tnum_and(false_reg->var_off,
4630                                               tnum_const(~val));
4631                 if (is_power_of_2(val))
4632                         true_reg->var_off = tnum_or(true_reg->var_off,
4633                                                     tnum_const(val));
4634                 break;
4635         case BPF_JGE:
4636         case BPF_JGT:
4637         {
4638                 u64 false_umax = opcode == BPF_JGT ? val    : val - 1;
4639                 u64 true_umin = opcode == BPF_JGT ? val + 1 : val;
4640
4641                 if (is_jmp32) {
4642                         false_umax += gen_hi_max(false_reg->var_off);
4643                         true_umin += gen_hi_min(true_reg->var_off);
4644                 }
4645                 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4646                 true_reg->umin_value = max(true_reg->umin_value, true_umin);
4647                 break;
4648         }
4649         case BPF_JSGE:
4650         case BPF_JSGT:
4651         {
4652                 s64 false_smax = opcode == BPF_JSGT ? sval    : sval - 1;
4653                 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
4654
4655                 /* If the full s64 was not sign-extended from s32 then don't
4656                  * deduct further info.
4657                  */
4658                 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4659                         break;
4660                 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4661                 true_reg->smin_value = max(true_reg->smin_value, true_smin);
4662                 break;
4663         }
4664         case BPF_JLE:
4665         case BPF_JLT:
4666         {
4667                 u64 false_umin = opcode == BPF_JLT ? val    : val + 1;
4668                 u64 true_umax = opcode == BPF_JLT ? val - 1 : val;
4669
4670                 if (is_jmp32) {
4671                         false_umin += gen_hi_min(false_reg->var_off);
4672                         true_umax += gen_hi_max(true_reg->var_off);
4673                 }
4674                 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4675                 true_reg->umax_value = min(true_reg->umax_value, true_umax);
4676                 break;
4677         }
4678         case BPF_JSLE:
4679         case BPF_JSLT:
4680         {
4681                 s64 false_smin = opcode == BPF_JSLT ? sval    : sval + 1;
4682                 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
4683
4684                 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4685                         break;
4686                 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4687                 true_reg->smax_value = min(true_reg->smax_value, true_smax);
4688                 break;
4689         }
4690         default:
4691                 break;
4692         }
4693
4694         __reg_deduce_bounds(false_reg);
4695         __reg_deduce_bounds(true_reg);
4696         /* We might have learned some bits from the bounds. */
4697         __reg_bound_offset(false_reg);
4698         __reg_bound_offset(true_reg);
4699         /* Intersecting with the old var_off might have improved our bounds
4700          * slightly.  e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4701          * then new var_off is (0; 0x7f...fc) which improves our umax.
4702          */
4703         __update_reg_bounds(false_reg);
4704         __update_reg_bounds(true_reg);
4705 }
4706
4707 /* Same as above, but for the case that dst_reg holds a constant and src_reg is
4708  * the variable reg.
4709  */
4710 static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
4711                                 struct bpf_reg_state *false_reg, u64 val,
4712                                 u8 opcode, bool is_jmp32)
4713 {
4714         s64 sval;
4715
4716         if (__is_pointer_value(false, false_reg))
4717                 return;
4718
4719         val = is_jmp32 ? (u32)val : val;
4720         sval = is_jmp32 ? (s64)(s32)val : (s64)val;
4721
4722         switch (opcode) {
4723         case BPF_JEQ:
4724         case BPF_JNE:
4725         {
4726                 struct bpf_reg_state *reg =
4727                         opcode == BPF_JEQ ? true_reg : false_reg;
4728
4729                 if (is_jmp32) {
4730                         u64 old_v = reg->var_off.value;
4731                         u64 hi_mask = ~0xffffffffULL;
4732
4733                         reg->var_off.value = (old_v & hi_mask) | val;
4734                         reg->var_off.mask &= hi_mask;
4735                 } else {
4736                         __mark_reg_known(reg, val);
4737                 }
4738                 break;
4739         }
4740         case BPF_JSET:
4741                 false_reg->var_off = tnum_and(false_reg->var_off,
4742                                               tnum_const(~val));
4743                 if (is_power_of_2(val))
4744                         true_reg->var_off = tnum_or(true_reg->var_off,
4745                                                     tnum_const(val));
4746                 break;
4747         case BPF_JGE:
4748         case BPF_JGT:
4749         {
4750                 u64 false_umin = opcode == BPF_JGT ? val    : val + 1;
4751                 u64 true_umax = opcode == BPF_JGT ? val - 1 : val;
4752
4753                 if (is_jmp32) {
4754                         false_umin += gen_hi_min(false_reg->var_off);
4755                         true_umax += gen_hi_max(true_reg->var_off);
4756                 }
4757                 false_reg->umin_value = max(false_reg->umin_value, false_umin);
4758                 true_reg->umax_value = min(true_reg->umax_value, true_umax);
4759                 break;
4760         }
4761         case BPF_JSGE:
4762         case BPF_JSGT:
4763         {
4764                 s64 false_smin = opcode == BPF_JSGT ? sval    : sval + 1;
4765                 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
4766
4767                 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4768                         break;
4769                 false_reg->smin_value = max(false_reg->smin_value, false_smin);
4770                 true_reg->smax_value = min(true_reg->smax_value, true_smax);
4771                 break;
4772         }
4773         case BPF_JLE:
4774         case BPF_JLT:
4775         {
4776                 u64 false_umax = opcode == BPF_JLT ? val    : val - 1;
4777                 u64 true_umin = opcode == BPF_JLT ? val + 1 : val;
4778
4779                 if (is_jmp32) {
4780                         false_umax += gen_hi_max(false_reg->var_off);
4781                         true_umin += gen_hi_min(true_reg->var_off);
4782                 }
4783                 false_reg->umax_value = min(false_reg->umax_value, false_umax);
4784                 true_reg->umin_value = max(true_reg->umin_value, true_umin);
4785                 break;
4786         }
4787         case BPF_JSLE:
4788         case BPF_JSLT:
4789         {
4790                 s64 false_smax = opcode == BPF_JSLT ? sval    : sval - 1;
4791                 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
4792
4793                 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
4794                         break;
4795                 false_reg->smax_value = min(false_reg->smax_value, false_smax);
4796                 true_reg->smin_value = max(true_reg->smin_value, true_smin);
4797                 break;
4798         }
4799         default:
4800                 break;
4801         }
4802
4803         __reg_deduce_bounds(false_reg);
4804         __reg_deduce_bounds(true_reg);
4805         /* We might have learned some bits from the bounds. */
4806         __reg_bound_offset(false_reg);
4807         __reg_bound_offset(true_reg);
4808         /* Intersecting with the old var_off might have improved our bounds
4809          * slightly.  e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4810          * then new var_off is (0; 0x7f...fc) which improves our umax.
4811          */
4812         __update_reg_bounds(false_reg);
4813         __update_reg_bounds(true_reg);
4814 }
4815
4816 /* Regs are known to be equal, so intersect their min/max/var_off */
4817 static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
4818                                   struct bpf_reg_state *dst_reg)
4819 {
4820         src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
4821                                                         dst_reg->umin_value);
4822         src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
4823                                                         dst_reg->umax_value);
4824         src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
4825                                                         dst_reg->smin_value);
4826         src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
4827                                                         dst_reg->smax_value);
4828         src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
4829                                                              dst_reg->var_off);
4830         /* We might have learned new bounds from the var_off. */
4831         __update_reg_bounds(src_reg);
4832         __update_reg_bounds(dst_reg);
4833         /* We might have learned something about the sign bit. */
4834         __reg_deduce_bounds(src_reg);
4835         __reg_deduce_bounds(dst_reg);
4836         /* We might have learned some bits from the bounds. */
4837         __reg_bound_offset(src_reg);
4838         __reg_bound_offset(dst_reg);
4839         /* Intersecting with the old var_off might have improved our bounds
4840          * slightly.  e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc),
4841          * then new var_off is (0; 0x7f...fc) which improves our umax.
4842          */
4843         __update_reg_bounds(src_reg);
4844         __update_reg_bounds(dst_reg);
4845 }
4846
4847 static void reg_combine_min_max(struct bpf_reg_state *true_src,
4848                                 struct bpf_reg_state *true_dst,
4849                                 struct bpf_reg_state *false_src,
4850                                 struct bpf_reg_state *false_dst,
4851                                 u8 opcode)
4852 {
4853         switch (opcode) {
4854         case BPF_JEQ:
4855                 __reg_combine_min_max(true_src, true_dst);
4856                 break;
4857         case BPF_JNE:
4858                 __reg_combine_min_max(false_src, false_dst);
4859                 break;
4860         }
4861 }
4862
4863 static void mark_ptr_or_null_reg(struct bpf_func_state *state,
4864                                  struct bpf_reg_state *reg, u32 id,
4865                                  bool is_null)
4866 {
4867         if (reg_type_may_be_null(reg->type) && reg->id == id) {
4868                 /* Old offset (both fixed and variable parts) should
4869                  * have been known-zero, because we don't allow pointer
4870                  * arithmetic on pointers that might be NULL.
4871                  */
4872                 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
4873                                  !tnum_equals_const(reg->var_off, 0) ||
4874                                  reg->off)) {
4875                         __mark_reg_known_zero(reg);
4876                         reg->off = 0;
4877                 }
4878                 if (is_null) {
4879                         reg->type = SCALAR_VALUE;
4880                 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
4881                         if (reg->map_ptr->inner_map_meta) {
4882                                 reg->type = CONST_PTR_TO_MAP;
4883                                 reg->map_ptr = reg->map_ptr->inner_map_meta;
4884                         } else {
4885                                 reg->type = PTR_TO_MAP_VALUE;
4886                         }
4887                 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
4888                         reg->type = PTR_TO_SOCKET;
4889                 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
4890                         reg->type = PTR_TO_SOCK_COMMON;
4891                 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
4892                         reg->type = PTR_TO_TCP_SOCK;
4893                 }
4894                 if (is_null) {
4895                         /* We don't need id and ref_obj_id from this point
4896                          * onwards anymore, thus we should better reset it,
4897                          * so that state pruning has chances to take effect.
4898                          */
4899                         reg->id = 0;
4900                         reg->ref_obj_id = 0;
4901                 } else if (!reg_may_point_to_spin_lock(reg)) {
4902                         /* For not-NULL ptr, reg->ref_obj_id will be reset
4903                          * in release_reg_references().
4904                          *
4905                          * reg->id is still used by spin_lock ptr. Other
4906                          * than spin_lock ptr type, reg->id can be reset.
4907                          */
4908                         reg->id = 0;
4909                 }
4910         }
4911 }
4912
4913 static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
4914                                     bool is_null)
4915 {
4916         struct bpf_reg_state *reg;
4917         int i;
4918
4919         for (i = 0; i < MAX_BPF_REG; i++)
4920                 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
4921
4922         bpf_for_each_spilled_reg(i, state, reg) {
4923                 if (!reg)
4924                         continue;
4925                 mark_ptr_or_null_reg(state, reg, id, is_null);
4926         }
4927 }
4928
4929 /* The logic is similar to find_good_pkt_pointers(), both could eventually
4930  * be folded together at some point.
4931  */
4932 static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
4933                                   bool is_null)
4934 {
4935         struct bpf_func_state *state = vstate->frame[vstate->curframe];
4936         struct bpf_reg_state *regs = state->regs;
4937         u32 ref_obj_id = regs[regno].ref_obj_id;
4938         u32 id = regs[regno].id;
4939         int i;
4940
4941         if (ref_obj_id && ref_obj_id == id && is_null)
4942                 /* regs[regno] is in the " == NULL" branch.
4943                  * No one could have freed the reference state before
4944                  * doing the NULL check.
4945                  */
4946                 WARN_ON_ONCE(release_reference_state(state, id));
4947
4948         for (i = 0; i <= vstate->curframe; i++)
4949                 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
4950 }
4951
4952 static bool try_match_pkt_pointers(const struct bpf_insn *insn,
4953                                    struct bpf_reg_state *dst_reg,
4954                                    struct bpf_reg_state *src_reg,
4955                                    struct bpf_verifier_state *this_branch,
4956                                    struct bpf_verifier_state *other_branch)
4957 {
4958         if (BPF_SRC(insn->code) != BPF_X)
4959                 return false;
4960
4961         /* Pointers are always 64-bit. */
4962         if (BPF_CLASS(insn->code) == BPF_JMP32)
4963                 return false;
4964
4965         switch (BPF_OP(insn->code)) {
4966         case BPF_JGT:
4967                 if ((dst_reg->type == PTR_TO_PACKET &&
4968                      src_reg->type == PTR_TO_PACKET_END) ||
4969                     (dst_reg->type == PTR_TO_PACKET_META &&
4970                      reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4971                         /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
4972                         find_good_pkt_pointers(this_branch, dst_reg,
4973                                                dst_reg->type, false);
4974                 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4975                             src_reg->type == PTR_TO_PACKET) ||
4976                            (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4977                             src_reg->type == PTR_TO_PACKET_META)) {
4978                         /* pkt_end > pkt_data', pkt_data > pkt_meta' */
4979                         find_good_pkt_pointers(other_branch, src_reg,
4980                                                src_reg->type, true);
4981                 } else {
4982                         return false;
4983                 }
4984                 break;
4985         case BPF_JLT:
4986                 if ((dst_reg->type == PTR_TO_PACKET &&
4987                      src_reg->type == PTR_TO_PACKET_END) ||
4988                     (dst_reg->type == PTR_TO_PACKET_META &&
4989                      reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
4990                         /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
4991                         find_good_pkt_pointers(other_branch, dst_reg,
4992                                                dst_reg->type, true);
4993                 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
4994                             src_reg->type == PTR_TO_PACKET) ||
4995                            (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
4996                             src_reg->type == PTR_TO_PACKET_META)) {
4997                         /* pkt_end < pkt_data', pkt_data > pkt_meta' */
4998                         find_good_pkt_pointers(this_branch, src_reg,
4999                                                src_reg->type, false);
5000                 } else {
5001                         return false;
5002                 }
5003                 break;
5004         case BPF_JGE:
5005                 if ((dst_reg->type == PTR_TO_PACKET &&
5006                      src_reg->type == PTR_TO_PACKET_END) ||
5007                     (dst_reg->type == PTR_TO_PACKET_META &&
5008                      reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5009                         /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
5010                         find_good_pkt_pointers(this_branch, dst_reg,
5011                                                dst_reg->type, true);
5012                 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5013                             src_reg->type == PTR_TO_PACKET) ||
5014                            (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5015                             src_reg->type == PTR_TO_PACKET_META)) {
5016                         /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
5017                         find_good_pkt_pointers(other_branch, src_reg,
5018                                                src_reg->type, false);
5019                 } else {
5020                         return false;
5021                 }
5022                 break;
5023         case BPF_JLE:
5024                 if ((dst_reg->type == PTR_TO_PACKET &&
5025                      src_reg->type == PTR_TO_PACKET_END) ||
5026                     (dst_reg->type == PTR_TO_PACKET_META &&
5027                      reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5028                         /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
5029                         find_good_pkt_pointers(other_branch, dst_reg,
5030                                                dst_reg->type, false);
5031                 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5032                             src_reg->type == PTR_TO_PACKET) ||
5033                            (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5034                             src_reg->type == PTR_TO_PACKET_META)) {
5035                         /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
5036                         find_good_pkt_pointers(this_branch, src_reg,
5037                                                src_reg->type, true);
5038                 } else {
5039                         return false;
5040                 }
5041                 break;
5042         default:
5043                 return false;
5044         }
5045
5046         return true;
5047 }
5048
5049 static int check_cond_jmp_op(struct bpf_verifier_env *env,
5050                              struct bpf_insn *insn, int *insn_idx)
5051 {
5052         struct bpf_verifier_state *this_branch = env->cur_state;
5053         struct bpf_verifier_state *other_branch;
5054         struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
5055         struct bpf_reg_state *dst_reg, *other_branch_regs;
5056         u8 opcode = BPF_OP(insn->code);
5057         bool is_jmp32;
5058         int err;
5059
5060         /* Only conditional jumps are expected to reach here. */
5061         if (opcode == BPF_JA || opcode > BPF_JSLE) {
5062                 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
5063                 return -EINVAL;
5064         }
5065
5066         if (BPF_SRC(insn->code) == BPF_X) {
5067                 if (insn->imm != 0) {
5068                         verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
5069                         return -EINVAL;
5070                 }
5071
5072                 /* check src1 operand */
5073                 err = check_reg_arg(env, insn->src_reg, SRC_OP);
5074                 if (err)
5075                         return err;
5076
5077                 if (is_pointer_value(env, insn->src_reg)) {
5078                         verbose(env, "R%d pointer comparison prohibited\n",
5079                                 insn->src_reg);
5080                         return -EACCES;
5081                 }
5082         } else {
5083                 if (insn->src_reg != BPF_REG_0) {
5084                         verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
5085                         return -EINVAL;
5086                 }
5087         }
5088
5089         /* check src2 operand */
5090         err = check_reg_arg(env, insn->dst_reg, SRC_OP);
5091         if (err)
5092                 return err;
5093
5094         dst_reg = &regs[insn->dst_reg];
5095         is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
5096
5097         if (BPF_SRC(insn->code) == BPF_K) {
5098                 int pred = is_branch_taken(dst_reg, insn->imm, opcode,
5099                                            is_jmp32);
5100
5101                 if (pred == 1) {
5102                          /* only follow the goto, ignore fall-through */
5103                         *insn_idx += insn->off;
5104                         return 0;
5105                 } else if (pred == 0) {
5106                         /* only follow fall-through branch, since
5107                          * that's where the program will go
5108                          */
5109                         return 0;
5110                 }
5111         }
5112
5113         other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5114                                   false);
5115         if (!other_branch)
5116                 return -EFAULT;
5117         other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
5118
5119         /* detect if we are comparing against a constant value so we can adjust
5120          * our min/max values for our dst register.
5121          * this is only legit if both are scalars (or pointers to the same
5122          * object, I suppose, but we don't support that right now), because
5123          * otherwise the different base pointers mean the offsets aren't
5124          * comparable.
5125          */
5126         if (BPF_SRC(insn->code) == BPF_X) {
5127                 struct bpf_reg_state *src_reg = &regs[insn->src_reg];
5128                 struct bpf_reg_state lo_reg0 = *dst_reg;
5129                 struct bpf_reg_state lo_reg1 = *src_reg;
5130                 struct bpf_reg_state *src_lo, *dst_lo;
5131
5132                 dst_lo = &lo_reg0;
5133                 src_lo = &lo_reg1;
5134                 coerce_reg_to_size(dst_lo, 4);
5135                 coerce_reg_to_size(src_lo, 4);
5136
5137                 if (dst_reg->type == SCALAR_VALUE &&
5138                     src_reg->type == SCALAR_VALUE) {
5139                         if (tnum_is_const(src_reg->var_off) ||
5140                             (is_jmp32 && tnum_is_const(src_lo->var_off)))
5141                                 reg_set_min_max(&other_branch_regs[insn->dst_reg],
5142                                                 dst_reg,
5143                                                 is_jmp32
5144                                                 ? src_lo->var_off.value
5145                                                 : src_reg->var_off.value,
5146                                                 opcode, is_jmp32);
5147                         else if (tnum_is_const(dst_reg->var_off) ||
5148                                  (is_jmp32 && tnum_is_const(dst_lo->var_off)))
5149                                 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
5150                                                     src_reg,
5151                                                     is_jmp32
5152                                                     ? dst_lo->var_off.value
5153                                                     : dst_reg->var_off.value,
5154                                                     opcode, is_jmp32);
5155                         else if (!is_jmp32 &&
5156                                  (opcode == BPF_JEQ || opcode == BPF_JNE))
5157                                 /* Comparing for equality, we can combine knowledge */
5158                                 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5159                                                     &other_branch_regs[insn->dst_reg],
5160                                                     src_reg, dst_reg, opcode);
5161                 }
5162         } else if (dst_reg->type == SCALAR_VALUE) {
5163                 reg_set_min_max(&other_branch_regs[insn->dst_reg],
5164                                         dst_reg, insn->imm, opcode, is_jmp32);
5165         }
5166
5167         /* detect if R == 0 where R is returned from bpf_map_lookup_elem().
5168          * NOTE: these optimizations below are related with pointer comparison
5169          *       which will never be JMP32.
5170          */
5171         if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
5172             insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
5173             reg_type_may_be_null(dst_reg->type)) {
5174                 /* Mark all identical registers in each branch as either
5175                  * safe or unknown depending R == 0 or R != 0 conditional.
5176                  */
5177                 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
5178                                       opcode == BPF_JNE);
5179                 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
5180                                       opcode == BPF_JEQ);
5181         } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
5182                                            this_branch, other_branch) &&
5183                    is_pointer_value(env, insn->dst_reg)) {
5184                 verbose(env, "R%d pointer comparison prohibited\n",
5185                         insn->dst_reg);
5186                 return -EACCES;
5187         }
5188         if (env->log.level & BPF_LOG_LEVEL)
5189                 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
5190         return 0;
5191 }
5192
5193 /* verify BPF_LD_IMM64 instruction */
5194 static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
5195 {
5196         struct bpf_insn_aux_data *aux = cur_aux(env);
5197         struct bpf_reg_state *regs = cur_regs(env);
5198         struct bpf_map *map;
5199         int err;
5200
5201         if (BPF_SIZE(insn->code) != BPF_DW) {
5202                 verbose(env, "invalid BPF_LD_IMM insn\n");
5203                 return -EINVAL;
5204         }
5205         if (insn->off != 0) {
5206                 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
5207                 return -EINVAL;
5208         }
5209
5210         err = check_reg_arg(env, insn->dst_reg, DST_OP);
5211         if (err)
5212                 return err;
5213
5214         if (insn->src_reg == 0) {
5215                 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
5216
5217                 regs[insn->dst_reg].type = SCALAR_VALUE;
5218                 __mark_reg_known(&regs[insn->dst_reg], imm);
5219                 return 0;
5220         }
5221
5222         map = env->used_maps[aux->map_index];
5223         mark_reg_known_zero(env, regs, insn->dst_reg);
5224         regs[insn->dst_reg].map_ptr = map;
5225
5226         if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
5227                 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
5228                 regs[insn->dst_reg].off = aux->map_off;
5229                 if (map_value_has_spin_lock(map))
5230                         regs[insn->dst_reg].id = ++env->id_gen;
5231         } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
5232                 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
5233         } else {
5234                 verbose(env, "bpf verifier is misconfigured\n");
5235                 return -EINVAL;
5236         }
5237
5238         return 0;
5239 }
5240
5241 static bool may_access_skb(enum bpf_prog_type type)
5242 {
5243         switch (type) {
5244         case BPF_PROG_TYPE_SOCKET_FILTER:
5245         case BPF_PROG_TYPE_SCHED_CLS:
5246         case BPF_PROG_TYPE_SCHED_ACT:
5247                 return true;
5248         default:
5249                 return false;
5250         }
5251 }
5252
5253 /* verify safety of LD_ABS|LD_IND instructions:
5254  * - they can only appear in the programs where ctx == skb
5255  * - since they are wrappers of function calls, they scratch R1-R5 registers,
5256  *   preserve R6-R9, and store return value into R0
5257  *
5258  * Implicit input:
5259  *   ctx == skb == R6 == CTX
5260  *
5261  * Explicit input:
5262  *   SRC == any register
5263  *   IMM == 32-bit immediate
5264  *
5265  * Output:
5266  *   R0 - 8/16/32-bit skb data converted to cpu endianness
5267  */
5268 static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
5269 {
5270         struct bpf_reg_state *regs = cur_regs(env);
5271         u8 mode = BPF_MODE(insn->code);
5272         int i, err;
5273
5274         if (!may_access_skb(env->prog->type)) {
5275                 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
5276                 return -EINVAL;
5277         }
5278
5279         if (!env->ops->gen_ld_abs) {
5280                 verbose(env, "bpf verifier is misconfigured\n");
5281                 return -EINVAL;
5282         }
5283
5284         if (env->subprog_cnt > 1) {
5285                 /* when program has LD_ABS insn JITs and interpreter assume
5286                  * that r1 == ctx == skb which is not the case for callees
5287                  * that can have arbitrary arguments. It's problematic
5288                  * for main prog as well since JITs would need to analyze
5289                  * all functions in order to make proper register save/restore
5290                  * decisions in the main prog. Hence disallow LD_ABS with calls
5291                  */
5292                 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
5293                 return -EINVAL;
5294         }
5295
5296         if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
5297             BPF_SIZE(insn->code) == BPF_DW ||
5298             (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
5299                 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
5300                 return -EINVAL;
5301         }
5302
5303         /* check whether implicit source operand (register R6) is readable */
5304         err = check_reg_arg(env, BPF_REG_6, SRC_OP);
5305         if (err)
5306                 return err;
5307
5308         /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as
5309          * gen_ld_abs() may terminate the program at runtime, leading to
5310          * reference leak.
5311          */
5312         err = check_reference_leak(env);
5313         if (err) {
5314                 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
5315                 return err;
5316         }
5317
5318         if (env->cur_state->active_spin_lock) {
5319                 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
5320                 return -EINVAL;
5321         }
5322
5323         if (regs[BPF_REG_6].type != PTR_TO_CTX) {
5324                 verbose(env,
5325                         "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
5326                 return -EINVAL;
5327         }
5328
5329         if (mode == BPF_IND) {
5330                 /* check explicit source operand */
5331                 err = check_reg_arg(env, insn->src_reg, SRC_OP);
5332                 if (err)
5333                         return err;
5334         }
5335
5336         /* reset caller saved regs to unreadable */
5337         for (i = 0; i < CALLER_SAVED_REGS; i++) {
5338                 mark_reg_not_init(env, regs, caller_saved[i]);
5339                 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
5340         }
5341
5342         /* mark destination R0 register as readable, since it contains
5343          * the value fetched from the packet.
5344          * Already marked as written above.
5345          */
5346         mark_reg_unknown(env, regs, BPF_REG_0);
5347         return 0;
5348 }
5349
5350 static int check_return_code(struct bpf_verifier_env *env)
5351 {
5352         struct bpf_reg_state *reg;
5353         struct tnum range = tnum_range(0, 1);
5354
5355         switch (env->prog->type) {
5356         case BPF_PROG_TYPE_CGROUP_SKB:
5357         case BPF_PROG_TYPE_CGROUP_SOCK:
5358         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
5359         case BPF_PROG_TYPE_SOCK_OPS:
5360         case BPF_PROG_TYPE_CGROUP_DEVICE:
5361         case BPF_PROG_TYPE_CGROUP_SYSCTL:
5362                 break;
5363         default:
5364                 return 0;
5365         }
5366
5367         reg = cur_regs(env) + BPF_REG_0;
5368         if (reg->type != SCALAR_VALUE) {
5369                 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
5370                         reg_type_str[reg->type]);
5371                 return -EINVAL;
5372         }
5373
5374         if (!tnum_in(range, reg->var_off)) {
5375                 verbose(env, "At program exit the register R0 ");
5376                 if (!tnum_is_unknown(reg->var_off)) {
5377                         char tn_buf[48];
5378
5379                         tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
5380                         verbose(env, "has value %s", tn_buf);
5381                 } else {
5382                         verbose(env, "has unknown scalar value");
5383                 }
5384                 verbose(env, " should have been 0 or 1\n");
5385                 return -EINVAL;
5386         }
5387         return 0;
5388 }
5389
5390 /* non-recursive DFS pseudo code
5391  * 1  procedure DFS-iterative(G,v):
5392  * 2      label v as discovered
5393  * 3      let S be a stack
5394  * 4      S.push(v)
5395  * 5      while S is not empty
5396  * 6            t <- S.pop()
5397  * 7            if t is what we're looking for:
5398  * 8                return t
5399  * 9            for all edges e in G.adjacentEdges(t) do
5400  * 10               if edge e is already labelled
5401  * 11                   continue with the next edge
5402  * 12               w <- G.adjacentVertex(t,e)
5403  * 13               if vertex w is not discovered and not explored
5404  * 14                   label e as tree-edge
5405  * 15                   label w as discovered
5406  * 16                   S.push(w)
5407  * 17                   continue at 5
5408  * 18               else if vertex w is discovered
5409  * 19                   label e as back-edge
5410  * 20               else
5411  * 21                   // vertex w is explored
5412  * 22                   label e as forward- or cross-edge
5413  * 23           label t as explored
5414  * 24           S.pop()
5415  *
5416  * convention:
5417  * 0x10 - discovered
5418  * 0x11 - discovered and fall-through edge labelled
5419  * 0x12 - discovered and fall-through and branch edges labelled
5420  * 0x20 - explored
5421  */
5422
5423 enum {
5424         DISCOVERED = 0x10,
5425         EXPLORED = 0x20,
5426         FALLTHROUGH = 1,
5427         BRANCH = 2,
5428 };
5429
5430 #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L)
5431
5432 /* t, w, e - match pseudo-code above:
5433  * t - index of current instruction
5434  * w - next instruction
5435  * e - edge
5436  */
5437 static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
5438 {
5439         int *insn_stack = env->cfg.insn_stack;
5440         int *insn_state = env->cfg.insn_state;
5441
5442         if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
5443                 return 0;
5444
5445         if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
5446                 return 0;
5447
5448         if (w < 0 || w >= env->prog->len) {
5449                 verbose_linfo(env, t, "%d: ", t);
5450                 verbose(env, "jump out of range from insn %d to %d\n", t, w);
5451                 return -EINVAL;
5452         }
5453
5454         if (e == BRANCH)
5455                 /* mark branch target for state pruning */
5456                 env->explored_states[w] = STATE_LIST_MARK;
5457
5458         if (insn_state[w] == 0) {
5459                 /* tree-edge */
5460                 insn_state[t] = DISCOVERED | e;
5461                 insn_state[w] = DISCOVERED;
5462                 if (env->cfg.cur_stack >= env->prog->len)
5463                         return -E2BIG;
5464                 insn_stack[env->cfg.cur_stack++] = w;
5465                 return 1;
5466         } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
5467                 verbose_linfo(env, t, "%d: ", t);
5468                 verbose_linfo(env, w, "%d: ", w);
5469                 verbose(env, "back-edge from insn %d to %d\n", t, w);
5470                 return -EINVAL;
5471         } else if (insn_state[w] == EXPLORED) {
5472                 /* forward- or cross-edge */
5473                 insn_state[t] = DISCOVERED | e;
5474         } else {
5475                 verbose(env, "insn state internal bug\n");
5476                 return -EFAULT;
5477         }
5478         return 0;
5479 }
5480
5481 /* non-recursive depth-first-search to detect loops in BPF program
5482  * loop == back-edge in directed graph
5483  */
5484 static int check_cfg(struct bpf_verifier_env *env)
5485 {
5486         struct bpf_insn *insns = env->prog->insnsi;
5487         int insn_cnt = env->prog->len;
5488         int *insn_stack, *insn_state;
5489         int ret = 0;
5490         int i, t;
5491
5492         insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5493         if (!insn_state)
5494                 return -ENOMEM;
5495
5496         insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
5497         if (!insn_stack) {
5498                 kvfree(insn_state);
5499                 return -ENOMEM;
5500         }
5501
5502         insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */
5503         insn_stack[0] = 0; /* 0 is the first instruction */
5504         env->cfg.cur_stack = 1;
5505
5506 peek_stack:
5507         if (env->cfg.cur_stack == 0)
5508                 goto check_state;
5509         t = insn_stack[env->cfg.cur_stack - 1];
5510
5511         if (BPF_CLASS(insns[t].code) == BPF_JMP ||
5512             BPF_CLASS(insns[t].code) == BPF_JMP32) {
5513                 u8 opcode = BPF_OP(insns[t].code);
5514
5515                 if (opcode == BPF_EXIT) {
5516                         goto mark_explored;
5517                 } else if (opcode == BPF_CALL) {
5518                         ret = push_insn(t, t + 1, FALLTHROUGH, env);
5519                         if (ret == 1)
5520                                 goto peek_stack;
5521                         else if (ret < 0)
5522                                 goto err_free;
5523                         if (t + 1 < insn_cnt)
5524                                 env->explored_states[t + 1] = STATE_LIST_MARK;
5525                         if (insns[t].src_reg == BPF_PSEUDO_CALL) {
5526                                 env->explored_states[t] = STATE_LIST_MARK;
5527                                 ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env);
5528                                 if (ret == 1)
5529                                         goto peek_stack;
5530                                 else if (ret < 0)
5531                                         goto err_free;
5532                         }
5533                 } else if (opcode == BPF_JA) {
5534                         if (BPF_SRC(insns[t].code) != BPF_K) {
5535                                 ret = -EINVAL;
5536                                 goto err_free;
5537                         }
5538                         /* unconditional jump with single edge */
5539                         ret = push_insn(t, t + insns[t].off + 1,
5540                                         FALLTHROUGH, env);
5541                         if (ret == 1)
5542                                 goto peek_stack;
5543                         else if (ret < 0)
5544                                 goto err_free;
5545                         /* tell verifier to check for equivalent states
5546                          * after every call and jump
5547                          */
5548                         if (t + 1 < insn_cnt)
5549                                 env->explored_states[t + 1] = STATE_LIST_MARK;
5550                 } else {
5551                         /* conditional jump with two edges */
5552                         env->explored_states[t] = STATE_LIST_MARK;
5553                         ret = push_insn(t, t + 1, FALLTHROUGH, env);
5554                         if (ret == 1)
5555                                 goto peek_stack;
5556                         else if (ret < 0)
5557                                 goto err_free;
5558
5559                         ret = push_insn(t, t + insns[t].off + 1, BRANCH, env);
5560                         if (ret == 1)
5561                                 goto peek_stack;
5562                         else if (ret < 0)
5563                                 goto err_free;
5564                 }
5565         } else {
5566                 /* all other non-branch instructions with single
5567                  * fall-through edge
5568                  */
5569                 ret = push_insn(t, t + 1, FALLTHROUGH, env);
5570                 if (ret == 1)
5571                         goto peek_stack;
5572                 else if (ret < 0)
5573                         goto err_free;
5574         }
5575
5576 mark_explored:
5577         insn_state[t] = EXPLORED;
5578         if (env->cfg.cur_stack-- <= 0) {
5579                 verbose(env, "pop stack internal bug\n");
5580                 ret = -EFAULT;
5581                 goto err_free;
5582         }
5583         goto peek_stack;
5584
5585 check_state:
5586         for (i = 0; i < insn_cnt; i++) {
5587                 if (insn_state[i] != EXPLORED) {
5588                         verbose(env, "unreachable insn %d\n", i);
5589                         ret = -EINVAL;
5590                         goto err_free;
5591                 }
5592         }
5593         ret = 0; /* cfg looks good */
5594
5595 err_free:
5596         kvfree(insn_state);
5597         kvfree(insn_stack);
5598         env->cfg.insn_state = env->cfg.insn_stack = NULL;
5599         return ret;
5600 }
5601
5602 /* The minimum supported BTF func info size */
5603 #define MIN_BPF_FUNCINFO_SIZE   8
5604 #define MAX_FUNCINFO_REC_SIZE   252
5605
5606 static int check_btf_func(struct bpf_verifier_env *env,
5607                           const union bpf_attr *attr,
5608                           union bpf_attr __user *uattr)
5609 {
5610         u32 i, nfuncs, urec_size, min_size;
5611         u32 krec_size = sizeof(struct bpf_func_info);
5612         struct bpf_func_info *krecord;
5613         const struct btf_type *type;
5614         struct bpf_prog *prog;
5615         const struct btf *btf;
5616         void __user *urecord;
5617         u32 prev_offset = 0;
5618         int ret = 0;
5619
5620         nfuncs = attr->func_info_cnt;
5621         if (!nfuncs)
5622                 return 0;
5623
5624         if (nfuncs != env->subprog_cnt) {
5625                 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
5626                 return -EINVAL;
5627         }
5628
5629         urec_size = attr->func_info_rec_size;
5630         if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
5631             urec_size > MAX_FUNCINFO_REC_SIZE ||
5632             urec_size % sizeof(u32)) {
5633                 verbose(env, "invalid func info rec size %u\n", urec_size);
5634                 return -EINVAL;
5635         }
5636
5637         prog = env->prog;
5638         btf = prog->aux->btf;
5639
5640         urecord = u64_to_user_ptr(attr->func_info);
5641         min_size = min_t(u32, krec_size, urec_size);
5642
5643         krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
5644         if (!krecord)
5645                 return -ENOMEM;
5646
5647         for (i = 0; i < nfuncs; i++) {
5648                 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
5649                 if (ret) {
5650                         if (ret == -E2BIG) {
5651                                 verbose(env, "nonzero tailing record in func info");
5652                                 /* set the size kernel expects so loader can zero
5653                                  * out the rest of the record.
5654                                  */
5655                                 if (put_user(min_size, &uattr->func_info_rec_size))
5656                                         ret = -EFAULT;
5657                         }
5658                         goto err_free;
5659                 }
5660
5661                 if (copy_from_user(&krecord[i], urecord, min_size)) {
5662                         ret = -EFAULT;
5663                         goto err_free;
5664                 }
5665
5666                 /* check insn_off */
5667                 if (i == 0) {
5668                         if (krecord[i].insn_off) {
5669                                 verbose(env,
5670                                         "nonzero insn_off %u for the first func info record",
5671                                         krecord[i].insn_off);
5672                                 ret = -EINVAL;
5673                                 goto err_free;
5674                         }
5675                 } else if (krecord[i].insn_off <= prev_offset) {
5676                         verbose(env,
5677                                 "same or smaller insn offset (%u) than previous func info record (%u)",
5678                                 krecord[i].insn_off, prev_offset);
5679                         ret = -EINVAL;
5680                         goto err_free;
5681                 }
5682
5683                 if (env->subprog_info[i].start != krecord[i].insn_off) {
5684                         verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
5685                         ret = -EINVAL;
5686                         goto err_free;
5687                 }
5688
5689                 /* check type_id */
5690                 type = btf_type_by_id(btf, krecord[i].type_id);
5691                 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
5692                         verbose(env, "invalid type id %d in func info",
5693                                 krecord[i].type_id);
5694                         ret = -EINVAL;
5695                         goto err_free;
5696                 }
5697
5698                 prev_offset = krecord[i].insn_off;
5699                 urecord += urec_size;
5700         }
5701
5702         prog->aux->func_info = krecord;
5703         prog->aux->func_info_cnt = nfuncs;
5704         return 0;
5705
5706 err_free:
5707         kvfree(krecord);
5708         return ret;
5709 }
5710
5711 static void adjust_btf_func(struct bpf_verifier_env *env)
5712 {
5713         int i;
5714
5715         if (!env->prog->aux->func_info)
5716                 return;
5717
5718         for (i = 0; i < env->subprog_cnt; i++)
5719                 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
5720 }
5721
5722 #define MIN_BPF_LINEINFO_SIZE   (offsetof(struct bpf_line_info, line_col) + \
5723                 sizeof(((struct bpf_line_info *)(0))->line_col))
5724 #define MAX_LINEINFO_REC_SIZE   MAX_FUNCINFO_REC_SIZE
5725
5726 static int check_btf_line(struct bpf_verifier_env *env,
5727                           const union bpf_attr *attr,
5728                           union bpf_attr __user *uattr)
5729 {
5730         u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
5731         struct bpf_subprog_info *sub;
5732         struct bpf_line_info *linfo;
5733         struct bpf_prog *prog;
5734         const struct btf *btf;
5735         void __user *ulinfo;
5736         int err;
5737
5738         nr_linfo = attr->line_info_cnt;
5739         if (!nr_linfo)
5740                 return 0;
5741
5742         rec_size = attr->line_info_rec_size;
5743         if (rec_size < MIN_BPF_LINEINFO_SIZE ||
5744             rec_size > MAX_LINEINFO_REC_SIZE ||
5745             rec_size & (sizeof(u32) - 1))
5746                 return -EINVAL;
5747
5748         /* Need to zero it in case the userspace may
5749          * pass in a smaller bpf_line_info object.
5750          */
5751         linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
5752                          GFP_KERNEL | __GFP_NOWARN);
5753         if (!linfo)
5754                 return -ENOMEM;
5755
5756         prog = env->prog;
5757         btf = prog->aux->btf;
5758
5759         s = 0;
5760         sub = env->subprog_info;
5761         ulinfo = u64_to_user_ptr(attr->line_info);
5762         expected_size = sizeof(struct bpf_line_info);
5763         ncopy = min_t(u32, expected_size, rec_size);
5764         for (i = 0; i < nr_linfo; i++) {
5765                 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
5766                 if (err) {
5767                         if (err == -E2BIG) {
5768                                 verbose(env, "nonzero tailing record in line_info");
5769                                 if (put_user(expected_size,
5770                                              &uattr->line_info_rec_size))
5771                                         err = -EFAULT;
5772                         }
5773                         goto err_free;
5774                 }
5775
5776                 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
5777                         err = -EFAULT;
5778                         goto err_free;
5779                 }
5780
5781                 /*
5782                  * Check insn_off to ensure
5783                  * 1) strictly increasing AND
5784                  * 2) bounded by prog->len
5785                  *
5786                  * The linfo[0].insn_off == 0 check logically falls into
5787                  * the later "missing bpf_line_info for func..." case
5788                  * because the first linfo[0].insn_off must be the
5789                  * first sub also and the first sub must have
5790                  * subprog_info[0].start == 0.
5791                  */
5792                 if ((i && linfo[i].insn_off <= prev_offset) ||
5793                     linfo[i].insn_off >= prog->len) {
5794                         verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
5795                                 i, linfo[i].insn_off, prev_offset,
5796                                 prog->len);
5797                         err = -EINVAL;
5798                         goto err_free;
5799                 }
5800
5801                 if (!prog->insnsi[linfo[i].insn_off].code) {
5802                         verbose(env,
5803                                 "Invalid insn code at line_info[%u].insn_off\n",
5804                                 i);
5805                         err = -EINVAL;
5806                         goto err_free;
5807                 }
5808
5809                 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
5810                     !btf_name_by_offset(btf, linfo[i].file_name_off)) {
5811                         verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
5812                         err = -EINVAL;
5813                         goto err_free;
5814                 }
5815
5816                 if (s != env->subprog_cnt) {
5817                         if (linfo[i].insn_off == sub[s].start) {
5818                                 sub[s].linfo_idx = i;
5819                                 s++;
5820                         } else if (sub[s].start < linfo[i].insn_off) {
5821                                 verbose(env, "missing bpf_line_info for func#%u\n", s);
5822                                 err = -EINVAL;
5823                                 goto err_free;
5824                         }
5825                 }
5826
5827                 prev_offset = linfo[i].insn_off;
5828                 ulinfo += rec_size;
5829         }
5830
5831         if (s != env->subprog_cnt) {
5832                 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
5833                         env->subprog_cnt - s, s);
5834                 err = -EINVAL;
5835                 goto err_free;
5836         }
5837
5838         prog->aux->linfo = linfo;
5839         prog->aux->nr_linfo = nr_linfo;
5840
5841         return 0;
5842
5843 err_free:
5844         kvfree(linfo);
5845         return err;
5846 }
5847
5848 static int check_btf_info(struct bpf_verifier_env *env,
5849                           const union bpf_attr *attr,
5850                           union bpf_attr __user *uattr)
5851 {
5852         struct btf *btf;
5853         int err;
5854
5855         if (!attr->func_info_cnt && !attr->line_info_cnt)
5856                 return 0;
5857
5858         btf = btf_get_by_fd(attr->prog_btf_fd);
5859         if (IS_ERR(btf))
5860                 return PTR_ERR(btf);
5861         env->prog->aux->btf = btf;
5862
5863         err = check_btf_func(env, attr, uattr);
5864         if (err)
5865                 return err;
5866
5867         err = check_btf_line(env, attr, uattr);
5868         if (err)
5869                 return err;
5870
5871         return 0;
5872 }
5873
5874 /* check %cur's range satisfies %old's */
5875 static bool range_within(struct bpf_reg_state *old,
5876                          struct bpf_reg_state *cur)
5877 {
5878         return old->umin_value <= cur->umin_value &&
5879                old->umax_value >= cur->umax_value &&
5880                old->smin_value <= cur->smin_value &&
5881                old->smax_value >= cur->smax_value;
5882 }
5883
5884 /* Maximum number of register states that can exist at once */
5885 #define ID_MAP_SIZE     (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
5886 struct idpair {
5887         u32 old;
5888         u32 cur;
5889 };
5890
5891 /* If in the old state two registers had the same id, then they need to have
5892  * the same id in the new state as well.  But that id could be different from
5893  * the old state, so we need to track the mapping from old to new ids.
5894  * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent
5895  * regs with old id 5 must also have new id 9 for the new state to be safe.  But
5896  * regs with a different old id could still have new id 9, we don't care about
5897  * that.
5898  * So we look through our idmap to see if this old id has been seen before.  If
5899  * so, we require the new id to match; otherwise, we add the id pair to the map.
5900  */
5901 static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
5902 {
5903         unsigned int i;
5904
5905         for (i = 0; i < ID_MAP_SIZE; i++) {
5906                 if (!idmap[i].old) {
5907                         /* Reached an empty slot; haven't seen this id before */
5908                         idmap[i].old = old_id;
5909                         idmap[i].cur = cur_id;
5910                         return true;
5911                 }
5912                 if (idmap[i].old == old_id)
5913                         return idmap[i].cur == cur_id;
5914         }
5915         /* We ran out of idmap slots, which should be impossible */
5916         WARN_ON_ONCE(1);
5917         return false;
5918 }
5919
5920 static void clean_func_state(struct bpf_verifier_env *env,
5921                              struct bpf_func_state *st)
5922 {
5923         enum bpf_reg_liveness live;
5924         int i, j;
5925
5926         for (i = 0; i < BPF_REG_FP; i++) {
5927                 live = st->regs[i].live;
5928                 /* liveness must not touch this register anymore */
5929                 st->regs[i].live |= REG_LIVE_DONE;
5930                 if (!(live & REG_LIVE_READ))
5931                         /* since the register is unused, clear its state
5932                          * to make further comparison simpler
5933                          */
5934                         __mark_reg_not_init(&st->regs[i]);
5935         }
5936
5937         for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
5938                 live = st->stack[i].spilled_ptr.live;
5939                 /* liveness must not touch this stack slot anymore */
5940                 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
5941                 if (!(live & REG_LIVE_READ)) {
5942                         __mark_reg_not_init(&st->stack[i].spilled_ptr);
5943                         for (j = 0; j < BPF_REG_SIZE; j++)
5944                                 st->stack[i].slot_type[j] = STACK_INVALID;
5945                 }
5946         }
5947 }
5948
5949 static void clean_verifier_state(struct bpf_verifier_env *env,
5950                                  struct bpf_verifier_state *st)
5951 {
5952         int i;
5953
5954         if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
5955                 /* all regs in this state in all frames were already marked */
5956                 return;
5957
5958         for (i = 0; i <= st->curframe; i++)
5959                 clean_func_state(env, st->frame[i]);
5960 }
5961
5962 /* the parentage chains form a tree.
5963  * the verifier states are added to state lists at given insn and
5964  * pushed into state stack for future exploration.
5965  * when the verifier reaches bpf_exit insn some of the verifer states
5966  * stored in the state lists have their final liveness state already,
5967  * but a lot of states will get revised from liveness point of view when
5968  * the verifier explores other branches.
5969  * Example:
5970  * 1: r0 = 1
5971  * 2: if r1 == 100 goto pc+1
5972  * 3: r0 = 2
5973  * 4: exit
5974  * when the verifier reaches exit insn the register r0 in the state list of
5975  * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch
5976  * of insn 2 and goes exploring further. At the insn 4 it will walk the
5977  * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ.
5978  *
5979  * Since the verifier pushes the branch states as it sees them while exploring
5980  * the program the condition of walking the branch instruction for the second
5981  * time means that all states below this branch were already explored and
5982  * their final liveness markes are already propagated.
5983  * Hence when the verifier completes the search of state list in is_state_visited()
5984  * we can call this clean_live_states() function to mark all liveness states
5985  * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state'
5986  * will not be used.
5987  * This function also clears the registers and stack for states that !READ
5988  * to simplify state merging.
5989  *
5990  * Important note here that walking the same branch instruction in the callee
5991  * doesn't meant that the states are DONE. The verifier has to compare
5992  * the callsites
5993  */
5994 static void clean_live_states(struct bpf_verifier_env *env, int insn,
5995                               struct bpf_verifier_state *cur)
5996 {
5997         struct bpf_verifier_state_list *sl;
5998         int i;
5999
6000         sl = env->explored_states[insn];
6001         if (!sl)
6002                 return;
6003
6004         while (sl != STATE_LIST_MARK) {
6005                 if (sl->state.curframe != cur->curframe)
6006                         goto next;
6007                 for (i = 0; i <= cur->curframe; i++)
6008                         if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
6009                                 goto next;
6010                 clean_verifier_state(env, &sl->state);
6011 next:
6012                 sl = sl->next;
6013         }
6014 }
6015
6016 /* Returns true if (rold safe implies rcur safe) */
6017 static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
6018                     struct idpair *idmap)
6019 {
6020         bool equal;
6021
6022         if (!(rold->live & REG_LIVE_READ))
6023                 /* explored state didn't use this */
6024                 return true;
6025
6026         equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
6027
6028         if (rold->type == PTR_TO_STACK)
6029                 /* two stack pointers are equal only if they're pointing to
6030                  * the same stack frame, since fp-8 in foo != fp-8 in bar
6031                  */
6032                 return equal && rold->frameno == rcur->frameno;
6033
6034         if (equal)
6035                 return true;
6036
6037         if (rold->type == NOT_INIT)
6038                 /* explored state can't have used this */
6039                 return true;
6040         if (rcur->type == NOT_INIT)
6041                 return false;
6042         switch (rold->type) {
6043         case SCALAR_VALUE:
6044                 if (rcur->type == SCALAR_VALUE) {
6045                         /* new val must satisfy old val knowledge */
6046                         return range_within(rold, rcur) &&
6047                                tnum_in(rold->var_off, rcur->var_off);
6048                 } else {
6049                         /* We're trying to use a pointer in place of a scalar.
6050                          * Even if the scalar was unbounded, this could lead to
6051                          * pointer leaks because scalars are allowed to leak
6052                          * while pointers are not. We could make this safe in
6053                          * special cases if root is calling us, but it's
6054                          * probably not worth the hassle.
6055                          */
6056                         return false;
6057                 }
6058         case PTR_TO_MAP_VALUE:
6059                 /* If the new min/max/var_off satisfy the old ones and
6060                  * everything else matches, we are OK.
6061                  * 'id' is not compared, since it's only used for maps with
6062                  * bpf_spin_lock inside map element and in such cases if
6063                  * the rest of the prog is valid for one map element then
6064                  * it's valid for all map elements regardless of the key
6065                  * used in bpf_map_lookup()
6066                  */
6067                 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6068                        range_within(rold, rcur) &&
6069                        tnum_in(rold->var_off, rcur->var_off);
6070         case PTR_TO_MAP_VALUE_OR_NULL:
6071                 /* a PTR_TO_MAP_VALUE could be safe to use as a
6072                  * PTR_TO_MAP_VALUE_OR_NULL into the same map.
6073                  * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
6074                  * checked, doing so could have affected others with the same
6075                  * id, and we can't check for that because we lost the id when
6076                  * we converted to a PTR_TO_MAP_VALUE.
6077                  */
6078                 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6079                         return false;
6080                 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6081                         return false;
6082                 /* Check our ids match any regs they're supposed to */
6083                 return check_ids(rold->id, rcur->id, idmap);
6084         case PTR_TO_PACKET_META:
6085         case PTR_TO_PACKET:
6086                 if (rcur->type != rold->type)
6087                         return false;
6088                 /* We must have at least as much range as the old ptr
6089                  * did, so that any accesses which were safe before are
6090                  * still safe.  This is true even if old range < old off,
6091                  * since someone could have accessed through (ptr - k), or
6092                  * even done ptr -= k in a register, to get a safe access.
6093                  */
6094                 if (rold->range > rcur->range)
6095                         return false;
6096                 /* If the offsets don't match, we can't trust our alignment;
6097                  * nor can we be sure that we won't fall out of range.
6098                  */
6099                 if (rold->off != rcur->off)
6100                         return false;
6101                 /* id relations must be preserved */
6102                 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6103                         return false;
6104                 /* new val must satisfy old val knowledge */
6105                 return range_within(rold, rcur) &&
6106                        tnum_in(rold->var_off, rcur->var_off);
6107         case PTR_TO_CTX:
6108         case CONST_PTR_TO_MAP:
6109         case PTR_TO_PACKET_END:
6110         case PTR_TO_FLOW_KEYS:
6111         case PTR_TO_SOCKET:
6112         case PTR_TO_SOCKET_OR_NULL:
6113         case PTR_TO_SOCK_COMMON:
6114         case PTR_TO_SOCK_COMMON_OR_NULL:
6115         case PTR_TO_TCP_SOCK:
6116         case PTR_TO_TCP_SOCK_OR_NULL:
6117                 /* Only valid matches are exact, which memcmp() above
6118                  * would have accepted
6119                  */
6120         default:
6121                 /* Don't know what's going on, just say it's not safe */
6122                 return false;
6123         }
6124
6125         /* Shouldn't get here; if we do, say it's not safe */
6126         WARN_ON_ONCE(1);
6127         return false;
6128 }
6129
6130 static bool stacksafe(struct bpf_func_state *old,
6131                       struct bpf_func_state *cur,
6132                       struct idpair *idmap)
6133 {
6134         int i, spi;
6135
6136         /* walk slots of the explored stack and ignore any additional
6137          * slots in the current stack, since explored(safe) state
6138          * didn't use them
6139          */
6140         for (i = 0; i < old->allocated_stack; i++) {
6141                 spi = i / BPF_REG_SIZE;
6142
6143                 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
6144                         i += BPF_REG_SIZE - 1;
6145                         /* explored state didn't use this */
6146                         continue;
6147                 }
6148
6149                 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
6150                         continue;
6151
6152                 /* explored stack has more populated slots than current stack
6153                  * and these slots were used
6154                  */
6155                 if (i >= cur->allocated_stack)
6156                         return false;
6157
6158                 /* if old state was safe with misc data in the stack
6159                  * it will be safe with zero-initialized stack.
6160                  * The opposite is not true
6161                  */
6162                 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
6163                     cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
6164                         continue;
6165                 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
6166                     cur->stack[spi].slot_type[i % BPF_REG_SIZE])
6167                         /* Ex: old explored (safe) state has STACK_SPILL in
6168                          * this stack slot, but current has has STACK_MISC ->
6169                          * this verifier states are not equivalent,
6170                          * return false to continue verification of this path
6171                          */
6172                         return false;
6173                 if (i % BPF_REG_SIZE)
6174                         continue;
6175                 if (old->stack[spi].slot_type[0] != STACK_SPILL)
6176                         continue;
6177                 if (!regsafe(&old->stack[spi].spilled_ptr,
6178                              &cur->stack[spi].spilled_ptr,
6179                              idmap))
6180                         /* when explored and current stack slot are both storing
6181                          * spilled registers, check that stored pointers types
6182                          * are the same as well.
6183                          * Ex: explored safe path could have stored
6184                          * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
6185                          * but current path has stored:
6186                          * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
6187                          * such verifier states are not equivalent.
6188                          * return false to continue verification of this path
6189                          */
6190                         return false;
6191         }
6192         return true;
6193 }
6194
6195 static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
6196 {
6197         if (old->acquired_refs != cur->acquired_refs)
6198                 return false;
6199         return !memcmp(old->refs, cur->refs,
6200                        sizeof(*old->refs) * old->acquired_refs);
6201 }
6202
6203 /* compare two verifier states
6204  *
6205  * all states stored in state_list are known to be valid, since
6206  * verifier reached 'bpf_exit' instruction through them
6207  *
6208  * this function is called when verifier exploring different branches of
6209  * execution popped from the state stack. If it sees an old state that has
6210  * more strict register state and more strict stack state then this execution
6211  * branch doesn't need to be explored further, since verifier already
6212  * concluded that more strict state leads to valid finish.
6213  *
6214  * Therefore two states are equivalent if register state is more conservative
6215  * and explored stack state is more conservative than the current one.
6216  * Example:
6217  *       explored                   current
6218  * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC)
6219  * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC)
6220  *
6221  * In other words if current stack state (one being explored) has more
6222  * valid slots than old one that already passed validation, it means
6223  * the verifier can stop exploring and conclude that current state is valid too
6224  *
6225  * Similarly with registers. If explored state has register type as invalid
6226  * whereas register type in current state is meaningful, it means that
6227  * the current state will reach 'bpf_exit' instruction safely
6228  */
6229 static bool func_states_equal(struct bpf_func_state *old,
6230                               struct bpf_func_state *cur)
6231 {
6232         struct idpair *idmap;
6233         bool ret = false;
6234         int i;
6235
6236         idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
6237         /* If we failed to allocate the idmap, just say it's not safe */
6238         if (!idmap)
6239                 return false;
6240
6241         for (i = 0; i < MAX_BPF_REG; i++) {
6242                 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
6243                         goto out_free;
6244         }
6245
6246         if (!stacksafe(old, cur, idmap))
6247                 goto out_free;
6248
6249         if (!refsafe(old, cur))
6250                 goto out_free;
6251         ret = true;
6252 out_free:
6253         kfree(idmap);
6254         return ret;
6255 }
6256
6257 static bool states_equal(struct bpf_verifier_env *env,
6258                          struct bpf_verifier_state *old,
6259                          struct bpf_verifier_state *cur)
6260 {
6261         int i;
6262
6263         if (old->curframe != cur->curframe)
6264                 return false;
6265
6266         /* Verification state from speculative execution simulation
6267          * must never prune a non-speculative execution one.
6268          */
6269         if (old->speculative && !cur->speculative)
6270                 return false;
6271
6272         if (old->active_spin_lock != cur->active_spin_lock)
6273                 return false;
6274
6275         /* for states to be equal callsites have to be the same
6276          * and all frame states need to be equivalent
6277          */
6278         for (i = 0; i <= old->curframe; i++) {
6279                 if (old->frame[i]->callsite != cur->frame[i]->callsite)
6280                         return false;
6281                 if (!func_states_equal(old->frame[i], cur->frame[i]))
6282                         return false;
6283         }
6284         return true;
6285 }
6286
6287 static int propagate_liveness_reg(struct bpf_verifier_env *env,
6288                                   struct bpf_reg_state *reg,
6289                                   struct bpf_reg_state *parent_reg)
6290 {
6291         int err;
6292
6293         if (parent_reg->live & REG_LIVE_READ || !(reg->live & REG_LIVE_READ))
6294                 return 0;
6295
6296         err = mark_reg_read(env, reg, parent_reg);
6297         if (err)
6298                 return err;
6299
6300         return 0;
6301 }
6302
6303 /* A write screens off any subsequent reads; but write marks come from the
6304  * straight-line code between a state and its parent.  When we arrive at an
6305  * equivalent state (jump target or such) we didn't arrive by the straight-line
6306  * code, so read marks in the state must propagate to the parent regardless
6307  * of the state's write marks. That's what 'parent == state->parent' comparison
6308  * in mark_reg_read() is for.
6309  */
6310 static int propagate_liveness(struct bpf_verifier_env *env,
6311                               const struct bpf_verifier_state *vstate,
6312                               struct bpf_verifier_state *vparent)
6313 {
6314         struct bpf_reg_state *state_reg, *parent_reg;
6315         struct bpf_func_state *state, *parent;
6316         int i, frame, err = 0;
6317
6318         if (vparent->curframe != vstate->curframe) {
6319                 WARN(1, "propagate_live: parent frame %d current frame %d\n",
6320                      vparent->curframe, vstate->curframe);
6321                 return -EFAULT;
6322         }
6323         /* Propagate read liveness of registers... */
6324         BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
6325         for (frame = 0; frame <= vstate->curframe; frame++) {
6326                 parent = vparent->frame[frame];
6327                 state = vstate->frame[frame];
6328                 parent_reg = parent->regs;
6329                 state_reg = state->regs;
6330                 /* We don't need to worry about FP liveness, it's read-only */
6331                 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
6332                         err = propagate_liveness_reg(env, &state_reg[i],
6333                                                      &parent_reg[i]);
6334                         if (err)
6335                                 return err;
6336                 }
6337
6338                 /* Propagate stack slots. */
6339                 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
6340                             i < parent->allocated_stack / BPF_REG_SIZE; i++) {
6341                         parent_reg = &parent->stack[i].spilled_ptr;
6342                         state_reg = &state->stack[i].spilled_ptr;
6343                         err = propagate_liveness_reg(env, state_reg,
6344                                                      parent_reg);
6345                         if (err)
6346                                 return err;
6347                 }
6348         }
6349         return err;
6350 }
6351
6352 static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
6353 {
6354         struct bpf_verifier_state_list *new_sl;
6355         struct bpf_verifier_state_list *sl, **pprev;
6356         struct bpf_verifier_state *cur = env->cur_state, *new;
6357         int i, j, err, states_cnt = 0;
6358
6359         pprev = &env->explored_states[insn_idx];
6360         sl = *pprev;
6361
6362         if (!sl)
6363                 /* this 'insn_idx' instruction wasn't marked, so we will not
6364                  * be doing state search here
6365                  */
6366                 return 0;
6367
6368         clean_live_states(env, insn_idx, cur);
6369
6370         while (sl != STATE_LIST_MARK) {
6371                 if (states_equal(env, &sl->state, cur)) {
6372                         sl->hit_cnt++;
6373                         /* reached equivalent register/stack state,
6374                          * prune the search.
6375                          * Registers read by the continuation are read by us.
6376                          * If we have any write marks in env->cur_state, they
6377                          * will prevent corresponding reads in the continuation
6378                          * from reaching our parent (an explored_state).  Our
6379                          * own state will get the read marks recorded, but
6380                          * they'll be immediately forgotten as we're pruning
6381                          * this state and will pop a new one.
6382                          */
6383                         err = propagate_liveness(env, &sl->state, cur);
6384                         if (err)
6385                                 return err;
6386                         return 1;
6387                 }
6388                 states_cnt++;
6389                 sl->miss_cnt++;
6390                 /* heuristic to determine whether this state is beneficial
6391                  * to keep checking from state equivalence point of view.
6392                  * Higher numbers increase max_states_per_insn and verification time,
6393                  * but do not meaningfully decrease insn_processed.
6394                  */
6395                 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
6396                         /* the state is unlikely to be useful. Remove it to
6397                          * speed up verification
6398                          */
6399                         *pprev = sl->next;
6400                         if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
6401                                 free_verifier_state(&sl->state, false);
6402                                 kfree(sl);
6403                                 env->peak_states--;
6404                         } else {
6405                                 /* cannot free this state, since parentage chain may
6406                                  * walk it later. Add it for free_list instead to
6407                                  * be freed at the end of verification
6408                                  */
6409                                 sl->next = env->free_list;
6410                                 env->free_list = sl;
6411                         }
6412                         sl = *pprev;
6413                         continue;
6414                 }
6415                 pprev = &sl->next;
6416                 sl = *pprev;
6417         }
6418
6419         if (env->max_states_per_insn < states_cnt)
6420                 env->max_states_per_insn = states_cnt;
6421
6422         if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
6423                 return 0;
6424
6425         /* there were no equivalent states, remember current one.
6426          * technically the current state is not proven to be safe yet,
6427          * but it will either reach outer most bpf_exit (which means it's safe)
6428          * or it will be rejected. Since there are no loops, we won't be
6429          * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx)
6430          * again on the way to bpf_exit
6431          */
6432         new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
6433         if (!new_sl)
6434                 return -ENOMEM;
6435         env->total_states++;
6436         env->peak_states++;
6437
6438         /* add new state to the head of linked list */
6439         new = &new_sl->state;
6440         err = copy_verifier_state(new, cur);
6441         if (err) {
6442                 free_verifier_state(new, false);
6443                 kfree(new_sl);
6444                 return err;
6445         }
6446         new_sl->next = env->explored_states[insn_idx];
6447         env->explored_states[insn_idx] = new_sl;
6448         /* connect new state to parentage chain. Current frame needs all
6449          * registers connected. Only r6 - r9 of the callers are alive (pushed
6450          * to the stack implicitly by JITs) so in callers' frames connect just
6451          * r6 - r9 as an optimization. Callers will have r1 - r5 connected to
6452          * the state of the call instruction (with WRITTEN set), and r0 comes
6453          * from callee with its full parentage chain, anyway.
6454          */
6455         for (j = 0; j <= cur->curframe; j++)
6456                 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
6457                         cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
6458         /* clear write marks in current state: the writes we did are not writes
6459          * our child did, so they don't screen off its reads from us.
6460          * (There are no read marks in current state, because reads always mark
6461          * their parent and current state never has children yet.  Only
6462          * explored_states can get read marks.)
6463          */
6464         for (i = 0; i < BPF_REG_FP; i++)
6465                 cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE;
6466
6467         /* all stack frames are accessible from callee, clear them all */
6468         for (j = 0; j <= cur->curframe; j++) {
6469                 struct bpf_func_state *frame = cur->frame[j];
6470                 struct bpf_func_state *newframe = new->frame[j];
6471
6472                 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
6473                         frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
6474                         frame->stack[i].spilled_ptr.parent =
6475                                                 &newframe->stack[i].spilled_ptr;
6476                 }
6477         }
6478         return 0;
6479 }
6480
6481 /* Return true if it's OK to have the same insn return a different type. */
6482 static bool reg_type_mismatch_ok(enum bpf_reg_type type)
6483 {
6484         switch (type) {
6485         case PTR_TO_CTX:
6486         case PTR_TO_SOCKET:
6487         case PTR_TO_SOCKET_OR_NULL:
6488         case PTR_TO_SOCK_COMMON:
6489         case PTR_TO_SOCK_COMMON_OR_NULL:
6490         case PTR_TO_TCP_SOCK:
6491         case PTR_TO_TCP_SOCK_OR_NULL:
6492                 return false;
6493         default:
6494                 return true;
6495         }
6496 }
6497
6498 /* If an instruction was previously used with particular pointer types, then we
6499  * need to be careful to avoid cases such as the below, where it may be ok
6500  * for one branch accessing the pointer, but not ok for the other branch:
6501  *
6502  * R1 = sock_ptr
6503  * goto X;
6504  * ...
6505  * R1 = some_other_valid_ptr;
6506  * goto X;
6507  * ...
6508  * R2 = *(u32 *)(R1 + 0);
6509  */
6510 static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
6511 {
6512         return src != prev && (!reg_type_mismatch_ok(src) ||
6513                                !reg_type_mismatch_ok(prev));
6514 }
6515
6516 static int do_check(struct bpf_verifier_env *env)
6517 {
6518         struct bpf_verifier_state *state;
6519         struct bpf_insn *insns = env->prog->insnsi;
6520         struct bpf_reg_state *regs;
6521         int insn_cnt = env->prog->len;
6522         bool do_print_state = false;
6523
6524         env->prev_linfo = NULL;
6525
6526         state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
6527         if (!state)
6528                 return -ENOMEM;
6529         state->curframe = 0;
6530         state->speculative = false;
6531         state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
6532         if (!state->frame[0]) {
6533                 kfree(state);
6534                 return -ENOMEM;
6535         }
6536         env->cur_state = state;
6537         init_func_state(env, state->frame[0],
6538                         BPF_MAIN_FUNC /* callsite */,
6539                         0 /* frameno */,
6540                         0 /* subprogno, zero == main subprog */);
6541
6542         for (;;) {
6543                 struct bpf_insn *insn;
6544                 u8 class;
6545                 int err;
6546
6547                 if (env->insn_idx >= insn_cnt) {
6548                         verbose(env, "invalid insn idx %d insn_cnt %d\n",
6549                                 env->insn_idx, insn_cnt);
6550                         return -EFAULT;
6551                 }
6552
6553                 insn = &insns[env->insn_idx];
6554                 class = BPF_CLASS(insn->code);
6555
6556                 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
6557                         verbose(env,
6558                                 "BPF program is too large. Processed %d insn\n",
6559                                 env->insn_processed);
6560                         return -E2BIG;
6561                 }
6562
6563                 err = is_state_visited(env, env->insn_idx);
6564                 if (err < 0)
6565                         return err;
6566                 if (err == 1) {
6567                         /* found equivalent state, can prune the search */
6568                         if (env->log.level & BPF_LOG_LEVEL) {
6569                                 if (do_print_state)
6570                                         verbose(env, "\nfrom %d to %d%s: safe\n",
6571                                                 env->prev_insn_idx, env->insn_idx,
6572                                                 env->cur_state->speculative ?
6573                                                 " (speculative execution)" : "");
6574                                 else
6575                                         verbose(env, "%d: safe\n", env->insn_idx);
6576                         }
6577                         goto process_bpf_exit;
6578                 }
6579
6580                 if (signal_pending(current))
6581                         return -EAGAIN;
6582
6583                 if (need_resched())
6584                         cond_resched();
6585
6586                 if (env->log.level & BPF_LOG_LEVEL2 ||
6587                     (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
6588                         if (env->log.level & BPF_LOG_LEVEL2)
6589                                 verbose(env, "%d:", env->insn_idx);
6590                         else
6591                                 verbose(env, "\nfrom %d to %d%s:",
6592                                         env->prev_insn_idx, env->insn_idx,
6593                                         env->cur_state->speculative ?
6594                                         " (speculative execution)" : "");
6595                         print_verifier_state(env, state->frame[state->curframe]);
6596                         do_print_state = false;
6597                 }
6598
6599                 if (env->log.level & BPF_LOG_LEVEL) {
6600                         const struct bpf_insn_cbs cbs = {
6601                                 .cb_print       = verbose,
6602                                 .private_data   = env,
6603                         };
6604
6605                         verbose_linfo(env, env->insn_idx, "; ");
6606                         verbose(env, "%d: ", env->insn_idx);
6607                         print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
6608                 }
6609
6610                 if (bpf_prog_is_dev_bound(env->prog->aux)) {
6611                         err = bpf_prog_offload_verify_insn(env, env->insn_idx,
6612                                                            env->prev_insn_idx);
6613                         if (err)
6614                                 return err;
6615                 }
6616
6617                 regs = cur_regs(env);
6618                 env->insn_aux_data[env->insn_idx].seen = true;
6619
6620                 if (class == BPF_ALU || class == BPF_ALU64) {
6621                         err = check_alu_op(env, insn);
6622                         if (err)
6623                                 return err;
6624
6625                 } else if (class == BPF_LDX) {
6626                         enum bpf_reg_type *prev_src_type, src_reg_type;
6627
6628                         /* check for reserved fields is already done */
6629
6630                         /* check src operand */
6631                         err = check_reg_arg(env, insn->src_reg, SRC_OP);
6632                         if (err)
6633                                 return err;
6634
6635                         err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
6636                         if (err)
6637                                 return err;
6638
6639                         src_reg_type = regs[insn->src_reg].type;
6640
6641                         /* check that memory (src_reg + off) is readable,
6642                          * the state of dst_reg will be updated by this func
6643                          */
6644                         err = check_mem_access(env, env->insn_idx, insn->src_reg,
6645                                                insn->off, BPF_SIZE(insn->code),
6646                                                BPF_READ, insn->dst_reg, false);
6647                         if (err)
6648                                 return err;
6649
6650                         prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
6651
6652                         if (*prev_src_type == NOT_INIT) {
6653                                 /* saw a valid insn
6654                                  * dst_reg = *(u32 *)(src_reg + off)
6655                                  * save type to validate intersecting paths
6656                                  */
6657                                 *prev_src_type = src_reg_type;
6658
6659                         } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
6660                                 /* ABuser program is trying to use the same insn
6661                                  * dst_reg = *(u32*) (src_reg + off)
6662                                  * with different pointer types:
6663                                  * src_reg == ctx in one branch and
6664                                  * src_reg == stack|map in some other branch.
6665                                  * Reject it.
6666                                  */
6667                                 verbose(env, "same insn cannot be used with different pointers\n");
6668                                 return -EINVAL;
6669                         }
6670
6671                 } else if (class == BPF_STX) {
6672                         enum bpf_reg_type *prev_dst_type, dst_reg_type;
6673
6674                         if (BPF_MODE(insn->code) == BPF_XADD) {
6675                                 err = check_xadd(env, env->insn_idx, insn);
6676                                 if (err)
6677                                         return err;
6678                                 env->insn_idx++;
6679                                 continue;
6680                         }
6681
6682                         /* check src1 operand */
6683                         err = check_reg_arg(env, insn->src_reg, SRC_OP);
6684                         if (err)
6685                                 return err;
6686                         /* check src2 operand */
6687                         err = check_reg_arg(env, insn->dst_reg, SRC_OP);
6688                         if (err)
6689                                 return err;
6690
6691                         dst_reg_type = regs[insn->dst_reg].type;
6692
6693                         /* check that memory (dst_reg + off) is writeable */
6694                         err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6695                                                insn->off, BPF_SIZE(insn->code),
6696                                                BPF_WRITE, insn->src_reg, false);
6697                         if (err)
6698                                 return err;
6699
6700                         prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
6701
6702                         if (*prev_dst_type == NOT_INIT) {
6703                                 *prev_dst_type = dst_reg_type;
6704                         } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
6705                                 verbose(env, "same insn cannot be used with different pointers\n");
6706                                 return -EINVAL;
6707                         }
6708
6709                 } else if (class == BPF_ST) {
6710                         if (BPF_MODE(insn->code) != BPF_MEM ||
6711                             insn->src_reg != BPF_REG_0) {
6712                                 verbose(env, "BPF_ST uses reserved fields\n");
6713                                 return -EINVAL;
6714                         }
6715                         /* check src operand */
6716                         err = check_reg_arg(env, insn->dst_reg, SRC_OP);
6717                         if (err)
6718                                 return err;
6719
6720                         if (is_ctx_reg(env, insn->dst_reg)) {
6721                                 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
6722                                         insn->dst_reg,
6723                                         reg_type_str[reg_state(env, insn->dst_reg)->type]);
6724                                 return -EACCES;
6725                         }
6726
6727                         /* check that memory (dst_reg + off) is writeable */
6728                         err = check_mem_access(env, env->insn_idx, insn->dst_reg,
6729                                                insn->off, BPF_SIZE(insn->code),
6730                                                BPF_WRITE, -1, false);
6731                         if (err)
6732                                 return err;
6733
6734                 } else if (class == BPF_JMP || class == BPF_JMP32) {
6735                         u8 opcode = BPF_OP(insn->code);
6736
6737                         if (opcode == BPF_CALL) {
6738                                 if (BPF_SRC(insn->code) != BPF_K ||
6739                                     insn->off != 0 ||
6740                                     (insn->src_reg != BPF_REG_0 &&
6741                                      insn->src_reg != BPF_PSEUDO_CALL) ||
6742                                     insn->dst_reg != BPF_REG_0 ||
6743                                     class == BPF_JMP32) {
6744                                         verbose(env, "BPF_CALL uses reserved fields\n");
6745                                         return -EINVAL;
6746                                 }
6747
6748                                 if (env->cur_state->active_spin_lock &&
6749                                     (insn->src_reg == BPF_PSEUDO_CALL ||
6750                                      insn->imm != BPF_FUNC_spin_unlock)) {
6751                                         verbose(env, "function calls are not allowed while holding a lock\n");
6752                                         return -EINVAL;
6753                                 }
6754                                 if (insn->src_reg == BPF_PSEUDO_CALL)
6755                                         err = check_func_call(env, insn, &env->insn_idx);
6756                                 else
6757                                         err = check_helper_call(env, insn->imm, env->insn_idx);
6758                                 if (err)
6759                                         return err;
6760
6761                         } else if (opcode == BPF_JA) {
6762                                 if (BPF_SRC(insn->code) != BPF_K ||
6763                                     insn->imm != 0 ||
6764                                     insn->src_reg != BPF_REG_0 ||
6765                                     insn->dst_reg != BPF_REG_0 ||
6766                                     class == BPF_JMP32) {
6767                                         verbose(env, "BPF_JA uses reserved fields\n");
6768                                         return -EINVAL;
6769                                 }
6770
6771                                 env->insn_idx += insn->off + 1;
6772                                 continue;
6773
6774                         } else if (opcode == BPF_EXIT) {
6775                                 if (BPF_SRC(insn->code) != BPF_K ||
6776                                     insn->imm != 0 ||
6777                                     insn->src_reg != BPF_REG_0 ||
6778                                     insn->dst_reg != BPF_REG_0 ||
6779                                     class == BPF_JMP32) {
6780                                         verbose(env, "BPF_EXIT uses reserved fields\n");
6781                                         return -EINVAL;
6782                                 }
6783
6784                                 if (env->cur_state->active_spin_lock) {
6785                                         verbose(env, "bpf_spin_unlock is missing\n");
6786                                         return -EINVAL;
6787                                 }
6788
6789                                 if (state->curframe) {
6790                                         /* exit from nested function */
6791                                         env->prev_insn_idx = env->insn_idx;
6792                                         err = prepare_func_exit(env, &env->insn_idx);
6793                                         if (err)
6794                                                 return err;
6795                                         do_print_state = true;
6796                                         continue;
6797                                 }
6798
6799                                 err = check_reference_leak(env);
6800                                 if (err)
6801                                         return err;
6802
6803                                 /* eBPF calling convetion is such that R0 is used
6804                                  * to return the value from eBPF program.
6805                                  * Make sure that it's readable at this time
6806                                  * of bpf_exit, which means that program wrote
6807                                  * something into it earlier
6808                                  */
6809                                 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
6810                                 if (err)
6811                                         return err;
6812
6813                                 if (is_pointer_value(env, BPF_REG_0)) {
6814                                         verbose(env, "R0 leaks addr as return value\n");
6815                                         return -EACCES;
6816                                 }
6817
6818                                 err = check_return_code(env);
6819                                 if (err)
6820                                         return err;
6821 process_bpf_exit:
6822                                 err = pop_stack(env, &env->prev_insn_idx,
6823                                                 &env->insn_idx);
6824                                 if (err < 0) {
6825                                         if (err != -ENOENT)
6826                                                 return err;
6827                                         break;
6828                                 } else {
6829                                         do_print_state = true;
6830                                         continue;
6831                                 }
6832                         } else {
6833                                 err = check_cond_jmp_op(env, insn, &env->insn_idx);
6834                                 if (err)
6835                                         return err;
6836                         }
6837                 } else if (class == BPF_LD) {
6838                         u8 mode = BPF_MODE(insn->code);
6839
6840                         if (mode == BPF_ABS || mode == BPF_IND) {
6841                                 err = check_ld_abs(env, insn);
6842                                 if (err)
6843                                         return err;
6844
6845                         } else if (mode == BPF_IMM) {
6846                                 err = check_ld_imm(env, insn);
6847                                 if (err)
6848                                         return err;
6849
6850                                 env->insn_idx++;
6851                                 env->insn_aux_data[env->insn_idx].seen = true;
6852                         } else {
6853                                 verbose(env, "invalid BPF_LD mode\n");
6854                                 return -EINVAL;
6855                         }
6856                 } else {
6857                         verbose(env, "unknown insn class %d\n", class);
6858                         return -EINVAL;
6859                 }
6860
6861                 env->insn_idx++;
6862         }
6863
6864         env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
6865         return 0;
6866 }
6867
6868 static int check_map_prealloc(struct bpf_map *map)
6869 {
6870         return (map->map_type != BPF_MAP_TYPE_HASH &&
6871                 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
6872                 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
6873                 !(map->map_flags & BPF_F_NO_PREALLOC);
6874 }
6875
6876 static bool is_tracing_prog_type(enum bpf_prog_type type)
6877 {
6878         switch (type) {
6879         case BPF_PROG_TYPE_KPROBE:
6880         case BPF_PROG_TYPE_TRACEPOINT:
6881         case BPF_PROG_TYPE_PERF_EVENT:
6882         case BPF_PROG_TYPE_RAW_TRACEPOINT:
6883                 return true;
6884         default:
6885                 return false;
6886         }
6887 }
6888
6889 static int check_map_prog_compatibility(struct bpf_verifier_env *env,
6890                                         struct bpf_map *map,
6891                                         struct bpf_prog *prog)
6892
6893 {
6894         /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use
6895          * preallocated hash maps, since doing memory allocation
6896          * in overflow_handler can crash depending on where nmi got
6897          * triggered.
6898          */
6899         if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
6900                 if (!check_map_prealloc(map)) {
6901                         verbose(env, "perf_event programs can only use preallocated hash map\n");
6902                         return -EINVAL;
6903                 }
6904                 if (map->inner_map_meta &&
6905                     !check_map_prealloc(map->inner_map_meta)) {
6906                         verbose(env, "perf_event programs can only use preallocated inner hash map\n");
6907                         return -EINVAL;
6908                 }
6909         }
6910
6911         if ((is_tracing_prog_type(prog->type) ||
6912              prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
6913             map_value_has_spin_lock(map)) {
6914                 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
6915                 return -EINVAL;
6916         }
6917
6918         if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
6919             !bpf_offload_prog_map_match(prog, map)) {
6920                 verbose(env, "offload device mismatch between prog and map\n");
6921                 return -EINVAL;
6922         }
6923
6924         return 0;
6925 }
6926
6927 static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
6928 {
6929         return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
6930                 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
6931 }
6932
6933 /* look for pseudo eBPF instructions that access map FDs and
6934  * replace them with actual map pointers
6935  */
6936 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
6937 {
6938         struct bpf_insn *insn = env->prog->insnsi;
6939         int insn_cnt = env->prog->len;
6940         int i, j, err;
6941
6942         err = bpf_prog_calc_tag(env->prog);
6943         if (err)
6944                 return err;
6945
6946         for (i = 0; i < insn_cnt; i++, insn++) {
6947                 if (BPF_CLASS(insn->code) == BPF_LDX &&
6948                     (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
6949                         verbose(env, "BPF_LDX uses reserved fields\n");
6950                         return -EINVAL;
6951                 }
6952
6953                 if (BPF_CLASS(insn->code) == BPF_STX &&
6954                     ((BPF_MODE(insn->code) != BPF_MEM &&
6955                       BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
6956                         verbose(env, "BPF_STX uses reserved fields\n");
6957                         return -EINVAL;
6958                 }
6959
6960                 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
6961                         struct bpf_insn_aux_data *aux;
6962                         struct bpf_map *map;
6963                         struct fd f;
6964                         u64 addr;
6965
6966                         if (i == insn_cnt - 1 || insn[1].code != 0 ||
6967                             insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
6968                             insn[1].off != 0) {
6969                                 verbose(env, "invalid bpf_ld_imm64 insn\n");
6970                                 return -EINVAL;
6971                         }
6972
6973                         if (insn[0].src_reg == 0)
6974                                 /* valid generic load 64-bit imm */
6975                                 goto next_insn;
6976
6977                         /* In final convert_pseudo_ld_imm64() step, this is
6978                          * converted into regular 64-bit imm load insn.
6979                          */
6980                         if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
6981                              insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
6982                             (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
6983                              insn[1].imm != 0)) {
6984                                 verbose(env,
6985                                         "unrecognized bpf_ld_imm64 insn\n");
6986                                 return -EINVAL;
6987                         }
6988
6989                         f = fdget(insn[0].imm);
6990                         map = __bpf_map_get(f);
6991                         if (IS_ERR(map)) {
6992                                 verbose(env, "fd %d is not pointing to valid bpf_map\n",
6993                                         insn[0].imm);
6994                                 return PTR_ERR(map);
6995                         }
6996
6997                         err = check_map_prog_compatibility(env, map, env->prog);
6998                         if (err) {
6999                                 fdput(f);
7000                                 return err;
7001                         }
7002
7003                         aux = &env->insn_aux_data[i];
7004                         if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
7005                                 addr = (unsigned long)map;
7006                         } else {
7007                                 u32 off = insn[1].imm;
7008
7009                                 if (off >= BPF_MAX_VAR_OFF) {
7010                                         verbose(env, "direct value offset of %u is not allowed\n", off);
7011                                         fdput(f);
7012                                         return -EINVAL;
7013                                 }
7014
7015                                 if (!map->ops->map_direct_value_addr) {
7016                                         verbose(env, "no direct value access support for this map type\n");
7017                                         fdput(f);
7018                                         return -EINVAL;
7019                                 }
7020
7021                                 err = map->ops->map_direct_value_addr(map, &addr, off);
7022                                 if (err) {
7023                                         verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
7024                                                 map->value_size, off);
7025                                         fdput(f);
7026                                         return err;
7027                                 }
7028
7029                                 aux->map_off = off;
7030                                 addr += off;
7031                         }
7032
7033                         insn[0].imm = (u32)addr;
7034                         insn[1].imm = addr >> 32;
7035
7036                         /* check whether we recorded this map already */
7037                         for (j = 0; j < env->used_map_cnt; j++) {
7038                                 if (env->used_maps[j] == map) {
7039                                         aux->map_index = j;
7040                                         fdput(f);
7041                                         goto next_insn;
7042                                 }
7043                         }
7044
7045                         if (env->used_map_cnt >= MAX_USED_MAPS) {
7046                                 fdput(f);
7047                                 return -E2BIG;
7048                         }
7049
7050                         /* hold the map. If the program is rejected by verifier,
7051                          * the map will be released by release_maps() or it
7052                          * will be used by the valid program until it's unloaded
7053                          * and all maps are released in free_used_maps()
7054                          */
7055                         map = bpf_map_inc(map, false);
7056                         if (IS_ERR(map)) {
7057                                 fdput(f);
7058                                 return PTR_ERR(map);
7059                         }
7060
7061                         aux->map_index = env->used_map_cnt;
7062                         env->used_maps[env->used_map_cnt++] = map;
7063
7064                         if (bpf_map_is_cgroup_storage(map) &&
7065                             bpf_cgroup_storage_assign(env->prog, map)) {
7066                                 verbose(env, "only one cgroup storage of each type is allowed\n");
7067                                 fdput(f);
7068                                 return -EBUSY;
7069                         }
7070
7071                         fdput(f);
7072 next_insn:
7073                         insn++;
7074                         i++;
7075                         continue;
7076                 }
7077
7078                 /* Basic sanity check before we invest more work here. */
7079                 if (!bpf_opcode_in_insntable(insn->code)) {
7080                         verbose(env, "unknown opcode %02x\n", insn->code);
7081                         return -EINVAL;
7082                 }
7083         }
7084
7085         /* now all pseudo BPF_LD_IMM64 instructions load valid
7086          * 'struct bpf_map *' into a register instead of user map_fd.
7087          * These pointers will be used later by verifier to validate map access.
7088          */
7089         return 0;
7090 }
7091
7092 /* drop refcnt of maps used by the rejected program */
7093 static void release_maps(struct bpf_verifier_env *env)
7094 {
7095         enum bpf_cgroup_storage_type stype;
7096         int i;
7097
7098         for_each_cgroup_storage_type(stype) {
7099                 if (!env->prog->aux->cgroup_storage[stype])
7100                         continue;
7101                 bpf_cgroup_storage_release(env->prog,
7102                         env->prog->aux->cgroup_storage[stype]);
7103         }
7104
7105         for (i = 0; i < env->used_map_cnt; i++)
7106                 bpf_map_put(env->used_maps[i]);
7107 }
7108
7109 /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */
7110 static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
7111 {
7112         struct bpf_insn *insn = env->prog->insnsi;
7113         int insn_cnt = env->prog->len;
7114         int i;
7115
7116         for (i = 0; i < insn_cnt; i++, insn++)
7117                 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
7118                         insn->src_reg = 0;
7119 }
7120
7121 /* single env->prog->insni[off] instruction was replaced with the range
7122  * insni[off, off + cnt).  Adjust corresponding insn_aux_data by copying
7123  * [0, off) and [off, end) to new locations, so the patched range stays zero
7124  */
7125 static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len,
7126                                 u32 off, u32 cnt)
7127 {
7128         struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
7129         int i;
7130
7131         if (cnt == 1)
7132                 return 0;
7133         new_data = vzalloc(array_size(prog_len,
7134                                       sizeof(struct bpf_insn_aux_data)));
7135         if (!new_data)
7136                 return -ENOMEM;
7137         memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
7138         memcpy(new_data + off + cnt - 1, old_data + off,
7139                sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
7140         for (i = off; i < off + cnt - 1; i++)
7141                 new_data[i].seen = true;
7142         env->insn_aux_data = new_data;
7143         vfree(old_data);
7144         return 0;
7145 }
7146
7147 static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
7148 {
7149         int i;
7150
7151         if (len == 1)
7152                 return;
7153         /* NOTE: fake 'exit' subprog should be updated as well. */
7154         for (i = 0; i <= env->subprog_cnt; i++) {
7155                 if (env->subprog_info[i].start <= off)
7156                         continue;
7157                 env->subprog_info[i].start += len - 1;
7158         }
7159 }
7160
7161 static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
7162                                             const struct bpf_insn *patch, u32 len)
7163 {
7164         struct bpf_prog *new_prog;
7165
7166         new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
7167         if (IS_ERR(new_prog)) {
7168                 if (PTR_ERR(new_prog) == -ERANGE)
7169                         verbose(env,
7170                                 "insn %d cannot be patched due to 16-bit range\n",
7171                                 env->insn_aux_data[off].orig_idx);
7172                 return NULL;
7173         }
7174         if (adjust_insn_aux_data(env, new_prog->len, off, len))
7175                 return NULL;
7176         adjust_subprog_starts(env, off, len);
7177         return new_prog;
7178 }
7179
7180 static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
7181                                               u32 off, u32 cnt)
7182 {
7183         int i, j;
7184
7185         /* find first prog starting at or after off (first to remove) */
7186         for (i = 0; i < env->subprog_cnt; i++)
7187                 if (env->subprog_info[i].start >= off)
7188                         break;
7189         /* find first prog starting at or after off + cnt (first to stay) */
7190         for (j = i; j < env->subprog_cnt; j++)
7191                 if (env->subprog_info[j].start >= off + cnt)
7192                         break;
7193         /* if j doesn't start exactly at off + cnt, we are just removing
7194          * the front of previous prog
7195          */
7196         if (env->subprog_info[j].start != off + cnt)
7197                 j--;
7198
7199         if (j > i) {
7200                 struct bpf_prog_aux *aux = env->prog->aux;
7201                 int move;
7202
7203                 /* move fake 'exit' subprog as well */
7204                 move = env->subprog_cnt + 1 - j;
7205
7206                 memmove(env->subprog_info + i,
7207                         env->subprog_info + j,
7208                         sizeof(*env->subprog_info) * move);
7209                 env->subprog_cnt -= j - i;
7210
7211                 /* remove func_info */
7212                 if (aux->func_info) {
7213                         move = aux->func_info_cnt - j;
7214
7215                         memmove(aux->func_info + i,
7216                                 aux->func_info + j,
7217                                 sizeof(*aux->func_info) * move);
7218                         aux->func_info_cnt -= j - i;
7219                         /* func_info->insn_off is set after all code rewrites,
7220                          * in adjust_btf_func() - no need to adjust
7221                          */
7222                 }
7223         } else {
7224                 /* convert i from "first prog to remove" to "first to adjust" */
7225                 if (env->subprog_info[i].start == off)
7226                         i++;
7227         }
7228
7229         /* update fake 'exit' subprog as well */
7230         for (; i <= env->subprog_cnt; i++)
7231                 env->subprog_info[i].start -= cnt;
7232
7233         return 0;
7234 }
7235
7236 static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
7237                                       u32 cnt)
7238 {
7239         struct bpf_prog *prog = env->prog;
7240         u32 i, l_off, l_cnt, nr_linfo;
7241         struct bpf_line_info *linfo;
7242
7243         nr_linfo = prog->aux->nr_linfo;
7244         if (!nr_linfo)
7245                 return 0;
7246
7247         linfo = prog->aux->linfo;
7248
7249         /* find first line info to remove, count lines to be removed */
7250         for (i = 0; i < nr_linfo; i++)
7251                 if (linfo[i].insn_off >= off)
7252                         break;
7253
7254         l_off = i;
7255         l_cnt = 0;
7256         for (; i < nr_linfo; i++)
7257                 if (linfo[i].insn_off < off + cnt)
7258                         l_cnt++;
7259                 else
7260                         break;
7261
7262         /* First live insn doesn't match first live linfo, it needs to "inherit"
7263          * last removed linfo.  prog is already modified, so prog->len == off
7264          * means no live instructions after (tail of the program was removed).
7265          */
7266         if (prog->len != off && l_cnt &&
7267             (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
7268                 l_cnt--;
7269                 linfo[--i].insn_off = off + cnt;
7270         }
7271
7272         /* remove the line info which refer to the removed instructions */
7273         if (l_cnt) {
7274                 memmove(linfo + l_off, linfo + i,
7275                         sizeof(*linfo) * (nr_linfo - i));
7276
7277                 prog->aux->nr_linfo -= l_cnt;
7278                 nr_linfo = prog->aux->nr_linfo;
7279         }
7280
7281         /* pull all linfo[i].insn_off >= off + cnt in by cnt */
7282         for (i = l_off; i < nr_linfo; i++)
7283                 linfo[i].insn_off -= cnt;
7284
7285         /* fix up all subprogs (incl. 'exit') which start >= off */
7286         for (i = 0; i <= env->subprog_cnt; i++)
7287                 if (env->subprog_info[i].linfo_idx > l_off) {
7288                         /* program may have started in the removed region but
7289                          * may not be fully removed
7290                          */
7291                         if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
7292                                 env->subprog_info[i].linfo_idx -= l_cnt;
7293                         else
7294                                 env->subprog_info[i].linfo_idx = l_off;
7295                 }
7296
7297         return 0;
7298 }
7299
7300 static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
7301 {
7302         struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7303         unsigned int orig_prog_len = env->prog->len;
7304         int err;
7305
7306         if (bpf_prog_is_dev_bound(env->prog->aux))
7307                 bpf_prog_offload_remove_insns(env, off, cnt);
7308
7309         err = bpf_remove_insns(env->prog, off, cnt);
7310         if (err)
7311                 return err;
7312
7313         err = adjust_subprog_starts_after_remove(env, off, cnt);
7314         if (err)
7315                 return err;
7316
7317         err = bpf_adj_linfo_after_remove(env, off, cnt);
7318         if (err)
7319                 return err;
7320
7321         memmove(aux_data + off, aux_data + off + cnt,
7322                 sizeof(*aux_data) * (orig_prog_len - off - cnt));
7323
7324         return 0;
7325 }
7326
7327 /* The verifier does more data flow analysis than llvm and will not
7328  * explore branches that are dead at run time. Malicious programs can
7329  * have dead code too. Therefore replace all dead at-run-time code
7330  * with 'ja -1'.
7331  *
7332  * Just nops are not optimal, e.g. if they would sit at the end of the
7333  * program and through another bug we would manage to jump there, then
7334  * we'd execute beyond program memory otherwise. Returning exception
7335  * code also wouldn't work since we can have subprogs where the dead
7336  * code could be located.
7337  */
7338 static void sanitize_dead_code(struct bpf_verifier_env *env)
7339 {
7340         struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7341         struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
7342         struct bpf_insn *insn = env->prog->insnsi;
7343         const int insn_cnt = env->prog->len;
7344         int i;
7345
7346         for (i = 0; i < insn_cnt; i++) {
7347                 if (aux_data[i].seen)
7348                         continue;
7349                 memcpy(insn + i, &trap, sizeof(trap));
7350         }
7351 }
7352
7353 static bool insn_is_cond_jump(u8 code)
7354 {
7355         u8 op;
7356
7357         if (BPF_CLASS(code) == BPF_JMP32)
7358                 return true;
7359
7360         if (BPF_CLASS(code) != BPF_JMP)
7361                 return false;
7362
7363         op = BPF_OP(code);
7364         return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
7365 }
7366
7367 static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
7368 {
7369         struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7370         struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7371         struct bpf_insn *insn = env->prog->insnsi;
7372         const int insn_cnt = env->prog->len;
7373         int i;
7374
7375         for (i = 0; i < insn_cnt; i++, insn++) {
7376                 if (!insn_is_cond_jump(insn->code))
7377                         continue;
7378
7379                 if (!aux_data[i + 1].seen)
7380                         ja.off = insn->off;
7381                 else if (!aux_data[i + 1 + insn->off].seen)
7382                         ja.off = 0;
7383                 else
7384                         continue;
7385
7386                 if (bpf_prog_is_dev_bound(env->prog->aux))
7387                         bpf_prog_offload_replace_insn(env, i, &ja);
7388
7389                 memcpy(insn, &ja, sizeof(ja));
7390         }
7391 }
7392
7393 static int opt_remove_dead_code(struct bpf_verifier_env *env)
7394 {
7395         struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
7396         int insn_cnt = env->prog->len;
7397         int i, err;
7398
7399         for (i = 0; i < insn_cnt; i++) {
7400                 int j;
7401
7402                 j = 0;
7403                 while (i + j < insn_cnt && !aux_data[i + j].seen)
7404                         j++;
7405                 if (!j)
7406                         continue;
7407
7408                 err = verifier_remove_insns(env, i, j);
7409                 if (err)
7410                         return err;
7411                 insn_cnt = env->prog->len;
7412         }
7413
7414         return 0;
7415 }
7416
7417 static int opt_remove_nops(struct bpf_verifier_env *env)
7418 {
7419         const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
7420         struct bpf_insn *insn = env->prog->insnsi;
7421         int insn_cnt = env->prog->len;
7422         int i, err;
7423
7424         for (i = 0; i < insn_cnt; i++) {
7425                 if (memcmp(&insn[i], &ja, sizeof(ja)))
7426                         continue;
7427
7428                 err = verifier_remove_insns(env, i, 1);
7429                 if (err)
7430                         return err;
7431                 insn_cnt--;
7432                 i--;
7433         }
7434
7435         return 0;
7436 }
7437
7438 /* convert load instructions that access fields of a context type into a
7439  * sequence of instructions that access fields of the underlying structure:
7440  *     struct __sk_buff    -> struct sk_buff
7441  *     struct bpf_sock_ops -> struct sock
7442  */
7443 static int convert_ctx_accesses(struct bpf_verifier_env *env)
7444 {
7445         const struct bpf_verifier_ops *ops = env->ops;
7446         int i, cnt, size, ctx_field_size, delta = 0;
7447         const int insn_cnt = env->prog->len;
7448         struct bpf_insn insn_buf[16], *insn;
7449         u32 target_size, size_default, off;
7450         struct bpf_prog *new_prog;
7451         enum bpf_access_type type;
7452         bool is_narrower_load;
7453
7454         if (ops->gen_prologue || env->seen_direct_write) {
7455                 if (!ops->gen_prologue) {
7456                         verbose(env, "bpf verifier is misconfigured\n");
7457                         return -EINVAL;
7458                 }
7459                 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
7460                                         env->prog);
7461                 if (cnt >= ARRAY_SIZE(insn_buf)) {
7462                         verbose(env, "bpf verifier is misconfigured\n");
7463                         return -EINVAL;
7464                 } else if (cnt) {
7465                         new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
7466                         if (!new_prog)
7467                                 return -ENOMEM;
7468
7469                         env->prog = new_prog;
7470                         delta += cnt - 1;
7471                 }
7472         }
7473
7474         if (bpf_prog_is_dev_bound(env->prog->aux))
7475                 return 0;
7476
7477         insn = env->prog->insnsi + delta;
7478
7479         for (i = 0; i < insn_cnt; i++, insn++) {
7480                 bpf_convert_ctx_access_t convert_ctx_access;
7481
7482                 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
7483                     insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
7484                     insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
7485                     insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
7486                         type = BPF_READ;
7487                 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
7488                          insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
7489                          insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
7490                          insn->code == (BPF_STX | BPF_MEM | BPF_DW))
7491                         type = BPF_WRITE;
7492                 else
7493                         continue;
7494
7495                 if (type == BPF_WRITE &&
7496                     env->insn_aux_data[i + delta].sanitize_stack_off) {
7497                         struct bpf_insn patch[] = {
7498                                 /* Sanitize suspicious stack slot with zero.
7499                                  * There are no memory dependencies for this store,
7500                                  * since it's only using frame pointer and immediate
7501                                  * constant of zero
7502                                  */
7503                                 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
7504                                            env->insn_aux_data[i + delta].sanitize_stack_off,
7505                                            0),
7506                                 /* the original STX instruction will immediately
7507                                  * overwrite the same stack slot with appropriate value
7508                                  */
7509                                 *insn,
7510                         };
7511
7512                         cnt = ARRAY_SIZE(patch);
7513                         new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
7514                         if (!new_prog)
7515                                 return -ENOMEM;
7516
7517                         delta    += cnt - 1;
7518                         env->prog = new_prog;
7519                         insn      = new_prog->insnsi + i + delta;
7520                         continue;
7521                 }
7522
7523                 switch (env->insn_aux_data[i + delta].ptr_type) {
7524                 case PTR_TO_CTX:
7525                         if (!ops->convert_ctx_access)
7526                                 continue;
7527                         convert_ctx_access = ops->convert_ctx_access;
7528                         break;
7529                 case PTR_TO_SOCKET:
7530                 case PTR_TO_SOCK_COMMON:
7531                         convert_ctx_access = bpf_sock_convert_ctx_access;
7532                         break;
7533                 case PTR_TO_TCP_SOCK:
7534                         convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
7535                         break;
7536                 default:
7537                         continue;
7538                 }
7539
7540                 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
7541                 size = BPF_LDST_BYTES(insn);
7542
7543                 /* If the read access is a narrower load of the field,
7544                  * convert to a 4/8-byte load, to minimum program type specific
7545                  * convert_ctx_access changes. If conversion is successful,
7546                  * we will apply proper mask to the result.
7547                  */
7548                 is_narrower_load = size < ctx_field_size;
7549                 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
7550                 off = insn->off;
7551                 if (is_narrower_load) {
7552                         u8 size_code;
7553
7554                         if (type == BPF_WRITE) {
7555                                 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
7556                                 return -EINVAL;
7557                         }
7558
7559                         size_code = BPF_H;
7560                         if (ctx_field_size == 4)
7561                                 size_code = BPF_W;
7562                         else if (ctx_field_size == 8)
7563                                 size_code = BPF_DW;
7564
7565                         insn->off = off & ~(size_default - 1);
7566                         insn->code = BPF_LDX | BPF_MEM | size_code;
7567                 }
7568
7569                 target_size = 0;
7570                 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
7571                                          &target_size);
7572                 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
7573                     (ctx_field_size && !target_size)) {
7574                         verbose(env, "bpf verifier is misconfigured\n");
7575                         return -EINVAL;
7576                 }
7577
7578                 if (is_narrower_load && size < target_size) {
7579                         u8 shift = (off & (size_default - 1)) * 8;
7580
7581                         if (ctx_field_size <= 4) {
7582                                 if (shift)
7583                                         insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
7584                                                                         insn->dst_reg,
7585                                                                         shift);
7586                                 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
7587                                                                 (1 << size * 8) - 1);
7588                         } else {
7589                                 if (shift)
7590                                         insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
7591                                                                         insn->dst_reg,
7592                                                                         shift);
7593                                 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
7594                                                                 (1ULL << size * 8) - 1);
7595                         }
7596                 }
7597
7598                 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7599                 if (!new_prog)
7600                         return -ENOMEM;
7601
7602                 delta += cnt - 1;
7603
7604                 /* keep walking new program and skip insns we just inserted */
7605                 env->prog = new_prog;
7606                 insn      = new_prog->insnsi + i + delta;
7607         }
7608
7609         return 0;
7610 }
7611
7612 static int jit_subprogs(struct bpf_verifier_env *env)
7613 {
7614         struct bpf_prog *prog = env->prog, **func, *tmp;
7615         int i, j, subprog_start, subprog_end = 0, len, subprog;
7616         struct bpf_insn *insn;
7617         void *old_bpf_func;
7618         int err;
7619
7620         if (env->subprog_cnt <= 1)
7621                 return 0;
7622
7623         for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7624                 if (insn->code != (BPF_JMP | BPF_CALL) ||
7625                     insn->src_reg != BPF_PSEUDO_CALL)
7626                         continue;
7627                 /* Upon error here we cannot fall back to interpreter but
7628                  * need a hard reject of the program. Thus -EFAULT is
7629                  * propagated in any case.
7630                  */
7631                 subprog = find_subprog(env, i + insn->imm + 1);
7632                 if (subprog < 0) {
7633                         WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
7634                                   i + insn->imm + 1);
7635                         return -EFAULT;
7636                 }
7637                 /* temporarily remember subprog id inside insn instead of
7638                  * aux_data, since next loop will split up all insns into funcs
7639                  */
7640                 insn->off = subprog;
7641                 /* remember original imm in case JIT fails and fallback
7642                  * to interpreter will be needed
7643                  */
7644                 env->insn_aux_data[i].call_imm = insn->imm;
7645                 /* point imm to __bpf_call_base+1 from JITs point of view */
7646                 insn->imm = 1;
7647         }
7648
7649         err = bpf_prog_alloc_jited_linfo(prog);
7650         if (err)
7651                 goto out_undo_insn;
7652
7653         err = -ENOMEM;
7654         func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
7655         if (!func)
7656                 goto out_undo_insn;
7657
7658         for (i = 0; i < env->subprog_cnt; i++) {
7659                 subprog_start = subprog_end;
7660                 subprog_end = env->subprog_info[i + 1].start;
7661
7662                 len = subprog_end - subprog_start;
7663                 /* BPF_PROG_RUN doesn't call subprogs directly,
7664                  * hence main prog stats include the runtime of subprogs.
7665                  * subprogs don't have IDs and not reachable via prog_get_next_id
7666                  * func[i]->aux->stats will never be accessed and stays NULL
7667                  */
7668                 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
7669                 if (!func[i])
7670                         goto out_free;
7671                 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
7672                        len * sizeof(struct bpf_insn));
7673                 func[i]->type = prog->type;
7674                 func[i]->len = len;
7675                 if (bpf_prog_calc_tag(func[i]))
7676                         goto out_free;
7677                 func[i]->is_func = 1;
7678                 func[i]->aux->func_idx = i;
7679                 /* the btf and func_info will be freed only at prog->aux */
7680                 func[i]->aux->btf = prog->aux->btf;
7681                 func[i]->aux->func_info = prog->aux->func_info;
7682
7683                 /* Use bpf_prog_F_tag to indicate functions in stack traces.
7684                  * Long term would need debug info to populate names
7685                  */
7686                 func[i]->aux->name[0] = 'F';
7687                 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
7688                 func[i]->jit_requested = 1;
7689                 func[i]->aux->linfo = prog->aux->linfo;
7690                 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
7691                 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
7692                 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
7693                 func[i] = bpf_int_jit_compile(func[i]);
7694                 if (!func[i]->jited) {
7695                         err = -ENOTSUPP;
7696                         goto out_free;
7697                 }
7698                 cond_resched();
7699         }
7700         /* at this point all bpf functions were successfully JITed
7701          * now populate all bpf_calls with correct addresses and
7702          * run last pass of JIT
7703          */
7704         for (i = 0; i < env->subprog_cnt; i++) {
7705                 insn = func[i]->insnsi;
7706                 for (j = 0; j < func[i]->len; j++, insn++) {
7707                         if (insn->code != (BPF_JMP | BPF_CALL) ||
7708                             insn->src_reg != BPF_PSEUDO_CALL)
7709                                 continue;
7710                         subprog = insn->off;
7711                         insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
7712                                     __bpf_call_base;
7713                 }
7714
7715                 /* we use the aux data to keep a list of the start addresses
7716                  * of the JITed images for each function in the program
7717                  *
7718                  * for some architectures, such as powerpc64, the imm field
7719                  * might not be large enough to hold the offset of the start
7720                  * address of the callee's JITed image from __bpf_call_base
7721                  *
7722                  * in such cases, we can lookup the start address of a callee
7723                  * by using its subprog id, available from the off field of
7724                  * the call instruction, as an index for this list
7725                  */
7726                 func[i]->aux->func = func;
7727                 func[i]->aux->func_cnt = env->subprog_cnt;
7728         }
7729         for (i = 0; i < env->subprog_cnt; i++) {
7730                 old_bpf_func = func[i]->bpf_func;
7731                 tmp = bpf_int_jit_compile(func[i]);
7732                 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
7733                         verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
7734                         err = -ENOTSUPP;
7735                         goto out_free;
7736                 }
7737                 cond_resched();
7738         }
7739
7740         /* finally lock prog and jit images for all functions and
7741          * populate kallsysm
7742          */
7743         for (i = 0; i < env->subprog_cnt; i++) {
7744                 bpf_prog_lock_ro(func[i]);
7745                 bpf_prog_kallsyms_add(func[i]);
7746         }
7747
7748         /* Last step: make now unused interpreter insns from main
7749          * prog consistent for later dump requests, so they can
7750          * later look the same as if they were interpreted only.
7751          */
7752         for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7753                 if (insn->code != (BPF_JMP | BPF_CALL) ||
7754                     insn->src_reg != BPF_PSEUDO_CALL)
7755                         continue;
7756                 insn->off = env->insn_aux_data[i].call_imm;
7757                 subprog = find_subprog(env, i + insn->off + 1);
7758                 insn->imm = subprog;
7759         }
7760
7761         prog->jited = 1;
7762         prog->bpf_func = func[0]->bpf_func;
7763         prog->aux->func = func;
7764         prog->aux->func_cnt = env->subprog_cnt;
7765         bpf_prog_free_unused_jited_linfo(prog);
7766         return 0;
7767 out_free:
7768         for (i = 0; i < env->subprog_cnt; i++)
7769                 if (func[i])
7770                         bpf_jit_free(func[i]);
7771         kfree(func);
7772 out_undo_insn:
7773         /* cleanup main prog to be interpreted */
7774         prog->jit_requested = 0;
7775         for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
7776                 if (insn->code != (BPF_JMP | BPF_CALL) ||
7777                     insn->src_reg != BPF_PSEUDO_CALL)
7778                         continue;
7779                 insn->off = 0;
7780                 insn->imm = env->insn_aux_data[i].call_imm;
7781         }
7782         bpf_prog_free_jited_linfo(prog);
7783         return err;
7784 }
7785
7786 static int fixup_call_args(struct bpf_verifier_env *env)
7787 {
7788 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
7789         struct bpf_prog *prog = env->prog;
7790         struct bpf_insn *insn = prog->insnsi;
7791         int i, depth;
7792 #endif
7793         int err = 0;
7794
7795         if (env->prog->jit_requested &&
7796             !bpf_prog_is_dev_bound(env->prog->aux)) {
7797                 err = jit_subprogs(env);
7798                 if (err == 0)
7799                         return 0;
7800                 if (err == -EFAULT)
7801                         return err;
7802         }
7803 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
7804         for (i = 0; i < prog->len; i++, insn++) {
7805                 if (insn->code != (BPF_JMP | BPF_CALL) ||
7806                     insn->src_reg != BPF_PSEUDO_CALL)
7807                         continue;
7808                 depth = get_callee_stack_depth(env, insn, i);
7809                 if (depth < 0)
7810                         return depth;
7811                 bpf_patch_call_args(insn, depth);
7812         }
7813         err = 0;
7814 #endif
7815         return err;
7816 }
7817
7818 /* fixup insn->imm field of bpf_call instructions
7819  * and inline eligible helpers as explicit sequence of BPF instructions
7820  *
7821  * this function is called after eBPF program passed verification
7822  */
7823 static int fixup_bpf_calls(struct bpf_verifier_env *env)
7824 {
7825         struct bpf_prog *prog = env->prog;
7826         struct bpf_insn *insn = prog->insnsi;
7827         const struct bpf_func_proto *fn;
7828         const int insn_cnt = prog->len;
7829         const struct bpf_map_ops *ops;
7830         struct bpf_insn_aux_data *aux;
7831         struct bpf_insn insn_buf[16];
7832         struct bpf_prog *new_prog;
7833         struct bpf_map *map_ptr;
7834         int i, cnt, delta = 0;
7835
7836         for (i = 0; i < insn_cnt; i++, insn++) {
7837                 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
7838                     insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7839                     insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
7840                     insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7841                         bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
7842                         struct bpf_insn mask_and_div[] = {
7843                                 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7844                                 /* Rx div 0 -> 0 */
7845                                 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
7846                                 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
7847                                 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
7848                                 *insn,
7849                         };
7850                         struct bpf_insn mask_and_mod[] = {
7851                                 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
7852                                 /* Rx mod 0 -> Rx */
7853                                 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
7854                                 *insn,
7855                         };
7856                         struct bpf_insn *patchlet;
7857
7858                         if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
7859                             insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
7860                                 patchlet = mask_and_div + (is64 ? 1 : 0);
7861                                 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
7862                         } else {
7863                                 patchlet = mask_and_mod + (is64 ? 1 : 0);
7864                                 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
7865                         }
7866
7867                         new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
7868                         if (!new_prog)
7869                                 return -ENOMEM;
7870
7871                         delta    += cnt - 1;
7872                         env->prog = prog = new_prog;
7873                         insn      = new_prog->insnsi + i + delta;
7874                         continue;
7875                 }
7876
7877                 if (BPF_CLASS(insn->code) == BPF_LD &&
7878                     (BPF_MODE(insn->code) == BPF_ABS ||
7879                      BPF_MODE(insn->code) == BPF_IND)) {
7880                         cnt = env->ops->gen_ld_abs(insn, insn_buf);
7881                         if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
7882                                 verbose(env, "bpf verifier is misconfigured\n");
7883                                 return -EINVAL;
7884                         }
7885
7886                         new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7887                         if (!new_prog)
7888                                 return -ENOMEM;
7889
7890                         delta    += cnt - 1;
7891                         env->prog = prog = new_prog;
7892                         insn      = new_prog->insnsi + i + delta;
7893                         continue;
7894                 }
7895
7896                 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
7897                     insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
7898                         const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
7899                         const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
7900                         struct bpf_insn insn_buf[16];
7901                         struct bpf_insn *patch = &insn_buf[0];
7902                         bool issrc, isneg;
7903                         u32 off_reg;
7904
7905                         aux = &env->insn_aux_data[i + delta];
7906                         if (!aux->alu_state ||
7907                             aux->alu_state == BPF_ALU_NON_POINTER)
7908                                 continue;
7909
7910                         isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
7911                         issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
7912                                 BPF_ALU_SANITIZE_SRC;
7913
7914                         off_reg = issrc ? insn->src_reg : insn->dst_reg;
7915                         if (isneg)
7916                                 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7917                         *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
7918                         *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
7919                         *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
7920                         *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
7921                         *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
7922                         if (issrc) {
7923                                 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
7924                                                          off_reg);
7925                                 insn->src_reg = BPF_REG_AX;
7926                         } else {
7927                                 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
7928                                                          BPF_REG_AX);
7929                         }
7930                         if (isneg)
7931                                 insn->code = insn->code == code_add ?
7932                                              code_sub : code_add;
7933                         *patch++ = *insn;
7934                         if (issrc && isneg)
7935                                 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
7936                         cnt = patch - insn_buf;
7937
7938                         new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
7939                         if (!new_prog)
7940                                 return -ENOMEM;
7941
7942                         delta    += cnt - 1;
7943                         env->prog = prog = new_prog;
7944                         insn      = new_prog->insnsi + i + delta;
7945                         continue;
7946                 }
7947
7948                 if (insn->code != (BPF_JMP | BPF_CALL))
7949                         continue;
7950                 if (insn->src_reg == BPF_PSEUDO_CALL)
7951                         continue;
7952
7953                 if (insn->imm == BPF_FUNC_get_route_realm)
7954                         prog->dst_needed = 1;
7955                 if (insn->imm == BPF_FUNC_get_prandom_u32)
7956                         bpf_user_rnd_init_once();
7957                 if (insn->imm == BPF_FUNC_override_return)
7958                         prog->kprobe_override = 1;
7959                 if (insn->imm == BPF_FUNC_tail_call) {
7960                         /* If we tail call into other programs, we
7961                          * cannot make any assumptions since they can
7962                          * be replaced dynamically during runtime in
7963                          * the program array.
7964                          */
7965                         prog->cb_access = 1;
7966                         env->prog->aux->stack_depth = MAX_BPF_STACK;
7967                         env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
7968
7969                         /* mark bpf_tail_call as different opcode to avoid
7970                          * conditional branch in the interpeter for every normal
7971                          * call and to prevent accidental JITing by JIT compiler
7972                          * that doesn't support bpf_tail_call yet
7973                          */
7974                         insn->imm = 0;
7975                         insn->code = BPF_JMP | BPF_TAIL_CALL;
7976
7977                         aux = &env->insn_aux_data[i + delta];
7978                         if (!bpf_map_ptr_unpriv(aux))
7979                                 continue;
7980
7981                         /* instead of changing every JIT dealing with tail_call
7982                          * emit two extra insns:
7983                          * if (index >= max_entries) goto out;
7984                          * index &= array->index_mask;
7985                          * to avoid out-of-bounds cpu speculation
7986                          */
7987                         if (bpf_map_ptr_poisoned(aux)) {
7988                                 verbose(env, "tail_call abusing map_ptr\n");
7989                                 return -EINVAL;
7990                         }
7991
7992                         map_ptr = BPF_MAP_PTR(aux->map_state);
7993                         insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
7994                                                   map_ptr->max_entries, 2);
7995                         insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
7996                                                     container_of(map_ptr,
7997                                                                  struct bpf_array,
7998                                                                  map)->index_mask);
7999                         insn_buf[2] = *insn;
8000                         cnt = 3;
8001                         new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8002                         if (!new_prog)
8003                                 return -ENOMEM;
8004
8005                         delta    += cnt - 1;
8006                         env->prog = prog = new_prog;
8007                         insn      = new_prog->insnsi + i + delta;
8008                         continue;
8009                 }
8010
8011                 /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup
8012                  * and other inlining handlers are currently limited to 64 bit
8013                  * only.
8014                  */
8015                 if (prog->jit_requested && BITS_PER_LONG == 64 &&
8016                     (insn->imm == BPF_FUNC_map_lookup_elem ||
8017                      insn->imm == BPF_FUNC_map_update_elem ||
8018                      insn->imm == BPF_FUNC_map_delete_elem ||
8019                      insn->imm == BPF_FUNC_map_push_elem   ||
8020                      insn->imm == BPF_FUNC_map_pop_elem    ||
8021                      insn->imm == BPF_FUNC_map_peek_elem)) {
8022                         aux = &env->insn_aux_data[i + delta];
8023                         if (bpf_map_ptr_poisoned(aux))
8024                                 goto patch_call_imm;
8025
8026                         map_ptr = BPF_MAP_PTR(aux->map_state);
8027                         ops = map_ptr->ops;
8028                         if (insn->imm == BPF_FUNC_map_lookup_elem &&
8029                             ops->map_gen_lookup) {
8030                                 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
8031                                 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
8032                                         verbose(env, "bpf verifier is misconfigured\n");
8033                                         return -EINVAL;
8034                                 }
8035
8036                                 new_prog = bpf_patch_insn_data(env, i + delta,
8037                                                                insn_buf, cnt);
8038                                 if (!new_prog)
8039                                         return -ENOMEM;
8040
8041                                 delta    += cnt - 1;
8042                                 env->prog = prog = new_prog;
8043                                 insn      = new_prog->insnsi + i + delta;
8044                                 continue;
8045                         }
8046
8047                         BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
8048                                      (void *(*)(struct bpf_map *map, void *key))NULL));
8049                         BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
8050                                      (int (*)(struct bpf_map *map, void *key))NULL));
8051                         BUILD_BUG_ON(!__same_type(ops->map_update_elem,
8052                                      (int (*)(struct bpf_map *map, void *key, void *value,
8053                                               u64 flags))NULL));
8054                         BUILD_BUG_ON(!__same_type(ops->map_push_elem,
8055                                      (int (*)(struct bpf_map *map, void *value,
8056                                               u64 flags))NULL));
8057                         BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
8058                                      (int (*)(struct bpf_map *map, void *value))NULL));
8059                         BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
8060                                      (int (*)(struct bpf_map *map, void *value))NULL));
8061
8062                         switch (insn->imm) {
8063                         case BPF_FUNC_map_lookup_elem:
8064                                 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
8065                                             __bpf_call_base;
8066                                 continue;
8067                         case BPF_FUNC_map_update_elem:
8068                                 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
8069                                             __bpf_call_base;
8070                                 continue;
8071                         case BPF_FUNC_map_delete_elem:
8072                                 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
8073                                             __bpf_call_base;
8074                                 continue;
8075                         case BPF_FUNC_map_push_elem:
8076                                 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
8077                                             __bpf_call_base;
8078                                 continue;
8079                         case BPF_FUNC_map_pop_elem:
8080                                 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
8081                                             __bpf_call_base;
8082                                 continue;
8083                         case BPF_FUNC_map_peek_elem:
8084                                 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
8085                                             __bpf_call_base;
8086                                 continue;
8087                         }
8088
8089                         goto patch_call_imm;
8090                 }
8091
8092 patch_call_imm:
8093                 fn = env->ops->get_func_proto(insn->imm, env->prog);
8094                 /* all functions that have prototype and verifier allowed
8095                  * programs to call them, must be real in-kernel functions
8096                  */
8097                 if (!fn->func) {
8098                         verbose(env,
8099                                 "kernel subsystem misconfigured func %s#%d\n",
8100                                 func_id_name(insn->imm), insn->imm);
8101                         return -EFAULT;
8102                 }
8103                 insn->imm = fn->func - __bpf_call_base;
8104         }
8105
8106         return 0;
8107 }
8108
8109 static void free_states(struct bpf_verifier_env *env)
8110 {
8111         struct bpf_verifier_state_list *sl, *sln;
8112         int i;
8113
8114         sl = env->free_list;
8115         while (sl) {
8116                 sln = sl->next;
8117                 free_verifier_state(&sl->state, false);
8118                 kfree(sl);
8119                 sl = sln;
8120         }
8121
8122         if (!env->explored_states)
8123                 return;
8124
8125         for (i = 0; i < env->prog->len; i++) {
8126                 sl = env->explored_states[i];
8127
8128                 if (sl)
8129                         while (sl != STATE_LIST_MARK) {
8130                                 sln = sl->next;
8131                                 free_verifier_state(&sl->state, false);
8132                                 kfree(sl);
8133                                 sl = sln;
8134                         }
8135         }
8136
8137         kvfree(env->explored_states);
8138 }
8139
8140 static void print_verification_stats(struct bpf_verifier_env *env)
8141 {
8142         int i;
8143
8144         if (env->log.level & BPF_LOG_STATS) {
8145                 verbose(env, "verification time %lld usec\n",
8146                         div_u64(env->verification_time, 1000));
8147                 verbose(env, "stack depth ");
8148                 for (i = 0; i < env->subprog_cnt; i++) {
8149                         u32 depth = env->subprog_info[i].stack_depth;
8150
8151                         verbose(env, "%d", depth);
8152                         if (i + 1 < env->subprog_cnt)
8153                                 verbose(env, "+");
8154                 }
8155                 verbose(env, "\n");
8156         }
8157         verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
8158                 "total_states %d peak_states %d mark_read %d\n",
8159                 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
8160                 env->max_states_per_insn, env->total_states,
8161                 env->peak_states, env->longest_mark_read_walk);
8162 }
8163
8164 int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
8165               union bpf_attr __user *uattr)
8166 {
8167         u64 start_time = ktime_get_ns();
8168         struct bpf_verifier_env *env;
8169         struct bpf_verifier_log *log;
8170         int i, len, ret = -EINVAL;
8171         bool is_priv;
8172
8173         /* no program is valid */
8174         if (ARRAY_SIZE(bpf_verifier_ops) == 0)
8175                 return -EINVAL;
8176
8177         /* 'struct bpf_verifier_env' can be global, but since it's not small,
8178          * allocate/free it every time bpf_check() is called
8179          */
8180         env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
8181         if (!env)
8182                 return -ENOMEM;
8183         log = &env->log;
8184
8185         len = (*prog)->len;
8186         env->insn_aux_data =
8187                 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
8188         ret = -ENOMEM;
8189         if (!env->insn_aux_data)
8190                 goto err_free_env;
8191         for (i = 0; i < len; i++)
8192                 env->insn_aux_data[i].orig_idx = i;
8193         env->prog = *prog;
8194         env->ops = bpf_verifier_ops[env->prog->type];
8195         is_priv = capable(CAP_SYS_ADMIN);
8196
8197         /* grab the mutex to protect few globals used by verifier */
8198         if (!is_priv)
8199                 mutex_lock(&bpf_verifier_lock);
8200
8201         if (attr->log_level || attr->log_buf || attr->log_size) {
8202                 /* user requested verbose verifier output
8203                  * and supplied buffer to store the verification trace
8204                  */
8205                 log->level = attr->log_level;
8206                 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
8207                 log->len_total = attr->log_size;
8208
8209                 ret = -EINVAL;
8210                 /* log attributes have to be sane */
8211                 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
8212                     !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
8213                         goto err_unlock;
8214         }
8215
8216         env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
8217         if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
8218                 env->strict_alignment = true;
8219         if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
8220                 env->strict_alignment = false;
8221
8222         env->allow_ptr_leaks = is_priv;
8223
8224         ret = replace_map_fd_with_map_ptr(env);
8225         if (ret < 0)
8226                 goto skip_full_check;
8227
8228         if (bpf_prog_is_dev_bound(env->prog->aux)) {
8229                 ret = bpf_prog_offload_verifier_prep(env->prog);
8230                 if (ret)
8231                         goto skip_full_check;
8232         }
8233
8234         env->explored_states = kvcalloc(env->prog->len,
8235                                        sizeof(struct bpf_verifier_state_list *),
8236                                        GFP_USER);
8237         ret = -ENOMEM;
8238         if (!env->explored_states)
8239                 goto skip_full_check;
8240
8241         ret = check_subprogs(env);
8242         if (ret < 0)
8243                 goto skip_full_check;
8244
8245         ret = check_btf_info(env, attr, uattr);
8246         if (ret < 0)
8247                 goto skip_full_check;
8248
8249         ret = check_cfg(env);
8250         if (ret < 0)
8251                 goto skip_full_check;
8252
8253         ret = do_check(env);
8254         if (env->cur_state) {
8255                 free_verifier_state(env->cur_state, true);
8256                 env->cur_state = NULL;
8257         }
8258
8259         if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
8260                 ret = bpf_prog_offload_finalize(env);
8261
8262 skip_full_check:
8263         while (!pop_stack(env, NULL, NULL));
8264         free_states(env);
8265
8266         if (ret == 0)
8267                 ret = check_max_stack_depth(env);
8268
8269         /* instruction rewrites happen after this point */
8270         if (is_priv) {
8271                 if (ret == 0)
8272                         opt_hard_wire_dead_code_branches(env);
8273                 if (ret == 0)
8274                         ret = opt_remove_dead_code(env);
8275                 if (ret == 0)
8276                         ret = opt_remove_nops(env);
8277         } else {
8278                 if (ret == 0)
8279                         sanitize_dead_code(env);
8280         }
8281
8282         if (ret == 0)
8283                 /* program is valid, convert *(u32*)(ctx + off) accesses */
8284                 ret = convert_ctx_accesses(env);
8285
8286         if (ret == 0)
8287                 ret = fixup_bpf_calls(env);
8288
8289         if (ret == 0)
8290                 ret = fixup_call_args(env);
8291
8292         env->verification_time = ktime_get_ns() - start_time;
8293         print_verification_stats(env);
8294
8295         if (log->level && bpf_verifier_log_full(log))
8296                 ret = -ENOSPC;
8297         if (log->level && !log->ubuf) {
8298                 ret = -EFAULT;
8299                 goto err_release_maps;
8300         }
8301
8302         if (ret == 0 && env->used_map_cnt) {
8303                 /* if program passed verifier, update used_maps in bpf_prog_info */
8304                 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
8305                                                           sizeof(env->used_maps[0]),
8306                                                           GFP_KERNEL);
8307
8308                 if (!env->prog->aux->used_maps) {
8309                         ret = -ENOMEM;
8310                         goto err_release_maps;
8311                 }
8312
8313                 memcpy(env->prog->aux->used_maps, env->used_maps,
8314                        sizeof(env->used_maps[0]) * env->used_map_cnt);
8315                 env->prog->aux->used_map_cnt = env->used_map_cnt;
8316
8317                 /* program is valid. Convert pseudo bpf_ld_imm64 into generic
8318                  * bpf_ld_imm64 instructions
8319                  */
8320                 convert_pseudo_ld_imm64(env);
8321         }
8322
8323         if (ret == 0)
8324                 adjust_btf_func(env);
8325
8326 err_release_maps:
8327         if (!env->prog->aux->used_maps)
8328                 /* if we didn't copy map pointers into bpf_prog_info, release
8329                  * them now. Otherwise free_used_maps() will release them.
8330                  */
8331                 release_maps(env);
8332         *prog = env->prog;
8333 err_unlock:
8334         if (!is_priv)
8335                 mutex_unlock(&bpf_verifier_lock);
8336         vfree(env->insn_aux_data);
8337 err_free_env:
8338         kfree(env);
8339         return ret;
8340 }