]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blobdiff - kernel/bpf/verifier.c
tipc: eliminate unnecessary probing
[mirror_ubuntu-hirsute-kernel.git] / kernel / bpf / verifier.c
index c48ca2a34b5e131420f4795c4a0eaf9d9a64861d..ab5aa549766650cdbbe6e42345ef90895f7d3e3b 100644 (file)
 #include <linux/vmalloc.h>
 #include <linux/stringify.h>
 
+#include "disasm.h"
+
+static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
+#define BPF_PROG_TYPE(_id, _name) \
+       [_id] = & _name ## _verifier_ops,
+#define BPF_MAP_TYPE(_id, _ops)
+#include <linux/bpf_types.h>
+#undef BPF_PROG_TYPE
+#undef BPF_MAP_TYPE
+};
+
 /* bpf_check() is a static code analyzer that walks eBPF program
  * instruction by instruction and updates register/stack state.
  * All paths of conditional branches are analyzed until 'bpf_exit' insn.
@@ -153,28 +164,42 @@ struct bpf_call_arg_meta {
        int access_size;
 };
 
-/* verbose verifier prints what it's seeing
- * bpf_check() is called under lock, so no race to access these global vars
- */
-static u32 log_level, log_size, log_len;
-static char *log_buf;
-
 static DEFINE_MUTEX(bpf_verifier_lock);
 
 /* log_level controls verbosity level of eBPF verifier.
  * verbose() is used to dump the verification trace to the log, so the user
  * can figure out what's wrong with the program
  */
-static __printf(1, 2) void verbose(const char *fmt, ...)
+static __printf(2, 3) void verbose(struct bpf_verifier_env *env,
+                                  const char *fmt, ...)
 {
+       struct bpf_verifer_log *log = &env->log;
+       unsigned int n;
        va_list args;
 
-       if (log_level == 0 || log_len >= log_size - 1)
+       if (!log->level || !log->ubuf || bpf_verifier_log_full(log))
                return;
 
        va_start(args, fmt);
-       log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args);
+       n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
        va_end(args);
+
+       WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
+                 "verifier log line truncated - local buffer too short\n");
+
+       n = min(log->len_total - log->len_used - 1, n);
+       log->kbuf[n] = '\0';
+
+       if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
+               log->len_used += n;
+       else
+               log->ubuf = NULL;
+}
+
+static bool type_is_pkt_pointer(enum bpf_reg_type type)
+{
+       return type == PTR_TO_PACKET ||
+              type == PTR_TO_PACKET_META;
 }
 
 /* string representation of 'enum bpf_reg_type' */
@@ -187,26 +212,12 @@ static const char * const reg_type_str[] = {
        [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
        [PTR_TO_STACK]          = "fp",
        [PTR_TO_PACKET]         = "pkt",
+       [PTR_TO_PACKET_META]    = "pkt_meta",
        [PTR_TO_PACKET_END]     = "pkt_end",
 };
 
-#define __BPF_FUNC_STR_FN(x) [BPF_FUNC_ ## x] = __stringify(bpf_ ## x)
-static const char * const func_id_str[] = {
-       __BPF_FUNC_MAPPER(__BPF_FUNC_STR_FN)
-};
-#undef __BPF_FUNC_STR_FN
-
-static const char *func_id_name(int id)
-{
-       BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
-
-       if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
-               return func_id_str[id];
-       else
-               return "unknown";
-}
-
-static void print_verifier_state(struct bpf_verifier_state *state)
+static void print_verifier_state(struct bpf_verifier_env *env,
+                                struct bpf_verifier_state *state)
 {
        struct bpf_reg_state *reg;
        enum bpf_reg_type t;
@@ -217,21 +228,21 @@ static void print_verifier_state(struct bpf_verifier_state *state)
                t = reg->type;
                if (t == NOT_INIT)
                        continue;
-               verbose(" R%d=%s", i, reg_type_str[t]);
+               verbose(env, " R%d=%s", i, reg_type_str[t]);
                if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
                    tnum_is_const(reg->var_off)) {
                        /* reg->off should be 0 for SCALAR_VALUE */
-                       verbose("%lld", reg->var_off.value + reg->off);
+                       verbose(env, "%lld", reg->var_off.value + reg->off);
                } else {
-                       verbose("(id=%d", reg->id);
+                       verbose(env, "(id=%d", reg->id);
                        if (t != SCALAR_VALUE)
-                               verbose(",off=%d", reg->off);
-                       if (t == PTR_TO_PACKET)
-                               verbose(",r=%d", reg->range);
+                               verbose(env, ",off=%d", reg->off);
+                       if (type_is_pkt_pointer(t))
+                               verbose(env, ",r=%d", reg->range);
                        else if (t == CONST_PTR_TO_MAP ||
                                 t == PTR_TO_MAP_VALUE ||
                                 t == PTR_TO_MAP_VALUE_OR_NULL)
-                               verbose(",ks=%d,vs=%d",
+                               verbose(env, ",ks=%d,vs=%d",
                                        reg->map_ptr->key_size,
                                        reg->map_ptr->value_size);
                        if (tnum_is_const(reg->var_off)) {
@@ -239,243 +250,174 @@ static void print_verifier_state(struct bpf_verifier_state *state)
                                 * could be a pointer whose offset is too big
                                 * for reg->off
                                 */
-                               verbose(",imm=%llx", reg->var_off.value);
+                               verbose(env, ",imm=%llx", reg->var_off.value);
                        } else {
                                if (reg->smin_value != reg->umin_value &&
                                    reg->smin_value != S64_MIN)
-                                       verbose(",smin_value=%lld",
+                                       verbose(env, ",smin_value=%lld",
                                                (long long)reg->smin_value);
                                if (reg->smax_value != reg->umax_value &&
                                    reg->smax_value != S64_MAX)
-                                       verbose(",smax_value=%lld",
+                                       verbose(env, ",smax_value=%lld",
                                                (long long)reg->smax_value);
                                if (reg->umin_value != 0)
-                                       verbose(",umin_value=%llu",
+                                       verbose(env, ",umin_value=%llu",
                                                (unsigned long long)reg->umin_value);
                                if (reg->umax_value != U64_MAX)
-                                       verbose(",umax_value=%llu",
+                                       verbose(env, ",umax_value=%llu",
                                                (unsigned long long)reg->umax_value);
                                if (!tnum_is_unknown(reg->var_off)) {
                                        char tn_buf[48];
 
                                        tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
-                                       verbose(",var_off=%s", tn_buf);
+                                       verbose(env, ",var_off=%s", tn_buf);
                                }
                        }
-                       verbose(")");
+                       verbose(env, ")");
                }
        }
-       for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
-               if (state->stack_slot_type[i] == STACK_SPILL)
-                       verbose(" fp%d=%s", -MAX_BPF_STACK + i,
-                               reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]);
+       for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+               if (state->stack[i].slot_type[0] == STACK_SPILL)
+                       verbose(env, " fp%d=%s",
+                               -MAX_BPF_STACK + i * BPF_REG_SIZE,
+                               reg_type_str[state->stack[i].spilled_ptr.type]);
        }
-       verbose("\n");
+       verbose(env, "\n");
 }
 
-static const char *const bpf_class_string[] = {
-       [BPF_LD]    = "ld",
-       [BPF_LDX]   = "ldx",
-       [BPF_ST]    = "st",
-       [BPF_STX]   = "stx",
-       [BPF_ALU]   = "alu",
-       [BPF_JMP]   = "jmp",
-       [BPF_RET]   = "BUG",
-       [BPF_ALU64] = "alu64",
-};
-
-static const char *const bpf_alu_string[16] = {
-       [BPF_ADD >> 4]  = "+=",
-       [BPF_SUB >> 4]  = "-=",
-       [BPF_MUL >> 4]  = "*=",
-       [BPF_DIV >> 4]  = "/=",
-       [BPF_OR  >> 4]  = "|=",
-       [BPF_AND >> 4]  = "&=",
-       [BPF_LSH >> 4]  = "<<=",
-       [BPF_RSH >> 4]  = ">>=",
-       [BPF_NEG >> 4]  = "neg",
-       [BPF_MOD >> 4]  = "%=",
-       [BPF_XOR >> 4]  = "^=",
-       [BPF_MOV >> 4]  = "=",
-       [BPF_ARSH >> 4] = "s>>=",
-       [BPF_END >> 4]  = "endian",
-};
-
-static const char *const bpf_ldst_string[] = {
-       [BPF_W >> 3]  = "u32",
-       [BPF_H >> 3]  = "u16",
-       [BPF_B >> 3]  = "u8",
-       [BPF_DW >> 3] = "u64",
-};
-
-static const char *const bpf_jmp_string[16] = {
-       [BPF_JA >> 4]   = "jmp",
-       [BPF_JEQ >> 4]  = "==",
-       [BPF_JGT >> 4]  = ">",
-       [BPF_JLT >> 4]  = "<",
-       [BPF_JGE >> 4]  = ">=",
-       [BPF_JLE >> 4]  = "<=",
-       [BPF_JSET >> 4] = "&",
-       [BPF_JNE >> 4]  = "!=",
-       [BPF_JSGT >> 4] = "s>",
-       [BPF_JSLT >> 4] = "s<",
-       [BPF_JSGE >> 4] = "s>=",
-       [BPF_JSLE >> 4] = "s<=",
-       [BPF_CALL >> 4] = "call",
-       [BPF_EXIT >> 4] = "exit",
-};
+static int copy_stack_state(struct bpf_verifier_state *dst,
+                           const struct bpf_verifier_state *src)
+{
+       if (!src->stack)
+               return 0;
+       if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) {
+               /* internal bug, make state invalid to reject the program */
+               memset(dst, 0, sizeof(*dst));
+               return -EFAULT;
+       }
+       memcpy(dst->stack, src->stack,
+              sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE));
+       return 0;
+}
 
-static void print_bpf_insn(const struct bpf_verifier_env *env,
-                          const struct bpf_insn *insn)
+/* do_check() starts with zero-sized stack in struct bpf_verifier_state to
+ * make it consume minimal amount of memory. check_stack_write() access from
+ * the program calls into realloc_verifier_state() to grow the stack size.
+ * Note there is a non-zero 'parent' pointer inside bpf_verifier_state
+ * which this function copies over. It points to previous bpf_verifier_state
+ * which is never reallocated
+ */
+static int realloc_verifier_state(struct bpf_verifier_state *state, int size,
+                                 bool copy_old)
 {
-       u8 class = BPF_CLASS(insn->code);
-
-       if (class == BPF_ALU || class == BPF_ALU64) {
-               if (BPF_SRC(insn->code) == BPF_X)
-                       verbose("(%02x) %sr%d %s %sr%d\n",
-                               insn->code, class == BPF_ALU ? "(u32) " : "",
-                               insn->dst_reg,
-                               bpf_alu_string[BPF_OP(insn->code) >> 4],
-                               class == BPF_ALU ? "(u32) " : "",
-                               insn->src_reg);
-               else
-                       verbose("(%02x) %sr%d %s %s%d\n",
-                               insn->code, class == BPF_ALU ? "(u32) " : "",
-                               insn->dst_reg,
-                               bpf_alu_string[BPF_OP(insn->code) >> 4],
-                               class == BPF_ALU ? "(u32) " : "",
-                               insn->imm);
-       } else if (class == BPF_STX) {
-               if (BPF_MODE(insn->code) == BPF_MEM)
-                       verbose("(%02x) *(%s *)(r%d %+d) = r%d\n",
-                               insn->code,
-                               bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                               insn->dst_reg,
-                               insn->off, insn->src_reg);
-               else if (BPF_MODE(insn->code) == BPF_XADD)
-                       verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n",
-                               insn->code,
-                               bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                               insn->dst_reg, insn->off,
-                               insn->src_reg);
-               else
-                       verbose("BUG_%02x\n", insn->code);
-       } else if (class == BPF_ST) {
-               if (BPF_MODE(insn->code) != BPF_MEM) {
-                       verbose("BUG_st_%02x\n", insn->code);
-                       return;
-               }
-               verbose("(%02x) *(%s *)(r%d %+d) = %d\n",
-                       insn->code,
-                       bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                       insn->dst_reg,
-                       insn->off, insn->imm);
-       } else if (class == BPF_LDX) {
-               if (BPF_MODE(insn->code) != BPF_MEM) {
-                       verbose("BUG_ldx_%02x\n", insn->code);
-                       return;
+       u32 old_size = state->allocated_stack;
+       struct bpf_stack_state *new_stack;
+       int slot = size / BPF_REG_SIZE;
+
+       if (size <= old_size || !size) {
+               if (copy_old)
+                       return 0;
+               state->allocated_stack = slot * BPF_REG_SIZE;
+               if (!size && old_size) {
+                       kfree(state->stack);
+                       state->stack = NULL;
                }
-               verbose("(%02x) r%d = *(%s *)(r%d %+d)\n",
-                       insn->code, insn->dst_reg,
-                       bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                       insn->src_reg, insn->off);
-       } else if (class == BPF_LD) {
-               if (BPF_MODE(insn->code) == BPF_ABS) {
-                       verbose("(%02x) r0 = *(%s *)skb[%d]\n",
-                               insn->code,
-                               bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                               insn->imm);
-               } else if (BPF_MODE(insn->code) == BPF_IND) {
-                       verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n",
-                               insn->code,
-                               bpf_ldst_string[BPF_SIZE(insn->code) >> 3],
-                               insn->src_reg, insn->imm);
-               } else if (BPF_MODE(insn->code) == BPF_IMM &&
-                          BPF_SIZE(insn->code) == BPF_DW) {
-                       /* At this point, we already made sure that the second
-                        * part of the ldimm64 insn is accessible.
-                        */
-                       u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
-                       bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
+               return 0;
+       }
+       new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state),
+                                 GFP_KERNEL);
+       if (!new_stack)
+               return -ENOMEM;
+       if (copy_old) {
+               if (state->stack)
+                       memcpy(new_stack, state->stack,
+                              sizeof(*new_stack) * (old_size / BPF_REG_SIZE));
+               memset(new_stack + old_size / BPF_REG_SIZE, 0,
+                      sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE);
+       }
+       state->allocated_stack = slot * BPF_REG_SIZE;
+       kfree(state->stack);
+       state->stack = new_stack;
+       return 0;
+}
 
-                       if (map_ptr && !env->allow_ptr_leaks)
-                               imm = 0;
+static void free_verifier_state(struct bpf_verifier_state *state,
+                               bool free_self)
+{
+       kfree(state->stack);
+       if (free_self)
+               kfree(state);
+}
 
-                       verbose("(%02x) r%d = 0x%llx\n", insn->code,
-                               insn->dst_reg, (unsigned long long)imm);
-               } else {
-                       verbose("BUG_ld_%02x\n", insn->code);
-                       return;
-               }
-       } else if (class == BPF_JMP) {
-               u8 opcode = BPF_OP(insn->code);
+/* copy verifier state from src to dst growing dst stack space
+ * when necessary to accommodate larger src stack
+ */
+static int copy_verifier_state(struct bpf_verifier_state *dst,
+                              const struct bpf_verifier_state *src)
+{
+       int err;
 
-               if (opcode == BPF_CALL) {
-                       verbose("(%02x) call %s#%d\n", insn->code,
-                               func_id_name(insn->imm), insn->imm);
-               } else if (insn->code == (BPF_JMP | BPF_JA)) {
-                       verbose("(%02x) goto pc%+d\n",
-                               insn->code, insn->off);
-               } else if (insn->code == (BPF_JMP | BPF_EXIT)) {
-                       verbose("(%02x) exit\n", insn->code);
-               } else if (BPF_SRC(insn->code) == BPF_X) {
-                       verbose("(%02x) if r%d %s r%d goto pc%+d\n",
-                               insn->code, insn->dst_reg,
-                               bpf_jmp_string[BPF_OP(insn->code) >> 4],
-                               insn->src_reg, insn->off);
-               } else {
-                       verbose("(%02x) if r%d %s 0x%x goto pc%+d\n",
-                               insn->code, insn->dst_reg,
-                               bpf_jmp_string[BPF_OP(insn->code) >> 4],
-                               insn->imm, insn->off);
-               }
-       } else {
-               verbose("(%02x) %s\n", insn->code, bpf_class_string[class]);
-       }
+       err = realloc_verifier_state(dst, src->allocated_stack, false);
+       if (err)
+               return err;
+       memcpy(dst, src, offsetof(struct bpf_verifier_state, allocated_stack));
+       return copy_stack_state(dst, src);
 }
 
-static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx)
+static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
+                    int *insn_idx)
 {
-       struct bpf_verifier_stack_elem *elem;
-       int insn_idx;
+       struct bpf_verifier_state *cur = env->cur_state;
+       struct bpf_verifier_stack_elem *elem, *head = env->head;
+       int err;
 
        if (env->head == NULL)
-               return -1;
+               return -ENOENT;
 
-       memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state));
-       insn_idx = env->head->insn_idx;
+       if (cur) {
+               err = copy_verifier_state(cur, &head->st);
+               if (err)
+                       return err;
+       }
+       if (insn_idx)
+               *insn_idx = head->insn_idx;
        if (prev_insn_idx)
-               *prev_insn_idx = env->head->prev_insn_idx;
-       elem = env->head->next;
-       kfree(env->head);
+               *prev_insn_idx = head->prev_insn_idx;
+       elem = head->next;
+       free_verifier_state(&head->st, false);
+       kfree(head);
        env->head = elem;
        env->stack_size--;
-       return insn_idx;
+       return 0;
 }
 
 static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
                                             int insn_idx, int prev_insn_idx)
 {
+       struct bpf_verifier_state *cur = env->cur_state;
        struct bpf_verifier_stack_elem *elem;
+       int err;
 
-       elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
+       elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
        if (!elem)
                goto err;
 
-       memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state));
        elem->insn_idx = insn_idx;
        elem->prev_insn_idx = prev_insn_idx;
        elem->next = env->head;
        env->head = elem;
        env->stack_size++;
+       err = copy_verifier_state(&elem->st, cur);
+       if (err)
+               goto err;
        if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) {
-               verbose("BPF program is too complex\n");
+               verbose(env, "BPF program is too complex\n");
                goto err;
        }
        return &elem->st;
 err:
        /* pop all elements and return */
-       while (pop_stack(env, NULL) >= 0);
+       while (!pop_stack(env, NULL, NULL));
        return NULL;
 }
 
@@ -507,10 +449,11 @@ static void __mark_reg_known_zero(struct bpf_reg_state *reg)
        __mark_reg_known(reg, 0);
 }
 
-static void mark_reg_known_zero(struct bpf_reg_state *regs, u32 regno)
+static void mark_reg_known_zero(struct bpf_verifier_env *env,
+                               struct bpf_reg_state *regs, u32 regno)
 {
        if (WARN_ON(regno >= MAX_BPF_REG)) {
-               verbose("mark_reg_known_zero(regs, %u)\n", regno);
+               verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
                /* Something bad happened, let's kill all regs */
                for (regno = 0; regno < MAX_BPF_REG; regno++)
                        __mark_reg_not_init(regs + regno);
@@ -519,6 +462,31 @@ static void mark_reg_known_zero(struct bpf_reg_state *regs, u32 regno)
        __mark_reg_known_zero(regs + regno);
 }
 
+static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
+{
+       return type_is_pkt_pointer(reg->type);
+}
+
+static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
+{
+       return reg_is_pkt_pointer(reg) ||
+              reg->type == PTR_TO_PACKET_END;
+}
+
+/* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */
+static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
+                                   enum bpf_reg_type which)
+{
+       /* The register can already have a range from prior markings.
+        * This is fine as long as it hasn't been advanced from its
+        * origin.
+        */
+       return reg->type == which &&
+              reg->id == 0 &&
+              reg->off == 0 &&
+              tnum_equals_const(reg->var_off, 0);
+}
+
 /* Attempts to improve min/max values based on var_off information */
 static void __update_reg_bounds(struct bpf_reg_state *reg)
 {
@@ -595,10 +563,11 @@ static void __mark_reg_unknown(struct bpf_reg_state *reg)
        __mark_reg_unbounded(reg);
 }
 
-static void mark_reg_unknown(struct bpf_reg_state *regs, u32 regno)
+static void mark_reg_unknown(struct bpf_verifier_env *env,
+                            struct bpf_reg_state *regs, u32 regno)
 {
        if (WARN_ON(regno >= MAX_BPF_REG)) {
-               verbose("mark_reg_unknown(regs, %u)\n", regno);
+               verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
                /* Something bad happened, let's kill all regs */
                for (regno = 0; regno < MAX_BPF_REG; regno++)
                        __mark_reg_not_init(regs + regno);
@@ -613,10 +582,11 @@ static void __mark_reg_not_init(struct bpf_reg_state *reg)
        reg->type = NOT_INIT;
 }
 
-static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
+static void mark_reg_not_init(struct bpf_verifier_env *env,
+                             struct bpf_reg_state *regs, u32 regno)
 {
        if (WARN_ON(regno >= MAX_BPF_REG)) {
-               verbose("mark_reg_not_init(regs, %u)\n", regno);
+               verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
                /* Something bad happened, let's kill all regs */
                for (regno = 0; regno < MAX_BPF_REG; regno++)
                        __mark_reg_not_init(regs + regno);
@@ -625,22 +595,23 @@ static void mark_reg_not_init(struct bpf_reg_state *regs, u32 regno)
        __mark_reg_not_init(regs + regno);
 }
 
-static void init_reg_state(struct bpf_reg_state *regs)
+static void init_reg_state(struct bpf_verifier_env *env,
+                          struct bpf_reg_state *regs)
 {
        int i;
 
        for (i = 0; i < MAX_BPF_REG; i++) {
-               mark_reg_not_init(regs, i);
+               mark_reg_not_init(env, regs, i);
                regs[i].live = REG_LIVE_NONE;
        }
 
        /* frame pointer */
        regs[BPF_REG_FP].type = PTR_TO_STACK;
-       mark_reg_known_zero(regs, BPF_REG_FP);
+       mark_reg_known_zero(env, regs, BPF_REG_FP);
 
        /* 1st arg to a function */
        regs[BPF_REG_1].type = PTR_TO_CTX;
-       mark_reg_known_zero(regs, BPF_REG_1);
+       mark_reg_known_zero(env, regs, BPF_REG_1);
 }
 
 enum reg_arg_type {
@@ -671,29 +642,29 @@ static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno)
 static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
                         enum reg_arg_type t)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = env->cur_state->regs;
 
        if (regno >= MAX_BPF_REG) {
-               verbose("R%d is invalid\n", regno);
+               verbose(env, "R%d is invalid\n", regno);
                return -EINVAL;
        }
 
        if (t == SRC_OP) {
                /* check whether register used as source operand can be read */
                if (regs[regno].type == NOT_INIT) {
-                       verbose("R%d !read_ok\n", regno);
+                       verbose(env, "R%d !read_ok\n", regno);
                        return -EACCES;
                }
-               mark_reg_read(&env->cur_state, regno);
+               mark_reg_read(env->cur_state, regno);
        } else {
                /* check whether register used as dest operand can be written to */
                if (regno == BPF_REG_FP) {
-                       verbose("frame pointer is read only\n");
+                       verbose(env, "frame pointer is read only\n");
                        return -EACCES;
                }
                regs[regno].live |= REG_LIVE_WRITTEN;
                if (t == DST_OP)
-                       mark_reg_unknown(regs, regno);
+                       mark_reg_unknown(env, regs, regno);
        }
        return 0;
 }
@@ -706,6 +677,7 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
        case PTR_TO_STACK:
        case PTR_TO_CTX:
        case PTR_TO_PACKET:
+       case PTR_TO_PACKET_META:
        case PTR_TO_PACKET_END:
        case CONST_PTR_TO_MAP:
                return true;
@@ -717,35 +689,48 @@ static bool is_spillable_regtype(enum bpf_reg_type type)
 /* check_stack_read/write functions track spill/fill of registers,
  * stack boundary and alignment are checked in check_mem_access()
  */
-static int check_stack_write(struct bpf_verifier_state *state, int off,
+static int check_stack_write(struct bpf_verifier_env *env,
+                            struct bpf_verifier_state *state, int off,
                             int size, int value_regno)
 {
-       int i, spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
+       int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
+
+       err = realloc_verifier_state(state, round_up(slot + 1, BPF_REG_SIZE),
+                                    true);
+       if (err)
+               return err;
        /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0,
         * so it's aligned access and [off, off + size) are within stack limits
         */
+       if (!env->allow_ptr_leaks &&
+           state->stack[spi].slot_type[0] == STACK_SPILL &&
+           size != BPF_REG_SIZE) {
+               verbose(env, "attempt to corrupt spilled pointer on stack\n");
+               return -EACCES;
+       }
 
        if (value_regno >= 0 &&
            is_spillable_regtype(state->regs[value_regno].type)) {
 
                /* register containing pointer is being spilled into stack */
                if (size != BPF_REG_SIZE) {
-                       verbose("invalid size of register spill\n");
+                       verbose(env, "invalid size of register spill\n");
                        return -EACCES;
                }
 
                /* save register state */
-               state->spilled_regs[spi] = state->regs[value_regno];
-               state->spilled_regs[spi].live |= REG_LIVE_WRITTEN;
+               state->stack[spi].spilled_ptr = state->regs[value_regno];
+               state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
 
                for (i = 0; i < BPF_REG_SIZE; i++)
-                       state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL;
+                       state->stack[spi].slot_type[i] = STACK_SPILL;
        } else {
                /* regular write of data into stack */
-               state->spilled_regs[spi] = (struct bpf_reg_state) {};
+               state->stack[spi].spilled_ptr = (struct bpf_reg_state) {};
 
                for (i = 0; i < size; i++)
-                       state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC;
+                       state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
+                               STACK_MISC;
        }
        return 0;
 }
@@ -756,54 +741,58 @@ static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slo
 
        while (parent) {
                /* if read wasn't screened by an earlier write ... */
-               if (state->spilled_regs[slot].live & REG_LIVE_WRITTEN)
+               if (state->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN)
                        break;
                /* ... then we depend on parent's value */
-               parent->spilled_regs[slot].live |= REG_LIVE_READ;
+               parent->stack[slot].spilled_ptr.live |= REG_LIVE_READ;
                state = parent;
                parent = state->parent;
        }
 }
 
-static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
+static int check_stack_read(struct bpf_verifier_env *env,
+                           struct bpf_verifier_state *state, int off, int size,
                            int value_regno)
 {
-       u8 *slot_type;
-       int i, spi;
+       int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
+       u8 *stype;
 
-       slot_type = &state->stack_slot_type[MAX_BPF_STACK + off];
+       if (state->allocated_stack <= slot) {
+               verbose(env, "invalid read from stack off %d+0 size %d\n",
+                       off, size);
+               return -EACCES;
+       }
+       stype = state->stack[spi].slot_type;
 
-       if (slot_type[0] == STACK_SPILL) {
+       if (stype[0] == STACK_SPILL) {
                if (size != BPF_REG_SIZE) {
-                       verbose("invalid size of register spill\n");
+                       verbose(env, "invalid size of register spill\n");
                        return -EACCES;
                }
                for (i = 1; i < BPF_REG_SIZE; i++) {
-                       if (slot_type[i] != STACK_SPILL) {
-                               verbose("corrupted spill memory\n");
+                       if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
+                               verbose(env, "corrupted spill memory\n");
                                return -EACCES;
                        }
                }
 
-               spi = (MAX_BPF_STACK + off) / BPF_REG_SIZE;
-
                if (value_regno >= 0) {
                        /* restore register state from stack */
-                       state->regs[value_regno] = state->spilled_regs[spi];
+                       state->regs[value_regno] = state->stack[spi].spilled_ptr;
                        mark_stack_slot_read(state, spi);
                }
                return 0;
        } else {
                for (i = 0; i < size; i++) {
-                       if (slot_type[i] != STACK_MISC) {
-                               verbose("invalid read from stack off %d+%d size %d\n",
+                       if (stype[(slot - i) % BPF_REG_SIZE] != STACK_MISC) {
+                               verbose(env, "invalid read from stack off %d+%d size %d\n",
                                        off, i, size);
                                return -EACCES;
                        }
                }
                if (value_regno >= 0)
                        /* have read misc data from the stack */
-                       mark_reg_unknown(state->regs, value_regno);
+                       mark_reg_unknown(env, state->regs, value_regno);
                return 0;
        }
 }
@@ -812,10 +801,11 @@ static int check_stack_read(struct bpf_verifier_state *state, int off, int size,
 static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
                            int size)
 {
-       struct bpf_map *map = env->cur_state.regs[regno].map_ptr;
+       struct bpf_reg_state *regs = cur_regs(env);
+       struct bpf_map *map = regs[regno].map_ptr;
 
        if (off < 0 || size <= 0 || off + size > map->value_size) {
-               verbose("invalid access to map value, value_size=%d off=%d size=%d\n",
+               verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
                        map->value_size, off, size);
                return -EACCES;
        }
@@ -824,9 +814,9 @@ static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
 
 /* check read/write into a map element with possible variable offset */
 static int check_map_access(struct bpf_verifier_env *env, u32 regno,
-                               int off, int size)
+                           int off, int size)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
+       struct bpf_verifier_state *state = env->cur_state;
        struct bpf_reg_state *reg = &state->regs[regno];
        int err;
 
@@ -834,8 +824,8 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno,
         * need to try adding each of min_value and max_value to off
         * to make sure our theoretical access will be safe.
         */
-       if (log_level)
-               print_verifier_state(state);
+       if (env->log.level)
+               print_verifier_state(env, state);
        /* The minimum value is only important with signed
         * comparisons where we can't assume the floor of a
         * value is 0.  If we are using signed variables for our
@@ -843,13 +833,14 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno,
         * will have a set floor within our range.
         */
        if (reg->smin_value < 0) {
-               verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
+               verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
                        regno);
                return -EACCES;
        }
        err = __check_map_access(env, regno, reg->smin_value + off, size);
        if (err) {
-               verbose("R%d min value is outside of the array range\n", regno);
+               verbose(env, "R%d min value is outside of the array range\n",
+                       regno);
                return err;
        }
 
@@ -858,13 +849,14 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno,
         * If reg->umax_value + off could overflow, treat that as unbounded too.
         */
        if (reg->umax_value >= BPF_MAX_VAR_OFF) {
-               verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n",
+               verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
                        regno);
                return -EACCES;
        }
        err = __check_map_access(env, regno, reg->umax_value + off, size);
        if (err)
-               verbose("R%d max value is outside of the array range\n", regno);
+               verbose(env, "R%d max value is outside of the array range\n",
+                       regno);
        return err;
 }
 
@@ -899,11 +891,11 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
 static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
                                 int off, int size)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        struct bpf_reg_state *reg = &regs[regno];
 
        if (off < 0 || size <= 0 || (u64)off + size > reg->range) {
-               verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
+               verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
                        off, size, regno, reg->id, reg->off, reg->range);
                return -EACCES;
        }
@@ -913,7 +905,7 @@ static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
 static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
                               int size)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        struct bpf_reg_state *reg = &regs[regno];
        int err;
 
@@ -926,13 +918,13 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
         * detail to prove they're safe.
         */
        if (reg->smin_value < 0) {
-               verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
+               verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
                        regno);
                return -EACCES;
        }
        err = __check_packet_access(env, regno, off, size);
        if (err) {
-               verbose("R%d offset is outside of the packet\n", regno);
+               verbose(env, "R%d offset is outside of the packet\n", regno);
                return err;
        }
        return err;
@@ -946,12 +938,8 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
                .reg_type = *reg_type,
        };
 
-       /* for analyzer ctx accesses are already validated and converted */
-       if (env->analyzer_ops)
-               return 0;
-
-       if (env->prog->aux->ops->is_valid_access &&
-           env->prog->aux->ops->is_valid_access(off, size, t, &info)) {
+       if (env->ops->is_valid_access &&
+           env->ops->is_valid_access(off, size, t, &info)) {
                /* A non zero info.ctx_field_size indicates that this field is a
                 * candidate for later verifier transformation to load the whole
                 * field and then apply a mask when accessed with a narrower
@@ -959,16 +947,19 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off,
                 * will only allow for whole field access and rejects any other
                 * type of narrower access.
                 */
-               env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
                *reg_type = info.reg_type;
 
+               if (env->analyzer_ops)
+                       return 0;
+
+               env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
                /* remember the offset of last byte accessed in ctx */
                if (env->prog->aux->max_ctx_offset < off + size)
                        env->prog->aux->max_ctx_offset = off + size;
                return 0;
        }
 
-       verbose("invalid bpf_context access off=%d size=%d\n", off, size);
+       verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
        return -EACCES;
 }
 
@@ -983,10 +974,11 @@ static bool __is_pointer_value(bool allow_ptr_leaks,
 
 static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
 {
-       return __is_pointer_value(env->allow_ptr_leaks, &env->cur_state.regs[regno]);
+       return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno);
 }
 
-static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
+static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
+                                  const struct bpf_reg_state *reg,
                                   int off, int size, bool strict)
 {
        struct tnum reg_off;
@@ -1011,7 +1003,8 @@ static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
                char tn_buf[48];
 
                tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
-               verbose("misaligned packet access off %d+%s+%d+%d size %d\n",
+               verbose(env,
+                       "misaligned packet access off %d+%s+%d+%d size %d\n",
                        ip_align, tn_buf, reg->off, off, size);
                return -EACCES;
        }
@@ -1019,7 +1012,8 @@ static int check_pkt_ptr_alignment(const struct bpf_reg_state *reg,
        return 0;
 }
 
-static int check_generic_ptr_alignment(const struct bpf_reg_state *reg,
+static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
+                                      const struct bpf_reg_state *reg,
                                       const char *pointer_desc,
                                       int off, int size, bool strict)
 {
@@ -1034,7 +1028,7 @@ static int check_generic_ptr_alignment(const struct bpf_reg_state *reg,
                char tn_buf[48];
 
                tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
-               verbose("misaligned %saccess off %s+%d+%d size %d\n",
+               verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
                        pointer_desc, tn_buf, reg->off, off, size);
                return -EACCES;
        }
@@ -1051,8 +1045,11 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,
 
        switch (reg->type) {
        case PTR_TO_PACKET:
-               /* special case, because of NET_IP_ALIGN */
-               return check_pkt_ptr_alignment(reg, off, size, strict);
+       case PTR_TO_PACKET_META:
+               /* Special case, because of NET_IP_ALIGN. Given metadata sits
+                * right in front, treat it the very same way.
+                */
+               return check_pkt_ptr_alignment(env, reg, off, size, strict);
        case PTR_TO_MAP_VALUE:
                pointer_desc = "value ";
                break;
@@ -1065,7 +1062,8 @@ static int check_ptr_alignment(struct bpf_verifier_env *env,
        default:
                break;
        }
-       return check_generic_ptr_alignment(reg, pointer_desc, off, size, strict);
+       return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
+                                          strict);
 }
 
 /* check whether memory at (regno + off) is accessible for t = (read | write)
@@ -1078,8 +1076,9 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                            int bpf_size, enum bpf_access_type t,
                            int value_regno)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
-       struct bpf_reg_state *reg = &state->regs[regno];
+       struct bpf_verifier_state *state = env->cur_state;
+       struct bpf_reg_state *regs = cur_regs(env);
+       struct bpf_reg_state *reg = regs + regno;
        int size, err = 0;
 
        size = bpf_size_to_bytes(bpf_size);
@@ -1097,27 +1096,28 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
        if (reg->type == PTR_TO_MAP_VALUE) {
                if (t == BPF_WRITE && value_regno >= 0 &&
                    is_pointer_value(env, value_regno)) {
-                       verbose("R%d leaks addr into map\n", value_regno);
+                       verbose(env, "R%d leaks addr into map\n", value_regno);
                        return -EACCES;
                }
 
                err = check_map_access(env, regno, off, size);
                if (!err && t == BPF_READ && value_regno >= 0)
-                       mark_reg_unknown(state->regs, value_regno);
+                       mark_reg_unknown(env, regs, value_regno);
 
        } else if (reg->type == PTR_TO_CTX) {
                enum bpf_reg_type reg_type = SCALAR_VALUE;
 
                if (t == BPF_WRITE && value_regno >= 0 &&
                    is_pointer_value(env, value_regno)) {
-                       verbose("R%d leaks addr into ctx\n", value_regno);
+                       verbose(env, "R%d leaks addr into ctx\n", value_regno);
                        return -EACCES;
                }
                /* ctx accesses must be at a fixed offset, so that we can
                 * determine what type of data were returned.
                 */
                if (reg->off) {
-                       verbose("dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
+                       verbose(env,
+                               "dereference of modified ctx ptr R%d off=%d+%d, ctx+const is allowed, ctx+const+const is not\n",
                                regno, reg->off, off - reg->off);
                        return -EACCES;
                }
@@ -1125,24 +1125,26 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                        char tn_buf[48];
 
                        tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
-                       verbose("variable ctx access var_off=%s off=%d size=%d",
+                       verbose(env,
+                               "variable ctx access var_off=%s off=%d size=%d",
                                tn_buf, off, size);
                        return -EACCES;
                }
                err = check_ctx_access(env, insn_idx, off, size, t, &reg_type);
                if (!err && t == BPF_READ && value_regno >= 0) {
                        /* ctx access returns either a scalar, or a
-                        * PTR_TO_PACKET[_END].  In the latter case, we know
-                        * the offset is zero.
+                        * PTR_TO_PACKET[_META,_END]. In the latter
+                        * case, we know the offset is zero.
                         */
                        if (reg_type == SCALAR_VALUE)
-                               mark_reg_unknown(state->regs, value_regno);
+                               mark_reg_unknown(env, regs, value_regno);
                        else
-                               mark_reg_known_zero(state->regs, value_regno);
-                       state->regs[value_regno].id = 0;
-                       state->regs[value_regno].off = 0;
-                       state->regs[value_regno].range = 0;
-                       state->regs[value_regno].type = reg_type;
+                               mark_reg_known_zero(env, regs,
+                                                   value_regno);
+                       regs[value_regno].id = 0;
+                       regs[value_regno].off = 0;
+                       regs[value_regno].range = 0;
+                       regs[value_regno].type = reg_type;
                }
 
        } else if (reg->type == PTR_TO_STACK) {
@@ -1154,55 +1156,52 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                        char tn_buf[48];
 
                        tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
-                       verbose("variable stack access var_off=%s off=%d size=%d",
+                       verbose(env, "variable stack access var_off=%s off=%d size=%d",
                                tn_buf, off, size);
                        return -EACCES;
                }
                off += reg->var_off.value;
                if (off >= 0 || off < -MAX_BPF_STACK) {
-                       verbose("invalid stack off=%d size=%d\n", off, size);
+                       verbose(env, "invalid stack off=%d size=%d\n", off,
+                               size);
                        return -EACCES;
                }
 
                if (env->prog->aux->stack_depth < -off)
                        env->prog->aux->stack_depth = -off;
 
-               if (t == BPF_WRITE) {
-                       if (!env->allow_ptr_leaks &&
-                           state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL &&
-                           size != BPF_REG_SIZE) {
-                               verbose("attempt to corrupt spilled pointer on stack\n");
-                               return -EACCES;
-                       }
-                       err = check_stack_write(state, off, size, value_regno);
-               } else {
-                       err = check_stack_read(state, off, size, value_regno);
-               }
-       } else if (reg->type == PTR_TO_PACKET) {
+               if (t == BPF_WRITE)
+                       err = check_stack_write(env, state, off, size,
+                                               value_regno);
+               else
+                       err = check_stack_read(env, state, off, size,
+                                              value_regno);
+       } else if (reg_is_pkt_pointer(reg)) {
                if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
-                       verbose("cannot write into packet\n");
+                       verbose(env, "cannot write into packet\n");
                        return -EACCES;
                }
                if (t == BPF_WRITE && value_regno >= 0 &&
                    is_pointer_value(env, value_regno)) {
-                       verbose("R%d leaks addr into packet\n", value_regno);
+                       verbose(env, "R%d leaks addr into packet\n",
+                               value_regno);
                        return -EACCES;
                }
                err = check_packet_access(env, regno, off, size);
                if (!err && t == BPF_READ && value_regno >= 0)
-                       mark_reg_unknown(state->regs, value_regno);
+                       mark_reg_unknown(env, regs, value_regno);
        } else {
-               verbose("R%d invalid mem access '%s'\n",
-                       regno, reg_type_str[reg->type]);
+               verbose(env, "R%d invalid mem access '%s'\n", regno,
+                       reg_type_str[reg->type]);
                return -EACCES;
        }
 
        if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
-           state->regs[value_regno].type == SCALAR_VALUE) {
+           regs[value_regno].type == SCALAR_VALUE) {
                /* b/h/w load zero-extends, mark upper bits as known 0 */
-               state->regs[value_regno].var_off = tnum_cast(
-                                       state->regs[value_regno].var_off, size);
-               __update_reg_bounds(&state->regs[value_regno]);
+               regs[value_regno].var_off =
+                       tnum_cast(regs[value_regno].var_off, size);
+               __update_reg_bounds(&regs[value_regno]);
        }
        return err;
 }
@@ -1213,7 +1212,7 @@ static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_ins
 
        if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
            insn->imm != 0) {
-               verbose("BPF_XADD uses reserved fields\n");
+               verbose(env, "BPF_XADD uses reserved fields\n");
                return -EINVAL;
        }
 
@@ -1228,7 +1227,7 @@ static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_ins
                return err;
 
        if (is_pointer_value(env, insn->src_reg)) {
-               verbose("R%d leaks addr into mem\n", insn->src_reg);
+               verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
                return -EACCES;
        }
 
@@ -1259,9 +1258,9 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
                                int access_size, bool zero_size_allowed,
                                struct bpf_call_arg_meta *meta)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
+       struct bpf_verifier_state *state = env->cur_state;
        struct bpf_reg_state *regs = state->regs;
-       int off, i;
+       int off, i, slot, spi;
 
        if (regs[regno].type != PTR_TO_STACK) {
                /* Allow zero-byte read from NULL, regardless of pointer type */
@@ -1269,7 +1268,7 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
                    register_is_null(regs[regno]))
                        return 0;
 
-               verbose("R%d type=%s expected=%s\n", regno,
+               verbose(env, "R%d type=%s expected=%s\n", regno,
                        reg_type_str[regs[regno].type],
                        reg_type_str[PTR_TO_STACK]);
                return -EACCES;
@@ -1280,13 +1279,13 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
                char tn_buf[48];
 
                tnum_strn(tn_buf, sizeof(tn_buf), regs[regno].var_off);
-               verbose("invalid variable stack read R%d var_off=%s\n",
+               verbose(env, "invalid variable stack read R%d var_off=%s\n",
                        regno, tn_buf);
        }
        off = regs[regno].off + regs[regno].var_off.value;
        if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
            access_size <= 0) {
-               verbose("invalid stack type R%d off=%d access_size=%d\n",
+               verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
                        regno, off, access_size);
                return -EACCES;
        }
@@ -1301,8 +1300,12 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
        }
 
        for (i = 0; i < access_size; i++) {
-               if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) {
-                       verbose("invalid indirect read from stack off %d+%d size %d\n",
+               slot = -(off + i) - 1;
+               spi = slot / BPF_REG_SIZE;
+               if (state->allocated_stack <= slot ||
+                   state->stack[spi].slot_type[slot % BPF_REG_SIZE] !=
+                       STACK_MISC) {
+                       verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
                                off, i, access_size);
                        return -EACCES;
                }
@@ -1314,10 +1317,11 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
                                   int access_size, bool zero_size_allowed,
                                   struct bpf_call_arg_meta *meta)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
 
        switch (reg->type) {
        case PTR_TO_PACKET:
+       case PTR_TO_PACKET_META:
                return check_packet_access(env, regno, reg->off, access_size);
        case PTR_TO_MAP_VALUE:
                return check_map_access(env, regno, reg->off, access_size);
@@ -1331,7 +1335,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                          enum bpf_arg_type arg_type,
                          struct bpf_call_arg_meta *meta)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs, *reg = &regs[regno];
+       struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
        enum bpf_reg_type expected_type, type = reg->type;
        int err = 0;
 
@@ -1344,22 +1348,24 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
 
        if (arg_type == ARG_ANYTHING) {
                if (is_pointer_value(env, regno)) {
-                       verbose("R%d leaks addr into helper function\n", regno);
+                       verbose(env, "R%d leaks addr into helper function\n",
+                               regno);
                        return -EACCES;
                }
                return 0;
        }
 
-       if (type == PTR_TO_PACKET &&
+       if (type_is_pkt_pointer(type) &&
            !may_access_direct_pkt_data(env, meta, BPF_READ)) {
-               verbose("helper access to the packet is not allowed\n");
+               verbose(env, "helper access to the packet is not allowed\n");
                return -EACCES;
        }
 
        if (arg_type == ARG_PTR_TO_MAP_KEY ||
            arg_type == ARG_PTR_TO_MAP_VALUE) {
                expected_type = PTR_TO_STACK;
-               if (type != PTR_TO_PACKET && type != expected_type)
+               if (!type_is_pkt_pointer(type) &&
+                   type != expected_type)
                        goto err_type;
        } else if (arg_type == ARG_CONST_SIZE ||
                   arg_type == ARG_CONST_SIZE_OR_ZERO) {
@@ -1383,12 +1389,13 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                 */
                if (register_is_null(*reg))
                        /* final test in check_stack_boundary() */;
-               else if (type != PTR_TO_PACKET && type != PTR_TO_MAP_VALUE &&
+               else if (!type_is_pkt_pointer(type) &&
+                        type != PTR_TO_MAP_VALUE &&
                         type != expected_type)
                        goto err_type;
                meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
        } else {
-               verbose("unsupported arg_type %d\n", arg_type);
+               verbose(env, "unsupported arg_type %d\n", arg_type);
                return -EFAULT;
        }
 
@@ -1406,10 +1413,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                         * we have to check map_key here. Otherwise it means
                         * that kernel subsystem misconfigured verifier
                         */
-                       verbose("invalid map_ptr to access map->key\n");
+                       verbose(env, "invalid map_ptr to access map->key\n");
                        return -EACCES;
                }
-               if (type == PTR_TO_PACKET)
+               if (type_is_pkt_pointer(type))
                        err = check_packet_access(env, regno, reg->off,
                                                  meta->map_ptr->key_size);
                else
@@ -1422,10 +1429,10 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                 */
                if (!meta->map_ptr) {
                        /* kernel subsystem misconfigured verifier */
-                       verbose("invalid map_ptr to access map->value\n");
+                       verbose(env, "invalid map_ptr to access map->value\n");
                        return -EACCES;
                }
-               if (type == PTR_TO_PACKET)
+               if (type_is_pkt_pointer(type))
                        err = check_packet_access(env, regno, reg->off,
                                                  meta->map_ptr->value_size);
                else
@@ -1442,7 +1449,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                 */
                if (regno == 0) {
                        /* kernel subsystem misconfigured verifier */
-                       verbose("ARG_CONST_SIZE cannot be first argument\n");
+                       verbose(env,
+                               "ARG_CONST_SIZE cannot be first argument\n");
                        return -EACCES;
                }
 
@@ -1459,7 +1467,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                        meta = NULL;
 
                if (reg->smin_value < 0) {
-                       verbose("R%d min value is negative, either use unsigned or 'var &= const'\n",
+                       verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
                                regno);
                        return -EACCES;
                }
@@ -1473,7 +1481,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
                }
 
                if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
-                       verbose("R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
+                       verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
                                regno);
                        return -EACCES;
                }
@@ -1484,12 +1492,13 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
 
        return err;
 err_type:
-       verbose("R%d type=%s expected=%s\n", regno,
+       verbose(env, "R%d type=%s expected=%s\n", regno,
                reg_type_str[type], reg_type_str[expected_type]);
        return -EACCES;
 }
 
-static int check_map_func_compatibility(struct bpf_map *map, int func_id)
+static int check_map_func_compatibility(struct bpf_verifier_env *env,
+                                       struct bpf_map *map, int func_id)
 {
        if (!map)
                return 0;
@@ -1502,7 +1511,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
                break;
        case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
                if (func_id != BPF_FUNC_perf_event_read &&
-                   func_id != BPF_FUNC_perf_event_output)
+                   func_id != BPF_FUNC_perf_event_output &&
+                   func_id != BPF_FUNC_perf_event_read_value)
                        goto error;
                break;
        case BPF_MAP_TYPE_STACK_TRACE:
@@ -1522,6 +1532,11 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
                if (func_id != BPF_FUNC_redirect_map)
                        goto error;
                break;
+       /* Restrict bpf side of cpumap, open when use-cases appear */
+       case BPF_MAP_TYPE_CPUMAP:
+               if (func_id != BPF_FUNC_redirect_map)
+                       goto error;
+               break;
        case BPF_MAP_TYPE_ARRAY_OF_MAPS:
        case BPF_MAP_TYPE_HASH_OF_MAPS:
                if (func_id != BPF_FUNC_map_lookup_elem)
@@ -1545,6 +1560,7 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
                break;
        case BPF_FUNC_perf_event_read:
        case BPF_FUNC_perf_event_output:
+       case BPF_FUNC_perf_event_read_value:
                if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
                        goto error;
                break;
@@ -1558,7 +1574,8 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
                        goto error;
                break;
        case BPF_FUNC_redirect_map:
-               if (map->map_type != BPF_MAP_TYPE_DEVMAP)
+               if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
+                   map->map_type != BPF_MAP_TYPE_CPUMAP)
                        goto error;
                break;
        case BPF_FUNC_sk_redirect_map:
@@ -1575,7 +1592,7 @@ static int check_map_func_compatibility(struct bpf_map *map, int func_id)
 
        return 0;
 error:
-       verbose("cannot pass map_type %d into func %s#%d\n",
+       verbose(env, "cannot pass map_type %d into func %s#%d\n",
                map->map_type, func_id_name(func_id), func_id);
        return -EINVAL;
 }
@@ -1598,57 +1615,55 @@ static int check_raw_mode(const struct bpf_func_proto *fn)
        return count > 1 ? -EINVAL : 0;
 }
 
-/* Packet data might have moved, any old PTR_TO_PACKET[_END] are now invalid,
- * so turn them into unknown SCALAR_VALUE.
+/* Packet data might have moved, any old PTR_TO_PACKET[_META,_END]
+ * are now invalid, so turn them into unknown SCALAR_VALUE.
  */
 static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
+       struct bpf_verifier_state *state = env->cur_state;
        struct bpf_reg_state *regs = state->regs, *reg;
        int i;
 
        for (i = 0; i < MAX_BPF_REG; i++)
-               if (regs[i].type == PTR_TO_PACKET ||
-                   regs[i].type == PTR_TO_PACKET_END)
-                       mark_reg_unknown(regs, i);
+               if (reg_is_pkt_pointer_any(&regs[i]))
+                       mark_reg_unknown(env, regs, i);
 
-       for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
-               if (state->stack_slot_type[i] != STACK_SPILL)
-                       continue;
-               reg = &state->spilled_regs[i / BPF_REG_SIZE];
-               if (reg->type != PTR_TO_PACKET &&
-                   reg->type != PTR_TO_PACKET_END)
+       for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+               if (state->stack[i].slot_type[0] != STACK_SPILL)
                        continue;
-               __mark_reg_unknown(reg);
+               reg = &state->stack[i].spilled_ptr;
+               if (reg_is_pkt_pointer_any(reg))
+                       __mark_reg_unknown(reg);
        }
 }
 
 static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
        const struct bpf_func_proto *fn = NULL;
-       struct bpf_reg_state *regs = state->regs;
+       struct bpf_reg_state *regs;
        struct bpf_call_arg_meta meta;
        bool changes_data;
        int i, err;
 
        /* find function prototype */
        if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
-               verbose("invalid func %s#%d\n", func_id_name(func_id), func_id);
+               verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
+                       func_id);
                return -EINVAL;
        }
 
-       if (env->prog->aux->ops->get_func_proto)
-               fn = env->prog->aux->ops->get_func_proto(func_id);
+       if (env->ops->get_func_proto)
+               fn = env->ops->get_func_proto(func_id);
 
        if (!fn) {
-               verbose("unknown func %s#%d\n", func_id_name(func_id), func_id);
+               verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
+                       func_id);
                return -EINVAL;
        }
 
        /* eBPF programs must be GPL compatible to use GPL-ed functions */
        if (!env->prog->gpl_compatible && fn->gpl_only) {
-               verbose("cannot call GPL only function from proprietary program\n");
+               verbose(env, "cannot call GPL only function from proprietary program\n");
                return -EINVAL;
        }
 
@@ -1662,7 +1677,7 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
         */
        err = check_raw_mode(fn);
        if (err) {
-               verbose("kernel subsystem misconfigured func %s#%d\n",
+               verbose(env, "kernel subsystem misconfigured func %s#%d\n",
                        func_id_name(func_id), func_id);
                return err;
        }
@@ -1693,16 +1708,17 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
                        return err;
        }
 
+       regs = cur_regs(env);
        /* reset caller saved regs */
        for (i = 0; i < CALLER_SAVED_REGS; i++) {
-               mark_reg_not_init(regs, caller_saved[i]);
+               mark_reg_not_init(env, regs, caller_saved[i]);
                check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
        }
 
        /* update return register (already marked as written above) */
        if (fn->ret_type == RET_INTEGER) {
                /* sets type to SCALAR_VALUE */
-               mark_reg_unknown(regs, BPF_REG_0);
+               mark_reg_unknown(env, regs, BPF_REG_0);
        } else if (fn->ret_type == RET_VOID) {
                regs[BPF_REG_0].type = NOT_INIT;
        } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) {
@@ -1710,14 +1726,15 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
 
                regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
                /* There is no offset yet applied, variable or fixed */
-               mark_reg_known_zero(regs, BPF_REG_0);
+               mark_reg_known_zero(env, regs, BPF_REG_0);
                regs[BPF_REG_0].off = 0;
                /* remember map_ptr, so that check_map_access()
                 * can check 'value_size' boundary of memory access
                 * to map element returned from bpf_map_lookup_elem()
                 */
                if (meta.map_ptr == NULL) {
-                       verbose("kernel subsystem misconfigured verifier\n");
+                       verbose(env,
+                               "kernel subsystem misconfigured verifier\n");
                        return -EINVAL;
                }
                regs[BPF_REG_0].map_ptr = meta.map_ptr;
@@ -1728,12 +1745,12 @@ static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
                else if (insn_aux->map_ptr != meta.map_ptr)
                        insn_aux->map_ptr = BPF_MAP_PTR_POISON;
        } else {
-               verbose("unknown return type %d of func %s#%d\n",
+               verbose(env, "unknown return type %d of func %s#%d\n",
                        fn->ret_type, func_id_name(func_id), func_id);
                return -EINVAL;
        }
 
-       err = check_map_func_compatibility(meta.map_ptr, func_id);
+       err = check_map_func_compatibility(env, meta.map_ptr, func_id);
        if (err)
                return err;
 
@@ -1780,7 +1797,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                                   const struct bpf_reg_state *ptr_reg,
                                   const struct bpf_reg_state *off_reg)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg;
+       struct bpf_reg_state *regs = cur_regs(env), *dst_reg;
        bool known = tnum_is_const(off_reg->var_off);
        s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
            smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
@@ -1792,39 +1809,42 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
        dst_reg = &regs[dst];
 
        if (WARN_ON_ONCE(known && (smin_val != smax_val))) {
-               print_verifier_state(&env->cur_state);
-               verbose("verifier internal error: known but bad sbounds\n");
+               print_verifier_state(env, env->cur_state);
+               verbose(env,
+                       "verifier internal error: known but bad sbounds\n");
                return -EINVAL;
        }
        if (WARN_ON_ONCE(known && (umin_val != umax_val))) {
-               print_verifier_state(&env->cur_state);
-               verbose("verifier internal error: known but bad ubounds\n");
+               print_verifier_state(env, env->cur_state);
+               verbose(env,
+                       "verifier internal error: known but bad ubounds\n");
                return -EINVAL;
        }
 
        if (BPF_CLASS(insn->code) != BPF_ALU64) {
                /* 32-bit ALU ops on pointers produce (meaningless) scalars */
                if (!env->allow_ptr_leaks)
-                       verbose("R%d 32-bit pointer arithmetic prohibited\n",
+                       verbose(env,
+                               "R%d 32-bit pointer arithmetic prohibited\n",
                                dst);
                return -EACCES;
        }
 
        if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
                if (!env->allow_ptr_leaks)
-                       verbose("R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
+                       verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n",
                                dst);
                return -EACCES;
        }
        if (ptr_reg->type == CONST_PTR_TO_MAP) {
                if (!env->allow_ptr_leaks)
-                       verbose("R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
+                       verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n",
                                dst);
                return -EACCES;
        }
        if (ptr_reg->type == PTR_TO_PACKET_END) {
                if (!env->allow_ptr_leaks)
-                       verbose("R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
+                       verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n",
                                dst);
                return -EACCES;
        }
@@ -1879,7 +1899,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                }
                dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
                dst_reg->off = ptr_reg->off;
-               if (ptr_reg->type == PTR_TO_PACKET) {
+               if (reg_is_pkt_pointer(ptr_reg)) {
                        dst_reg->id = ++env->id_gen;
                        /* something was added to pkt_ptr, set range to zero */
                        dst_reg->range = 0;
@@ -1889,7 +1909,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                if (dst_reg == off_reg) {
                        /* scalar -= pointer.  Creates an unknown scalar */
                        if (!env->allow_ptr_leaks)
-                               verbose("R%d tried to subtract pointer from scalar\n",
+                               verbose(env, "R%d tried to subtract pointer from scalar\n",
                                        dst);
                        return -EACCES;
                }
@@ -1899,7 +1919,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                 */
                if (ptr_reg->type == PTR_TO_STACK) {
                        if (!env->allow_ptr_leaks)
-                               verbose("R%d subtraction from stack pointer prohibited\n",
+                               verbose(env, "R%d subtraction from stack pointer prohibited\n",
                                        dst);
                        return -EACCES;
                }
@@ -1939,7 +1959,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                }
                dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
                dst_reg->off = ptr_reg->off;
-               if (ptr_reg->type == PTR_TO_PACKET) {
+               if (reg_is_pkt_pointer(ptr_reg)) {
                        dst_reg->id = ++env->id_gen;
                        /* something was added to pkt_ptr, set range to zero */
                        if (smin_val < 0)
@@ -1954,13 +1974,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
                 * ptr &= ~3 which would reduce min_value by 3.)
                 */
                if (!env->allow_ptr_leaks)
-                       verbose("R%d bitwise operator %s on pointer prohibited\n",
+                       verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
                                dst, bpf_alu_string[opcode >> 4]);
                return -EACCES;
        default:
                /* other operators (e.g. MUL,LSH) produce non-pointer results */
                if (!env->allow_ptr_leaks)
-                       verbose("R%d pointer arithmetic with %s operator prohibited\n",
+                       verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
                                dst, bpf_alu_string[opcode >> 4]);
                return -EACCES;
        }
@@ -1976,7 +1996,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
                                      struct bpf_reg_state *dst_reg,
                                      struct bpf_reg_state src_reg)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        u8 opcode = BPF_OP(insn->code);
        bool src_known, dst_known;
        s64 smin_val, smax_val;
@@ -2126,7 +2146,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
                        /* Shifts greater than 63 are undefined.  This includes
                         * shifts by a negative number.
                         */
-                       mark_reg_unknown(regs, insn->dst_reg);
+                       mark_reg_unknown(env, regs, insn->dst_reg);
                        break;
                }
                /* We lose all sign bit information (except what we can pick
@@ -2154,7 +2174,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
                        /* Shifts greater than 63 are undefined.  This includes
                         * shifts by a negative number.
                         */
-                       mark_reg_unknown(regs, insn->dst_reg);
+                       mark_reg_unknown(env, regs, insn->dst_reg);
                        break;
                }
                /* BPF_RSH is an unsigned shift, so make the appropriate casts */
@@ -2182,7 +2202,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
                __update_reg_bounds(dst_reg);
                break;
        default:
-               mark_reg_unknown(regs, insn->dst_reg);
+               mark_reg_unknown(env, regs, insn->dst_reg);
                break;
        }
 
@@ -2197,7 +2217,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
 static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
                                   struct bpf_insn *insn)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg, *src_reg;
+       struct bpf_reg_state *regs = cur_regs(env), *dst_reg, *src_reg;
        struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
        u8 opcode = BPF_OP(insn->code);
        int rc;
@@ -2214,12 +2234,12 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
                                 * an arbitrary scalar.
                                 */
                                if (!env->allow_ptr_leaks) {
-                                       verbose("R%d pointer %s pointer prohibited\n",
+                                       verbose(env, "R%d pointer %s pointer prohibited\n",
                                                insn->dst_reg,
                                                bpf_alu_string[opcode >> 4]);
                                        return -EACCES;
                                }
-                               mark_reg_unknown(regs, insn->dst_reg);
+                               mark_reg_unknown(env, regs, insn->dst_reg);
                                return 0;
                        } else {
                                /* scalar += pointer
@@ -2271,13 +2291,13 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 
        /* Got here implies adding two SCALAR_VALUEs */
        if (WARN_ON_ONCE(ptr_reg)) {
-               print_verifier_state(&env->cur_state);
-               verbose("verifier internal error: unexpected ptr_reg\n");
+               print_verifier_state(env, env->cur_state);
+               verbose(env, "verifier internal error: unexpected ptr_reg\n");
                return -EINVAL;
        }
        if (WARN_ON(!src_reg)) {
-               print_verifier_state(&env->cur_state);
-               verbose("verifier internal error: no src_reg\n");
+               print_verifier_state(env, env->cur_state);
+               verbose(env, "verifier internal error: no src_reg\n");
                return -EINVAL;
        }
        return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
@@ -2286,7 +2306,7 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 /* check validity of 32-bit and 64-bit arithmetic operations */
 static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        u8 opcode = BPF_OP(insn->code);
        int err;
 
@@ -2295,14 +2315,14 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                        if (BPF_SRC(insn->code) != 0 ||
                            insn->src_reg != BPF_REG_0 ||
                            insn->off != 0 || insn->imm != 0) {
-                               verbose("BPF_NEG uses reserved fields\n");
+                               verbose(env, "BPF_NEG uses reserved fields\n");
                                return -EINVAL;
                        }
                } else {
                        if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
                            (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
                            BPF_CLASS(insn->code) == BPF_ALU64) {
-                               verbose("BPF_END uses reserved fields\n");
+                               verbose(env, "BPF_END uses reserved fields\n");
                                return -EINVAL;
                        }
                }
@@ -2313,7 +2333,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                        return err;
 
                if (is_pointer_value(env, insn->dst_reg)) {
-                       verbose("R%d pointer arithmetic prohibited\n",
+                       verbose(env, "R%d pointer arithmetic prohibited\n",
                                insn->dst_reg);
                        return -EACCES;
                }
@@ -2327,7 +2347,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
 
                if (BPF_SRC(insn->code) == BPF_X) {
                        if (insn->imm != 0 || insn->off != 0) {
-                               verbose("BPF_MOV uses reserved fields\n");
+                               verbose(env, "BPF_MOV uses reserved fields\n");
                                return -EINVAL;
                        }
 
@@ -2337,7 +2357,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                                return err;
                } else {
                        if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
-                               verbose("BPF_MOV uses reserved fields\n");
+                               verbose(env, "BPF_MOV uses reserved fields\n");
                                return -EINVAL;
                        }
                }
@@ -2357,11 +2377,12 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                        } else {
                                /* R1 = (u32) R2 */
                                if (is_pointer_value(env, insn->src_reg)) {
-                                       verbose("R%d partial copy of pointer\n",
+                                       verbose(env,
+                                               "R%d partial copy of pointer\n",
                                                insn->src_reg);
                                        return -EACCES;
                                }
-                               mark_reg_unknown(regs, insn->dst_reg);
+                               mark_reg_unknown(env, regs, insn->dst_reg);
                                /* high 32 bits are known zero. */
                                regs[insn->dst_reg].var_off = tnum_cast(
                                                regs[insn->dst_reg].var_off, 4);
@@ -2376,14 +2397,14 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                }
 
        } else if (opcode > BPF_END) {
-               verbose("invalid BPF_ALU opcode %x\n", opcode);
+               verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
                return -EINVAL;
 
        } else {        /* all other ALU ops: and, sub, xor, add, ... */
 
                if (BPF_SRC(insn->code) == BPF_X) {
                        if (insn->imm != 0 || insn->off != 0) {
-                               verbose("BPF_ALU uses reserved fields\n");
+                               verbose(env, "BPF_ALU uses reserved fields\n");
                                return -EINVAL;
                        }
                        /* check src1 operand */
@@ -2392,7 +2413,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                                return err;
                } else {
                        if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
-                               verbose("BPF_ALU uses reserved fields\n");
+                               verbose(env, "BPF_ALU uses reserved fields\n");
                                return -EINVAL;
                        }
                }
@@ -2404,7 +2425,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
 
                if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
                    BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
-                       verbose("div by zero\n");
+                       verbose(env, "div by zero\n");
                        return -EINVAL;
                }
 
@@ -2413,7 +2434,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
                        int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
 
                        if (insn->imm < 0 || insn->imm >= size) {
-                               verbose("invalid shift %d\n", insn->imm);
+                               verbose(env, "invalid shift %d\n", insn->imm);
                                return -EINVAL;
                        }
                }
@@ -2431,6 +2452,7 @@ static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
 
 static void find_good_pkt_pointers(struct bpf_verifier_state *state,
                                   struct bpf_reg_state *dst_reg,
+                                  enum bpf_reg_type type,
                                   bool range_right_open)
 {
        struct bpf_reg_state *regs = state->regs, *reg;
@@ -2501,15 +2523,15 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *state,
         * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16.
         */
        for (i = 0; i < MAX_BPF_REG; i++)
-               if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id)
+               if (regs[i].type == type && regs[i].id == dst_reg->id)
                        /* keep the maximum range already checked */
                        regs[i].range = max(regs[i].range, new_range);
 
-       for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
-               if (state->stack_slot_type[i] != STACK_SPILL)
+       for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+               if (state->stack[i].slot_type[0] != STACK_SPILL)
                        continue;
-               reg = &state->spilled_regs[i / BPF_REG_SIZE];
-               if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id)
+               reg = &state->stack[i].spilled_ptr;
+               if (reg->type == type && reg->id == dst_reg->id)
                        reg->range = max(reg->range, new_range);
        }
 }
@@ -2758,29 +2780,122 @@ static void mark_map_regs(struct bpf_verifier_state *state, u32 regno,
        for (i = 0; i < MAX_BPF_REG; i++)
                mark_map_reg(regs, i, id, is_null);
 
-       for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) {
-               if (state->stack_slot_type[i] != STACK_SPILL)
+       for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
+               if (state->stack[i].slot_type[0] != STACK_SPILL)
                        continue;
-               mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, is_null);
+               mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
        }
 }
 
+static bool try_match_pkt_pointers(const struct bpf_insn *insn,
+                                  struct bpf_reg_state *dst_reg,
+                                  struct bpf_reg_state *src_reg,
+                                  struct bpf_verifier_state *this_branch,
+                                  struct bpf_verifier_state *other_branch)
+{
+       if (BPF_SRC(insn->code) != BPF_X)
+               return false;
+
+       switch (BPF_OP(insn->code)) {
+       case BPF_JGT:
+               if ((dst_reg->type == PTR_TO_PACKET &&
+                    src_reg->type == PTR_TO_PACKET_END) ||
+                   (dst_reg->type == PTR_TO_PACKET_META &&
+                    reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
+                       /* pkt_data' > pkt_end, pkt_meta' > pkt_data */
+                       find_good_pkt_pointers(this_branch, dst_reg,
+                                              dst_reg->type, false);
+               } else if ((dst_reg->type == PTR_TO_PACKET_END &&
+                           src_reg->type == PTR_TO_PACKET) ||
+                          (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
+                           src_reg->type == PTR_TO_PACKET_META)) {
+                       /* pkt_end > pkt_data', pkt_data > pkt_meta' */
+                       find_good_pkt_pointers(other_branch, src_reg,
+                                              src_reg->type, true);
+               } else {
+                       return false;
+               }
+               break;
+       case BPF_JLT:
+               if ((dst_reg->type == PTR_TO_PACKET &&
+                    src_reg->type == PTR_TO_PACKET_END) ||
+                   (dst_reg->type == PTR_TO_PACKET_META &&
+                    reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
+                       /* pkt_data' < pkt_end, pkt_meta' < pkt_data */
+                       find_good_pkt_pointers(other_branch, dst_reg,
+                                              dst_reg->type, true);
+               } else if ((dst_reg->type == PTR_TO_PACKET_END &&
+                           src_reg->type == PTR_TO_PACKET) ||
+                          (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
+                           src_reg->type == PTR_TO_PACKET_META)) {
+                       /* pkt_end < pkt_data', pkt_data > pkt_meta' */
+                       find_good_pkt_pointers(this_branch, src_reg,
+                                              src_reg->type, false);
+               } else {
+                       return false;
+               }
+               break;
+       case BPF_JGE:
+               if ((dst_reg->type == PTR_TO_PACKET &&
+                    src_reg->type == PTR_TO_PACKET_END) ||
+                   (dst_reg->type == PTR_TO_PACKET_META &&
+                    reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
+                       /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */
+                       find_good_pkt_pointers(this_branch, dst_reg,
+                                              dst_reg->type, true);
+               } else if ((dst_reg->type == PTR_TO_PACKET_END &&
+                           src_reg->type == PTR_TO_PACKET) ||
+                          (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
+                           src_reg->type == PTR_TO_PACKET_META)) {
+                       /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */
+                       find_good_pkt_pointers(other_branch, src_reg,
+                                              src_reg->type, false);
+               } else {
+                       return false;
+               }
+               break;
+       case BPF_JLE:
+               if ((dst_reg->type == PTR_TO_PACKET &&
+                    src_reg->type == PTR_TO_PACKET_END) ||
+                   (dst_reg->type == PTR_TO_PACKET_META &&
+                    reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
+                       /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */
+                       find_good_pkt_pointers(other_branch, dst_reg,
+                                              dst_reg->type, false);
+               } else if ((dst_reg->type == PTR_TO_PACKET_END &&
+                           src_reg->type == PTR_TO_PACKET) ||
+                          (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
+                           src_reg->type == PTR_TO_PACKET_META)) {
+                       /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */
+                       find_good_pkt_pointers(this_branch, src_reg,
+                                              src_reg->type, true);
+               } else {
+                       return false;
+               }
+               break;
+       default:
+               return false;
+       }
+
+       return true;
+}
+
 static int check_cond_jmp_op(struct bpf_verifier_env *env,
                             struct bpf_insn *insn, int *insn_idx)
 {
-       struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state;
+       struct bpf_verifier_state *other_branch, *this_branch = env->cur_state;
        struct bpf_reg_state *regs = this_branch->regs, *dst_reg;
        u8 opcode = BPF_OP(insn->code);
        int err;
 
        if (opcode > BPF_JSLE) {
-               verbose("invalid BPF_JMP opcode %x\n", opcode);
+               verbose(env, "invalid BPF_JMP opcode %x\n", opcode);
                return -EINVAL;
        }
 
        if (BPF_SRC(insn->code) == BPF_X) {
                if (insn->imm != 0) {
-                       verbose("BPF_JMP uses reserved fields\n");
+                       verbose(env, "BPF_JMP uses reserved fields\n");
                        return -EINVAL;
                }
 
@@ -2790,13 +2905,13 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
                        return err;
 
                if (is_pointer_value(env, insn->src_reg)) {
-                       verbose("R%d pointer comparison prohibited\n",
+                       verbose(env, "R%d pointer comparison prohibited\n",
                                insn->src_reg);
                        return -EACCES;
                }
        } else {
                if (insn->src_reg != BPF_REG_0) {
-                       verbose("BPF_JMP uses reserved fields\n");
+                       verbose(env, "BPF_JMP uses reserved fields\n");
                        return -EINVAL;
                }
        }
@@ -2871,52 +2986,15 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env,
                 */
                mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE);
                mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
-                  dst_reg->type == PTR_TO_PACKET &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET_END) {
-               /* pkt_data' > pkt_end */
-               find_good_pkt_pointers(this_branch, dst_reg, false);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT &&
-                  dst_reg->type == PTR_TO_PACKET_END &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET) {
-               /* pkt_end > pkt_data' */
-               find_good_pkt_pointers(other_branch, &regs[insn->src_reg], true);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT &&
-                  dst_reg->type == PTR_TO_PACKET &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET_END) {
-               /* pkt_data' < pkt_end */
-               find_good_pkt_pointers(other_branch, dst_reg, true);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLT &&
-                  dst_reg->type == PTR_TO_PACKET_END &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET) {
-               /* pkt_end < pkt_data' */
-               find_good_pkt_pointers(this_branch, &regs[insn->src_reg], false);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
-                  dst_reg->type == PTR_TO_PACKET &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET_END) {
-               /* pkt_data' >= pkt_end */
-               find_good_pkt_pointers(this_branch, dst_reg, true);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE &&
-                  dst_reg->type == PTR_TO_PACKET_END &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET) {
-               /* pkt_end >= pkt_data' */
-               find_good_pkt_pointers(other_branch, &regs[insn->src_reg], false);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE &&
-                  dst_reg->type == PTR_TO_PACKET &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET_END) {
-               /* pkt_data' <= pkt_end */
-               find_good_pkt_pointers(other_branch, dst_reg, false);
-       } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JLE &&
-                  dst_reg->type == PTR_TO_PACKET_END &&
-                  regs[insn->src_reg].type == PTR_TO_PACKET) {
-               /* pkt_end <= pkt_data' */
-               find_good_pkt_pointers(this_branch, &regs[insn->src_reg], true);
-       } else if (is_pointer_value(env, insn->dst_reg)) {
-               verbose("R%d pointer comparison prohibited\n", insn->dst_reg);
+       } else if (!try_match_pkt_pointers(insn, dst_reg, &regs[insn->src_reg],
+                                          this_branch, other_branch) &&
+                  is_pointer_value(env, insn->dst_reg)) {
+               verbose(env, "R%d pointer comparison prohibited\n",
+                       insn->dst_reg);
                return -EACCES;
        }
-       if (log_level)
-               print_verifier_state(this_branch);
+       if (env->log.level)
+               print_verifier_state(env, this_branch);
        return 0;
 }
 
@@ -2931,15 +3009,15 @@ static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn)
 /* verify BPF_LD_IMM64 instruction */
 static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        int err;
 
        if (BPF_SIZE(insn->code) != BPF_DW) {
-               verbose("invalid BPF_LD_IMM insn\n");
+               verbose(env, "invalid BPF_LD_IMM insn\n");
                return -EINVAL;
        }
        if (insn->off != 0) {
-               verbose("BPF_LD_IMM64 uses reserved fields\n");
+               verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
                return -EINVAL;
        }
 
@@ -2992,19 +3070,19 @@ static bool may_access_skb(enum bpf_prog_type type)
  */
 static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
 {
-       struct bpf_reg_state *regs = env->cur_state.regs;
+       struct bpf_reg_state *regs = cur_regs(env);
        u8 mode = BPF_MODE(insn->code);
        int i, err;
 
        if (!may_access_skb(env->prog->type)) {
-               verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
+               verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
                return -EINVAL;
        }
 
        if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
            BPF_SIZE(insn->code) == BPF_DW ||
            (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
-               verbose("BPF_LD_[ABS|IND] uses reserved fields\n");
+               verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
                return -EINVAL;
        }
 
@@ -3014,7 +3092,8 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
                return err;
 
        if (regs[BPF_REG_6].type != PTR_TO_CTX) {
-               verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
+               verbose(env,
+                       "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
                return -EINVAL;
        }
 
@@ -3027,7 +3106,7 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
 
        /* reset caller saved regs to unreadable */
        for (i = 0; i < CALLER_SAVED_REGS; i++) {
-               mark_reg_not_init(regs, caller_saved[i]);
+               mark_reg_not_init(env, regs, caller_saved[i]);
                check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
        }
 
@@ -3035,7 +3114,44 @@ static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
         * the value fetched from the packet.
         * Already marked as written above.
         */
-       mark_reg_unknown(regs, BPF_REG_0);
+       mark_reg_unknown(env, regs, BPF_REG_0);
+       return 0;
+}
+
+static int check_return_code(struct bpf_verifier_env *env)
+{
+       struct bpf_reg_state *reg;
+       struct tnum range = tnum_range(0, 1);
+
+       switch (env->prog->type) {
+       case BPF_PROG_TYPE_CGROUP_SKB:
+       case BPF_PROG_TYPE_CGROUP_SOCK:
+       case BPF_PROG_TYPE_SOCK_OPS:
+               break;
+       default:
+               return 0;
+       }
+
+       reg = cur_regs(env) + BPF_REG_0;
+       if (reg->type != SCALAR_VALUE) {
+               verbose(env, "At program exit the register R0 is not a known value (%s)\n",
+                       reg_type_str[reg->type]);
+               return -EINVAL;
+       }
+
+       if (!tnum_in(range, reg->var_off)) {
+               verbose(env, "At program exit the register R0 ");
+               if (!tnum_is_unknown(reg->var_off)) {
+                       char tn_buf[48];
+
+                       tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
+                       verbose(env, "has value %s", tn_buf);
+               } else {
+                       verbose(env, "has unknown scalar value");
+               }
+               verbose(env, " should have been 0 or 1\n");
+               return -EINVAL;
+       }
        return 0;
 }
 
@@ -3099,7 +3215,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
                return 0;
 
        if (w < 0 || w >= env->prog->len) {
-               verbose("jump out of range from insn %d to %d\n", t, w);
+               verbose(env, "jump out of range from insn %d to %d\n", t, w);
                return -EINVAL;
        }
 
@@ -3116,13 +3232,13 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
                insn_stack[cur_stack++] = w;
                return 1;
        } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
-               verbose("back-edge from insn %d to %d\n", t, w);
+               verbose(env, "back-edge from insn %d to %d\n", t, w);
                return -EINVAL;
        } else if (insn_state[w] == EXPLORED) {
                /* forward- or cross-edge */
                insn_state[t] = DISCOVERED | e;
        } else {
-               verbose("insn state internal bug\n");
+               verbose(env, "insn state internal bug\n");
                return -EFAULT;
        }
        return 0;
@@ -3216,7 +3332,7 @@ peek_stack:
 mark_explored:
        insn_state[t] = EXPLORED;
        if (cur_stack-- <= 0) {
-               verbose("pop stack internal bug\n");
+               verbose(env, "pop stack internal bug\n");
                ret = -EFAULT;
                goto err_free;
        }
@@ -3225,7 +3341,7 @@ mark_explored:
 check_state:
        for (i = 0; i < insn_cnt; i++) {
                if (insn_state[i] != EXPLORED) {
-                       verbose("unreachable insn %d\n", i);
+                       verbose(env, "unreachable insn %d\n", i);
                        ret = -EINVAL;
                        goto err_free;
                }
@@ -3340,8 +3456,9 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
                        return false;
                /* Check our ids match any regs they're supposed to */
                return check_ids(rold->id, rcur->id, idmap);
+       case PTR_TO_PACKET_META:
        case PTR_TO_PACKET:
-               if (rcur->type != PTR_TO_PACKET)
+               if (rcur->type != rold->type)
                        return false;
                /* We must have at least as much range as the old ptr
                 * did, so that any accesses which were safe before are
@@ -3379,6 +3496,57 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
        return false;
 }
 
+static bool stacksafe(struct bpf_verifier_state *old,
+                     struct bpf_verifier_state *cur,
+                     struct idpair *idmap)
+{
+       int i, spi;
+
+       /* if explored stack has more populated slots than current stack
+        * such stacks are not equivalent
+        */
+       if (old->allocated_stack > cur->allocated_stack)
+               return false;
+
+       /* walk slots of the explored stack and ignore any additional
+        * slots in the current stack, since explored(safe) state
+        * didn't use them
+        */
+       for (i = 0; i < old->allocated_stack; i++) {
+               spi = i / BPF_REG_SIZE;
+
+               if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
+                       continue;
+               if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
+                   cur->stack[spi].slot_type[i % BPF_REG_SIZE])
+                       /* Ex: old explored (safe) state has STACK_SPILL in
+                        * this stack slot, but current has has STACK_MISC ->
+                        * this verifier states are not equivalent,
+                        * return false to continue verification of this path
+                        */
+                       return false;
+               if (i % BPF_REG_SIZE)
+                       continue;
+               if (old->stack[spi].slot_type[0] != STACK_SPILL)
+                       continue;
+               if (!regsafe(&old->stack[spi].spilled_ptr,
+                            &cur->stack[spi].spilled_ptr,
+                            idmap))
+                       /* when explored and current stack slot are both storing
+                        * spilled registers, check that stored pointers types
+                        * are the same as well.
+                        * Ex: explored safe path could have stored
+                        * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
+                        * but current path has stored:
+                        * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
+                        * such verifier states are not equivalent.
+                        * return false to continue verification of this path
+                        */
+                       return false;
+       }
+       return true;
+}
+
 /* compare two verifier states
  *
  * all states stored in state_list are known to be valid, since
@@ -3423,37 +3591,8 @@ static bool states_equal(struct bpf_verifier_env *env,
                        goto out_free;
        }
 
-       for (i = 0; i < MAX_BPF_STACK; i++) {
-               if (old->stack_slot_type[i] == STACK_INVALID)
-                       continue;
-               if (old->stack_slot_type[i] != cur->stack_slot_type[i])
-                       /* Ex: old explored (safe) state has STACK_SPILL in
-                        * this stack slot, but current has has STACK_MISC ->
-                        * this verifier states are not equivalent,
-                        * return false to continue verification of this path
-                        */
-                       goto out_free;
-               if (i % BPF_REG_SIZE)
-                       continue;
-               if (old->stack_slot_type[i] != STACK_SPILL)
-                       continue;
-               if (!regsafe(&old->spilled_regs[i / BPF_REG_SIZE],
-                            &cur->spilled_regs[i / BPF_REG_SIZE],
-                            idmap))
-                       /* when explored and current stack slot are both storing
-                        * spilled registers, check that stored pointers types
-                        * are the same as well.
-                        * Ex: explored safe path could have stored
-                        * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8}
-                        * but current path has stored:
-                        * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16}
-                        * such verifier states are not equivalent.
-                        * return false to continue verification of this path
-                        */
-                       goto out_free;
-               else
-                       continue;
-       }
+       if (!stacksafe(old, cur, idmap))
+               goto out_free;
        ret = true;
 out_free:
        kfree(idmap);
@@ -3489,17 +3628,19 @@ static bool do_propagate_liveness(const struct bpf_verifier_state *state,
                }
        }
        /* ... and stack slots */
-       for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++) {
-               if (parent->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
+       for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
+                   i < parent->allocated_stack / BPF_REG_SIZE; i++) {
+               if (parent->stack[i].slot_type[0] != STACK_SPILL)
                        continue;
-               if (state->stack_slot_type[i * BPF_REG_SIZE] != STACK_SPILL)
+               if (state->stack[i].slot_type[0] != STACK_SPILL)
                        continue;
-               if (parent->spilled_regs[i].live & REG_LIVE_READ)
+               if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ)
                        continue;
-               if (writes && (state->spilled_regs[i].live & REG_LIVE_WRITTEN))
+               if (writes &&
+                   (state->stack[i].spilled_ptr.live & REG_LIVE_WRITTEN))
                        continue;
-               if (state->spilled_regs[i].live & REG_LIVE_READ) {
-                       parent->spilled_regs[i].live |= REG_LIVE_READ;
+               if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) {
+                       parent->stack[i].spilled_ptr.live |= REG_LIVE_READ;
                        touched = true;
                }
        }
@@ -3529,7 +3670,8 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 {
        struct bpf_verifier_state_list *new_sl;
        struct bpf_verifier_state_list *sl;
-       int i;
+       struct bpf_verifier_state *cur = env->cur_state;
+       int i, err;
 
        sl = env->explored_states[insn_idx];
        if (!sl)
@@ -3539,7 +3681,7 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
                return 0;
 
        while (sl != STATE_LIST_MARK) {
-               if (states_equal(env, &sl->state, &env->cur_state)) {
+               if (states_equal(env, &sl->state, cur)) {
                        /* reached equivalent register/stack state,
                         * prune the search.
                         * Registers read by the continuation are read by us.
@@ -3550,7 +3692,7 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
                         * they'll be immediately forgotten as we're pruning
                         * this state and will pop a new one.
                         */
-                       propagate_liveness(&sl->state, &env->cur_state);
+                       propagate_liveness(&sl->state, cur);
                        return 1;
                }
                sl = sl->next;
@@ -3562,16 +3704,21 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
         * it will be rejected. Since there are no loops, we won't be
         * seeing this 'insn_idx' instruction again on the way to bpf_exit
         */
-       new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER);
+       new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
        if (!new_sl)
                return -ENOMEM;
 
        /* add new state to the head of linked list */
-       memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state));
+       err = copy_verifier_state(&new_sl->state, cur);
+       if (err) {
+               free_verifier_state(&new_sl->state, false);
+               kfree(new_sl);
+               return err;
+       }
        new_sl->next = env->explored_states[insn_idx];
        env->explored_states[insn_idx] = new_sl;
        /* connect new state to parentage chain */
-       env->cur_state.parent = &new_sl->state;
+       cur->parent = &new_sl->state;
        /* clear write marks in current state: the writes we did are not writes
         * our child did, so they don't screen off its reads from us.
         * (There are no read marks in current state, because reads always mark
@@ -3579,10 +3726,10 @@ static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
         * explored_states can get read marks.)
         */
        for (i = 0; i < BPF_REG_FP; i++)
-               env->cur_state.regs[i].live = REG_LIVE_NONE;
-       for (i = 0; i < MAX_BPF_STACK / BPF_REG_SIZE; i++)
-               if (env->cur_state.stack_slot_type[i * BPF_REG_SIZE] == STACK_SPILL)
-                       env->cur_state.spilled_regs[i].live = REG_LIVE_NONE;
+               cur->regs[i].live = REG_LIVE_NONE;
+       for (i = 0; i < cur->allocated_stack / BPF_REG_SIZE; i++)
+               if (cur->stack[i].slot_type[0] == STACK_SPILL)
+                       cur->stack[i].spilled_ptr.live = REG_LIVE_NONE;
        return 0;
 }
 
@@ -3597,15 +3744,19 @@ static int ext_analyzer_insn_hook(struct bpf_verifier_env *env,
 
 static int do_check(struct bpf_verifier_env *env)
 {
-       struct bpf_verifier_state *state = &env->cur_state;
+       struct bpf_verifier_state *state;
        struct bpf_insn *insns = env->prog->insnsi;
-       struct bpf_reg_state *regs = state->regs;
+       struct bpf_reg_state *regs;
        int insn_cnt = env->prog->len;
        int insn_idx, prev_insn_idx = 0;
        int insn_processed = 0;
        bool do_print_state = false;
 
-       init_reg_state(regs);
+       state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
+       if (!state)
+               return -ENOMEM;
+       env->cur_state = state;
+       init_reg_state(env, state->regs);
        state->parent = NULL;
        insn_idx = 0;
        for (;;) {
@@ -3614,7 +3765,7 @@ static int do_check(struct bpf_verifier_env *env)
                int err;
 
                if (insn_idx >= insn_cnt) {
-                       verbose("invalid insn idx %d insn_cnt %d\n",
+                       verbose(env, "invalid insn idx %d insn_cnt %d\n",
                                insn_idx, insn_cnt);
                        return -EFAULT;
                }
@@ -3623,7 +3774,8 @@ static int do_check(struct bpf_verifier_env *env)
                class = BPF_CLASS(insn->code);
 
                if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
-                       verbose("BPF program is too large. Processed %d insn\n",
+                       verbose(env,
+                               "BPF program is too large. Processed %d insn\n",
                                insn_processed);
                        return -E2BIG;
                }
@@ -3633,12 +3785,12 @@ static int do_check(struct bpf_verifier_env *env)
                        return err;
                if (err == 1) {
                        /* found equivalent state, can prune the search */
-                       if (log_level) {
+                       if (env->log.level) {
                                if (do_print_state)
-                                       verbose("\nfrom %d to %d: safe\n",
+                                       verbose(env, "\nfrom %d to %d: safe\n",
                                                prev_insn_idx, insn_idx);
                                else
-                                       verbose("%d: safe\n", insn_idx);
+                                       verbose(env, "%d: safe\n", insn_idx);
                        }
                        goto process_bpf_exit;
                }
@@ -3646,25 +3798,27 @@ static int do_check(struct bpf_verifier_env *env)
                if (need_resched())
                        cond_resched();
 
-               if (log_level > 1 || (log_level && do_print_state)) {
-                       if (log_level > 1)
-                               verbose("%d:", insn_idx);
+               if (env->log.level > 1 || (env->log.level && do_print_state)) {
+                       if (env->log.level > 1)
+                               verbose(env, "%d:", insn_idx);
                        else
-                               verbose("\nfrom %d to %d:",
+                               verbose(env, "\nfrom %d to %d:",
                                        prev_insn_idx, insn_idx);
-                       print_verifier_state(&env->cur_state);
+                       print_verifier_state(env, state);
                        do_print_state = false;
                }
 
-               if (log_level) {
-                       verbose("%d: ", insn_idx);
-                       print_bpf_insn(env, insn);
+               if (env->log.level) {
+                       verbose(env, "%d: ", insn_idx);
+                       print_bpf_insn(verbose, env, insn,
+                                      env->allow_ptr_leaks);
                }
 
                err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
                if (err)
                        return err;
 
+               regs = cur_regs(env);
                if (class == BPF_ALU || class == BPF_ALU64) {
                        err = check_alu_op(env, insn);
                        if (err)
@@ -3714,7 +3868,7 @@ static int do_check(struct bpf_verifier_env *env)
                                 * src_reg == stack|map in some other branch.
                                 * Reject it.
                                 */
-                               verbose("same insn cannot be used with different pointers\n");
+                               verbose(env, "same insn cannot be used with different pointers\n");
                                return -EINVAL;
                        }
 
@@ -3754,14 +3908,14 @@ static int do_check(struct bpf_verifier_env *env)
                        } else if (dst_reg_type != *prev_dst_type &&
                                   (dst_reg_type == PTR_TO_CTX ||
                                    *prev_dst_type == PTR_TO_CTX)) {
-                               verbose("same insn cannot be used with different pointers\n");
+                               verbose(env, "same insn cannot be used with different pointers\n");
                                return -EINVAL;
                        }
 
                } else if (class == BPF_ST) {
                        if (BPF_MODE(insn->code) != BPF_MEM ||
                            insn->src_reg != BPF_REG_0) {
-                               verbose("BPF_ST uses reserved fields\n");
+                               verbose(env, "BPF_ST uses reserved fields\n");
                                return -EINVAL;
                        }
                        /* check src operand */
@@ -3784,7 +3938,7 @@ static int do_check(struct bpf_verifier_env *env)
                                    insn->off != 0 ||
                                    insn->src_reg != BPF_REG_0 ||
                                    insn->dst_reg != BPF_REG_0) {
-                                       verbose("BPF_CALL uses reserved fields\n");
+                                       verbose(env, "BPF_CALL uses reserved fields\n");
                                        return -EINVAL;
                                }
 
@@ -3797,7 +3951,7 @@ static int do_check(struct bpf_verifier_env *env)
                                    insn->imm != 0 ||
                                    insn->src_reg != BPF_REG_0 ||
                                    insn->dst_reg != BPF_REG_0) {
-                                       verbose("BPF_JA uses reserved fields\n");
+                                       verbose(env, "BPF_JA uses reserved fields\n");
                                        return -EINVAL;
                                }
 
@@ -3809,7 +3963,7 @@ static int do_check(struct bpf_verifier_env *env)
                                    insn->imm != 0 ||
                                    insn->src_reg != BPF_REG_0 ||
                                    insn->dst_reg != BPF_REG_0) {
-                                       verbose("BPF_EXIT uses reserved fields\n");
+                                       verbose(env, "BPF_EXIT uses reserved fields\n");
                                        return -EINVAL;
                                }
 
@@ -3824,13 +3978,18 @@ static int do_check(struct bpf_verifier_env *env)
                                        return err;
 
                                if (is_pointer_value(env, BPF_REG_0)) {
-                                       verbose("R0 leaks addr as return value\n");
+                                       verbose(env, "R0 leaks addr as return value\n");
                                        return -EACCES;
                                }
 
+                               err = check_return_code(env);
+                               if (err)
+                                       return err;
 process_bpf_exit:
-                               insn_idx = pop_stack(env, &prev_insn_idx);
-                               if (insn_idx < 0) {
+                               err = pop_stack(env, &prev_insn_idx, &insn_idx);
+                               if (err < 0) {
+                                       if (err != -ENOENT)
+                                               return err;
                                        break;
                                } else {
                                        do_print_state = true;
@@ -3856,19 +4015,19 @@ process_bpf_exit:
 
                                insn_idx++;
                        } else {
-                               verbose("invalid BPF_LD mode\n");
+                               verbose(env, "invalid BPF_LD mode\n");
                                return -EINVAL;
                        }
                } else {
-                       verbose("unknown insn class %d\n", class);
+                       verbose(env, "unknown insn class %d\n", class);
                        return -EINVAL;
                }
 
                insn_idx++;
        }
 
-       verbose("processed %d insns, stack depth %d\n",
-               insn_processed, env->prog->aux->stack_depth);
+       verbose(env, "processed %d insns, stack depth %d\n", insn_processed,
+               env->prog->aux->stack_depth);
        return 0;
 }
 
@@ -3880,7 +4039,8 @@ static int check_map_prealloc(struct bpf_map *map)
                !(map->map_flags & BPF_F_NO_PREALLOC);
 }
 
-static int check_map_prog_compatibility(struct bpf_map *map,
+static int check_map_prog_compatibility(struct bpf_verifier_env *env,
+                                       struct bpf_map *map,
                                        struct bpf_prog *prog)
 
 {
@@ -3891,12 +4051,12 @@ static int check_map_prog_compatibility(struct bpf_map *map,
         */
        if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
                if (!check_map_prealloc(map)) {
-                       verbose("perf_event programs can only use preallocated hash map\n");
+                       verbose(env, "perf_event programs can only use preallocated hash map\n");
                        return -EINVAL;
                }
                if (map->inner_map_meta &&
                    !check_map_prealloc(map->inner_map_meta)) {
-                       verbose("perf_event programs can only use preallocated inner hash map\n");
+                       verbose(env, "perf_event programs can only use preallocated inner hash map\n");
                        return -EINVAL;
                }
        }
@@ -3919,14 +4079,14 @@ static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
        for (i = 0; i < insn_cnt; i++, insn++) {
                if (BPF_CLASS(insn->code) == BPF_LDX &&
                    (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
-                       verbose("BPF_LDX uses reserved fields\n");
+                       verbose(env, "BPF_LDX uses reserved fields\n");
                        return -EINVAL;
                }
 
                if (BPF_CLASS(insn->code) == BPF_STX &&
                    ((BPF_MODE(insn->code) != BPF_MEM &&
                      BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
-                       verbose("BPF_STX uses reserved fields\n");
+                       verbose(env, "BPF_STX uses reserved fields\n");
                        return -EINVAL;
                }
 
@@ -3937,7 +4097,7 @@ static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
                        if (i == insn_cnt - 1 || insn[1].code != 0 ||
                            insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
                            insn[1].off != 0) {
-                               verbose("invalid bpf_ld_imm64 insn\n");
+                               verbose(env, "invalid bpf_ld_imm64 insn\n");
                                return -EINVAL;
                        }
 
@@ -3946,19 +4106,20 @@ static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
                                goto next_insn;
 
                        if (insn->src_reg != BPF_PSEUDO_MAP_FD) {
-                               verbose("unrecognized bpf_ld_imm64 insn\n");
+                               verbose(env,
+                                       "unrecognized bpf_ld_imm64 insn\n");
                                return -EINVAL;
                        }
 
                        f = fdget(insn->imm);
                        map = __bpf_map_get(f);
                        if (IS_ERR(map)) {
-                               verbose("fd %d is not pointing to valid bpf_map\n",
+                               verbose(env, "fd %d is not pointing to valid bpf_map\n",
                                        insn->imm);
                                return PTR_ERR(map);
                        }
 
-                       err = check_map_prog_compatibility(map, env->prog);
+                       err = check_map_prog_compatibility(env, map, env->prog);
                        if (err) {
                                fdput(f);
                                return err;
@@ -4067,7 +4228,7 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of
  */
 static int convert_ctx_accesses(struct bpf_verifier_env *env)
 {
-       const struct bpf_verifier_ops *ops = env->prog->aux->ops;
+       const struct bpf_verifier_ops *ops = env->ops;
        int i, cnt, size, ctx_field_size, delta = 0;
        const int insn_cnt = env->prog->len;
        struct bpf_insn insn_buf[16], *insn;
@@ -4080,7 +4241,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
                cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
                                        env->prog);
                if (cnt >= ARRAY_SIZE(insn_buf)) {
-                       verbose("bpf verifier is misconfigured\n");
+                       verbose(env, "bpf verifier is misconfigured\n");
                        return -EINVAL;
                } else if (cnt) {
                        new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
@@ -4128,7 +4289,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
                        u8 size_code;
 
                        if (type == BPF_WRITE) {
-                               verbose("bpf verifier narrow ctx access misconfigured\n");
+                               verbose(env, "bpf verifier narrow ctx access misconfigured\n");
                                return -EINVAL;
                        }
 
@@ -4147,7 +4308,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
                                              &target_size);
                if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
                    (ctx_field_size && !target_size)) {
-                       verbose("bpf verifier is misconfigured\n");
+                       verbose(env, "bpf verifier is misconfigured\n");
                        return -EINVAL;
                }
 
@@ -4229,7 +4390,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
 
                        cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf);
                        if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
-                               verbose("bpf verifier is misconfigured\n");
+                               verbose(env, "bpf verifier is misconfigured\n");
                                return -EINVAL;
                        }
 
@@ -4268,12 +4429,13 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
                        insn      = new_prog->insnsi + i + delta;
                }
 patch_call_imm:
-               fn = prog->aux->ops->get_func_proto(insn->imm);
+               fn = env->ops->get_func_proto(insn->imm);
                /* all functions that have prototype and verifier allowed
                 * programs to call them, must be real in-kernel functions
                 */
                if (!fn->func) {
-                       verbose("kernel subsystem misconfigured func %s#%d\n",
+                       verbose(env,
+                               "kernel subsystem misconfigured func %s#%d\n",
                                func_id_name(insn->imm), insn->imm);
                        return -EFAULT;
                }
@@ -4297,6 +4459,7 @@ static void free_states(struct bpf_verifier_env *env)
                if (sl)
                        while (sl != STATE_LIST_MARK) {
                                sln = sl->next;
+                               free_verifier_state(&sl->state, false);
                                kfree(sl);
                                sl = sln;
                        }
@@ -4307,16 +4470,21 @@ static void free_states(struct bpf_verifier_env *env)
 
 int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
 {
-       char __user *log_ubuf = NULL;
        struct bpf_verifier_env *env;
+       struct bpf_verifer_log *log;
        int ret = -EINVAL;
 
+       /* no program is valid */
+       if (ARRAY_SIZE(bpf_verifier_ops) == 0)
+               return -EINVAL;
+
        /* 'struct bpf_verifier_env' can be global, but since it's not small,
         * allocate/free it every time bpf_check() is called
         */
        env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
        if (!env)
                return -ENOMEM;
+       log = &env->log;
 
        env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) *
                                     (*prog)->len);
@@ -4324,6 +4492,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
        if (!env->insn_aux_data)
                goto err_free_env;
        env->prog = *prog;
+       env->ops = bpf_verifier_ops[env->prog->type];
 
        /* grab the mutex to protect few globals used by verifier */
        mutex_lock(&bpf_verifier_lock);
@@ -4332,23 +4501,15 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
                /* user requested verbose verifier output
                 * and supplied buffer to store the verification trace
                 */
-               log_level = attr->log_level;
-               log_ubuf = (char __user *) (unsigned long) attr->log_buf;
-               log_size = attr->log_size;
-               log_len = 0;
+               log->level = attr->log_level;
+               log->ubuf = (char __user *) (unsigned long) attr->log_buf;
+               log->len_total = attr->log_size;
 
                ret = -EINVAL;
-               /* log_* values have to be sane */
-               if (log_size < 128 || log_size > UINT_MAX >> 8 ||
-                   log_level == 0 || log_ubuf == NULL)
-                       goto err_unlock;
-
-               ret = -ENOMEM;
-               log_buf = vmalloc(log_size);
-               if (!log_buf)
+               /* log attributes have to be sane */
+               if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 ||
+                   !log->level || !log->ubuf)
                        goto err_unlock;
-       } else {
-               log_level = 0;
        }
 
        env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
@@ -4373,9 +4534,11 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr)
        env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
 
        ret = do_check(env);
+       free_verifier_state(env->cur_state, true);
+       env->cur_state = NULL;
 
 skip_full_check:
-       while (pop_stack(env, NULL) >= 0);
+       while (!pop_stack(env, NULL, NULL));
        free_states(env);
 
        if (ret == 0)
@@ -4385,17 +4548,11 @@ skip_full_check:
        if (ret == 0)
                ret = fixup_bpf_calls(env);
 
-       if (log_level && log_len >= log_size - 1) {
-               BUG_ON(log_len >= log_size);
-               /* verifier log exceeded user supplied buffer */
+       if (log->level && bpf_verifier_log_full(log))
                ret = -ENOSPC;
-               /* fall through to return what was recorded */
-       }
-
-       /* copy verifier log back to user space including trailing zero */
-       if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) {
+       if (log->level && !log->ubuf) {
                ret = -EFAULT;
-               goto free_log_buf;
+               goto err_release_maps;
        }
 
        if (ret == 0 && env->used_map_cnt) {
@@ -4406,7 +4563,7 @@ skip_full_check:
 
                if (!env->prog->aux->used_maps) {
                        ret = -ENOMEM;
-                       goto free_log_buf;
+                       goto err_release_maps;
                }
 
                memcpy(env->prog->aux->used_maps, env->used_maps,
@@ -4419,9 +4576,7 @@ skip_full_check:
                convert_pseudo_ld_imm64(env);
        }
 
-free_log_buf:
-       if (log_level)
-               vfree(log_buf);
+err_release_maps:
        if (!env->prog->aux->used_maps)
                /* if we didn't copy map pointers into bpf_prog_info, release
                 * them now. Otherwise free_bpf_prog_info() will release them.
@@ -4436,12 +4591,23 @@ err_free_env:
        return ret;
 }
 
+static const struct bpf_verifier_ops * const bpf_analyzer_ops[] = {
+#ifdef CONFIG_NET
+       [BPF_PROG_TYPE_XDP]             = &xdp_analyzer_ops,
+       [BPF_PROG_TYPE_SCHED_CLS]       = &tc_cls_act_analyzer_ops,
+#endif
+};
+
 int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
                 void *priv)
 {
        struct bpf_verifier_env *env;
        int ret;
 
+       if (prog->type >= ARRAY_SIZE(bpf_analyzer_ops) ||
+           !bpf_analyzer_ops[prog->type])
+               return -EOPNOTSUPP;
+
        env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
        if (!env)
                return -ENOMEM;
@@ -4452,14 +4618,13 @@ int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
        if (!env->insn_aux_data)
                goto err_free_env;
        env->prog = prog;
+       env->ops = bpf_analyzer_ops[env->prog->type];
        env->analyzer_ops = ops;
        env->analyzer_priv = priv;
 
        /* grab the mutex to protect few globals used by verifier */
        mutex_lock(&bpf_verifier_lock);
 
-       log_level = 0;
-
        env->strict_alignment = false;
        if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
                env->strict_alignment = true;
@@ -4478,9 +4643,11 @@ int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops,
        env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
 
        ret = do_check(env);
+       free_verifier_state(env->cur_state, true);
+       env->cur_state = NULL;
 
 skip_full_check:
-       while (pop_stack(env, NULL) >= 0);
+       while (!pop_stack(env, NULL, NULL));
        free_states(env);
 
        mutex_unlock(&bpf_verifier_lock);