Thomas Gleixner | 25763b3 | 2019-05-28 10:10:09 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3 | */ |
| 4 | #ifndef _LINUX_BPF_VERIFIER_H |
| 5 | #define _LINUX_BPF_VERIFIER_H 1 |
| 6 | |
| 7 | #include <linux/bpf.h> /* for enum bpf_reg_type */ |
| 8 | #include <linux/filter.h> /* for MAX_BPF_STACK */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9 | #include <linux/tnum.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 11 | /* Maximum variable offset umax_value permitted when resolving memory accesses. |
| 12 | * In practice this is far bigger than any realistic pointer offset; this limit |
| 13 | * ensures that umax_value + (int)off + (int)size cannot overflow a u64. |
| 14 | */ |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 15 | #define BPF_MAX_VAR_OFF (1 << 29) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 16 | /* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures |
| 17 | * that converting umax_value to int cannot overflow. |
| 18 | */ |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 19 | #define BPF_MAX_VAR_SIZ (1 << 29) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 20 | |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 21 | /* Liveness marks, used for registers and spilled-regs (in stack slots). |
| 22 | * Read marks propagate upwards until they find a write mark; they record that |
| 23 | * "one of this state's descendants read this reg" (and therefore the reg is |
| 24 | * relevant for states_equal() checks). |
| 25 | * Write marks collect downwards and do not propagate; they record that "the |
| 26 | * straight-line code that reached this state (from its parent) wrote this reg" |
| 27 | * (and therefore that reads propagated from this state or its descendants |
| 28 | * should not propagate to its parent). |
| 29 | * A state with a write mark can receive read marks; it just won't propagate |
| 30 | * them to its parent, since the write mark is a property, not of the state, |
| 31 | * but of the link between it and its parent. See mark_reg_read() and |
| 32 | * mark_stack_slot_read() in kernel/bpf/verifier.c. |
| 33 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 34 | enum bpf_reg_liveness { |
| 35 | REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 36 | REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */ |
| 37 | REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */ |
| 38 | REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64, |
| 39 | REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */ |
| 40 | REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 43 | struct bpf_reg_state { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 44 | /* Ordering of fields matters. See states_equal() */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 45 | enum bpf_reg_type type; |
| 46 | union { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 47 | /* valid when type == PTR_TO_PACKET */ |
| 48 | u16 range; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 49 | |
| 50 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | |
| 51 | * PTR_TO_MAP_VALUE_OR_NULL |
| 52 | */ |
| 53 | struct bpf_map *map_ptr; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 54 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 55 | u32 btf_id; /* for PTR_TO_BTF_ID */ |
| 56 | |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 57 | /* Max size from any of the above. */ |
| 58 | unsigned long raw; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 59 | }; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 60 | /* Fixed part of pointer offset, pointer types only */ |
| 61 | s32 off; |
| 62 | /* For PTR_TO_PACKET, used to find other pointers with the same variable |
| 63 | * offset, so they can share range knowledge. |
| 64 | * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we |
| 65 | * came from, when one is tested for != NULL. |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 66 | * For PTR_TO_SOCKET this is used to share which pointers retain the |
| 67 | * same reference to the socket, to determine proper reference freeing. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 68 | */ |
Alexei Starovoitov | d2a4dd3 | 2016-12-07 10:57:59 -0800 | [diff] [blame] | 69 | u32 id; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 70 | /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned |
| 71 | * from a pointer-cast helper, bpf_sk_fullsock() and |
| 72 | * bpf_tcp_sock(). |
| 73 | * |
| 74 | * Consider the following where "sk" is a reference counted |
| 75 | * pointer returned from "sk = bpf_sk_lookup_tcp();": |
| 76 | * |
| 77 | * 1: sk = bpf_sk_lookup_tcp(); |
| 78 | * 2: if (!sk) { return 0; } |
| 79 | * 3: fullsock = bpf_sk_fullsock(sk); |
| 80 | * 4: if (!fullsock) { bpf_sk_release(sk); return 0; } |
| 81 | * 5: tp = bpf_tcp_sock(fullsock); |
| 82 | * 6: if (!tp) { bpf_sk_release(sk); return 0; } |
| 83 | * 7: bpf_sk_release(sk); |
| 84 | * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain |
| 85 | * |
| 86 | * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and |
| 87 | * "tp" ptr should be invalidated also. In order to do that, |
| 88 | * the reg holding "fullsock" and "sk" need to remember |
| 89 | * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id |
| 90 | * such that the verifier can reset all regs which have |
| 91 | * ref_obj_id matching the sk_reg->id. |
| 92 | * |
| 93 | * sk_reg->ref_obj_id is set to sk_reg->id at line 1. |
| 94 | * sk_reg->id will stay as NULL-marking purpose only. |
| 95 | * After NULL-marking is done, sk_reg->id can be reset to 0. |
| 96 | * |
| 97 | * After "fullsock = bpf_sk_fullsock(sk);" at line 3, |
| 98 | * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id. |
| 99 | * |
| 100 | * After "tp = bpf_tcp_sock(fullsock);" at line 5, |
| 101 | * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id |
| 102 | * which is the same as sk_reg->ref_obj_id. |
| 103 | * |
| 104 | * From the verifier perspective, if sk, fullsock and tp |
| 105 | * are not NULL, they are the same ptr with different |
| 106 | * reg->type. In particular, bpf_sk_release(tp) is also |
| 107 | * allowed and has the same effect as bpf_sk_release(sk). |
| 108 | */ |
| 109 | u32 ref_obj_id; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 110 | /* For scalar types (SCALAR_VALUE), this represents our knowledge of |
| 111 | * the actual value. |
| 112 | * For pointer types, this represents the variable part of the offset |
| 113 | * from the pointed-to object, and is shared with all bpf_reg_states |
| 114 | * with the same id as us. |
| 115 | */ |
| 116 | struct tnum var_off; |
Alexei Starovoitov | d2a4dd3 | 2016-12-07 10:57:59 -0800 | [diff] [blame] | 117 | /* Used to determine if any memory access using this register will |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 118 | * result in a bad access. |
| 119 | * These refer to the same value as var_off, not necessarily the actual |
| 120 | * contents of the register. |
Alexei Starovoitov | d2a4dd3 | 2016-12-07 10:57:59 -0800 | [diff] [blame] | 121 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 122 | s64 smin_value; /* minimum possible (s64)value */ |
| 123 | s64 smax_value; /* maximum possible (s64)value */ |
| 124 | u64 umin_value; /* minimum possible (u64)value */ |
| 125 | u64 umax_value; /* maximum possible (u64)value */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 126 | s32 s32_min_value; /* minimum possible (s32)value */ |
| 127 | s32 s32_max_value; /* maximum possible (s32)value */ |
| 128 | u32 u32_min_value; /* minimum possible (u32)value */ |
| 129 | u32 u32_max_value; /* maximum possible (u32)value */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 130 | /* parentage chain for liveness checking */ |
| 131 | struct bpf_reg_state *parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 132 | /* Inside the callee two registers can be both PTR_TO_STACK like |
| 133 | * R1=fp-8 and R2=fp-8, but one of them points to this function stack |
| 134 | * while another to the caller's stack. To differentiate them 'frameno' |
| 135 | * is used which is an index in bpf_verifier_state->frame[] array |
| 136 | * pointing to bpf_func_state. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 137 | */ |
| 138 | u32 frameno; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 139 | /* Tracks subreg definition. The stored value is the insn_idx of the |
| 140 | * writing insn. This is safe because subreg_def is used before any insn |
| 141 | * patching which only happens after main verification finished. |
| 142 | */ |
| 143 | s32 subreg_def; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 144 | enum bpf_reg_liveness live; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 145 | /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */ |
| 146 | bool precise; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | enum bpf_stack_slot_type { |
| 150 | STACK_INVALID, /* nothing was stored in this stack slot */ |
| 151 | STACK_SPILL, /* register spilled into stack */ |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 152 | STACK_MISC, /* BPF program wrote some data into this slot */ |
| 153 | STACK_ZERO, /* BPF program wrote constant zero */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ |
| 157 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 158 | struct bpf_stack_state { |
| 159 | struct bpf_reg_state spilled_ptr; |
| 160 | u8 slot_type[BPF_REG_SIZE]; |
| 161 | }; |
| 162 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 163 | struct bpf_reference_state { |
| 164 | /* Track each reference created with a unique id, even if the same |
| 165 | * instruction creates the reference multiple times (eg, via CALL). |
| 166 | */ |
| 167 | int id; |
| 168 | /* Instruction where the allocation of this reference occurred. This |
| 169 | * is used purely to inform the user of a reference leak. |
| 170 | */ |
| 171 | int insn_idx; |
| 172 | }; |
| 173 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 174 | /* state of the program: |
| 175 | * type of all registers and stack info |
| 176 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 177 | struct bpf_func_state { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 178 | struct bpf_reg_state regs[MAX_BPF_REG]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 179 | /* index of call instruction that called into this func */ |
| 180 | int callsite; |
| 181 | /* stack frame number of this function state from pov of |
| 182 | * enclosing bpf_verifier_state. |
| 183 | * 0 = main function, 1 = first callee. |
| 184 | */ |
| 185 | u32 frameno; |
| 186 | /* subprog number == index within subprog_stack_depth |
| 187 | * zero == main subprog |
| 188 | */ |
| 189 | u32 subprogno; |
| 190 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 191 | /* The following fields should be last. See copy_func_state() */ |
| 192 | int acquired_refs; |
| 193 | struct bpf_reference_state *refs; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 194 | int allocated_stack; |
| 195 | struct bpf_stack_state *stack; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 196 | }; |
| 197 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 198 | struct bpf_idx_pair { |
| 199 | u32 prev_idx; |
| 200 | u32 idx; |
| 201 | }; |
| 202 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 203 | #define MAX_CALL_FRAMES 8 |
| 204 | struct bpf_verifier_state { |
| 205 | /* call stack tracking */ |
| 206 | struct bpf_func_state *frame[MAX_CALL_FRAMES]; |
Alexei Starovoitov | 2589726d | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 207 | struct bpf_verifier_state *parent; |
| 208 | /* |
| 209 | * 'branches' field is the number of branches left to explore: |
| 210 | * 0 - all possible paths from this state reached bpf_exit or |
| 211 | * were safely pruned |
| 212 | * 1 - at least one path is being explored. |
| 213 | * This state hasn't reached bpf_exit |
| 214 | * 2 - at least two paths are being explored. |
| 215 | * This state is an immediate parent of two children. |
| 216 | * One is fallthrough branch with branches==1 and another |
| 217 | * state is pushed into stack (to be explored later) also with |
| 218 | * branches==1. The parent of this state has branches==1. |
| 219 | * The verifier state tree connected via 'parent' pointer looks like: |
| 220 | * 1 |
| 221 | * 1 |
| 222 | * 2 -> 1 (first 'if' pushed into stack) |
| 223 | * 1 |
| 224 | * 2 -> 1 (second 'if' pushed into stack) |
| 225 | * 1 |
| 226 | * 1 |
| 227 | * 1 bpf_exit. |
| 228 | * |
| 229 | * Once do_check() reaches bpf_exit, it calls update_branch_counts() |
| 230 | * and the verifier state tree will look: |
| 231 | * 1 |
| 232 | * 1 |
| 233 | * 2 -> 1 (first 'if' pushed into stack) |
| 234 | * 1 |
| 235 | * 1 -> 1 (second 'if' pushed into stack) |
| 236 | * 0 |
| 237 | * 0 |
| 238 | * 0 bpf_exit. |
| 239 | * After pop_stack() the do_check() will resume at second 'if'. |
| 240 | * |
| 241 | * If is_state_visited() sees a state with branches > 0 it means |
| 242 | * there is a loop. If such state is exactly equal to the current state |
| 243 | * it's an infinite loop. Note states_equal() checks for states |
| 244 | * equvalency, so two states being 'states_equal' does not mean |
| 245 | * infinite loop. The exact comparison is provided by |
| 246 | * states_maybe_looping() function. It's a stronger pre-check and |
| 247 | * much faster than states_equal(). |
| 248 | * |
| 249 | * This algorithm may not find all possible infinite loops or |
| 250 | * loop iteration count may be too high. |
| 251 | * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in. |
| 252 | */ |
| 253 | u32 branches; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 254 | u32 insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 255 | u32 curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 256 | u32 active_spin_lock; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 257 | bool speculative; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 258 | |
| 259 | /* first and last insn idx of this verifier state */ |
| 260 | u32 first_insn_idx; |
| 261 | u32 last_insn_idx; |
| 262 | /* jmp history recorded from first to last. |
| 263 | * backtracking is using it to go from last to first. |
| 264 | * For most states jmp_history_cnt is [0-3]. |
| 265 | * For loops can go up to ~40. |
| 266 | */ |
| 267 | struct bpf_idx_pair *jmp_history; |
| 268 | u32 jmp_history_cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 269 | }; |
| 270 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 271 | #define bpf_get_spilled_reg(slot, frame) \ |
| 272 | (((slot < frame->allocated_stack / BPF_REG_SIZE) && \ |
| 273 | (frame->stack[slot].slot_type[0] == STACK_SPILL)) \ |
| 274 | ? &frame->stack[slot].spilled_ptr : NULL) |
| 275 | |
| 276 | /* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */ |
| 277 | #define bpf_for_each_spilled_reg(iter, frame, reg) \ |
| 278 | for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \ |
| 279 | iter < frame->allocated_stack / BPF_REG_SIZE; \ |
| 280 | iter++, reg = bpf_get_spilled_reg(iter, frame)) |
| 281 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 282 | /* linked list of verifier states used to prune search */ |
| 283 | struct bpf_verifier_state_list { |
| 284 | struct bpf_verifier_state state; |
| 285 | struct bpf_verifier_state_list *next; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 286 | int miss_cnt, hit_cnt; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 287 | }; |
| 288 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 289 | /* Possible states for alu_state member. */ |
| 290 | #define BPF_ALU_SANITIZE_SRC 1U |
| 291 | #define BPF_ALU_SANITIZE_DST 2U |
| 292 | #define BPF_ALU_NEG_VALUE (1U << 2) |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 293 | #define BPF_ALU_NON_POINTER (1U << 3) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 294 | #define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \ |
| 295 | BPF_ALU_SANITIZE_DST) |
| 296 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 297 | struct bpf_insn_aux_data { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 298 | union { |
| 299 | enum bpf_reg_type ptr_type; /* pointer type for load/store insns */ |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 300 | unsigned long map_ptr_state; /* pointer/poison value for maps */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 301 | s32 call_imm; /* saved imm field of call insn */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 302 | u32 alu_limit; /* limit for add/sub register with pointer */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 303 | struct { |
| 304 | u32 map_index; /* index into used_maps[] */ |
| 305 | u32 map_off; /* offset from value base address */ |
| 306 | }; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 307 | }; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 308 | u64 map_key_state; /* constant (32 bit) key tracking for maps */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 309 | int ctx_field_size; /* the ctx field size for load insn, maybe 0 */ |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 310 | int sanitize_stack_off; /* stack slot to be cleared */ |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 311 | u32 seen; /* this insn was processed by the verifier at env->pass_cnt */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 312 | bool zext_dst; /* this insn zero extends dst reg */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 313 | u8 alu_state; /* used in combination with alu_limit */ |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 314 | |
| 315 | /* below fields are initialized once */ |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 316 | unsigned int orig_idx; /* original instruction index */ |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 317 | bool prune_point; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 318 | }; |
| 319 | |
| 320 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ |
| 321 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 322 | #define BPF_VERIFIER_TMP_LOG_SIZE 1024 |
| 323 | |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 324 | struct bpf_verifier_log { |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 325 | u32 level; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 326 | char kbuf[BPF_VERIFIER_TMP_LOG_SIZE]; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 327 | char __user *ubuf; |
| 328 | u32 len_used; |
| 329 | u32 len_total; |
| 330 | }; |
| 331 | |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 332 | static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log) |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 333 | { |
| 334 | return log->len_used >= log->len_total - 1; |
| 335 | } |
| 336 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 337 | #define BPF_LOG_LEVEL1 1 |
| 338 | #define BPF_LOG_LEVEL2 2 |
| 339 | #define BPF_LOG_STATS 4 |
| 340 | #define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2) |
| 341 | #define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS) |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 342 | #define BPF_LOG_KERNEL (BPF_LOG_MASK + 1) /* kernel internal flag */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 343 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 344 | static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log) |
| 345 | { |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 346 | return (log->level && log->ubuf && !bpf_verifier_log_full(log)) || |
| 347 | log->level == BPF_LOG_KERNEL; |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 350 | #define BPF_MAX_SUBPROGS 256 |
| 351 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 352 | struct bpf_subprog_info { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 353 | /* 'start' has to be the first field otherwise find_subprog() won't work */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 354 | u32 start; /* insn idx of function entry point */ |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 355 | u32 linfo_idx; /* The idx to the main_prog->aux->linfo */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 356 | u16 stack_depth; /* max. stack depth used by this function */ |
| 357 | }; |
| 358 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 359 | /* single container for all structs |
| 360 | * one verifier_env per bpf_check() call |
| 361 | */ |
| 362 | struct bpf_verifier_env { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 363 | u32 insn_idx; |
| 364 | u32 prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 365 | struct bpf_prog *prog; /* eBPF program being verified */ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 366 | const struct bpf_verifier_ops *ops; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 367 | struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */ |
| 368 | int stack_size; /* number of states to be processed */ |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 369 | bool strict_alignment; /* perform strict pointer alignment checks */ |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 370 | bool test_state_freq; /* test verifier with different pruning frequency */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 371 | struct bpf_verifier_state *cur_state; /* current verifier state */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 372 | struct bpf_verifier_state_list **explored_states; /* search pruning optimization */ |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 373 | struct bpf_verifier_state_list *free_list; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 374 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
| 375 | u32 used_map_cnt; /* number of used maps */ |
| 376 | u32 id_gen; /* used to generate unique reg IDs */ |
| 377 | bool allow_ptr_leaks; |
| 378 | bool seen_direct_write; |
| 379 | struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */ |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 380 | const struct bpf_line_info *prev_linfo; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 381 | struct bpf_verifier_log log; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 382 | struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1]; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 383 | struct { |
| 384 | int *insn_state; |
| 385 | int *insn_stack; |
| 386 | int cur_stack; |
| 387 | } cfg; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 388 | u32 pass_cnt; /* number of times do_check() was called */ |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 389 | u32 subprog_cnt; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 390 | /* number of instructions analyzed by the verifier */ |
Alexei Starovoitov | 2589726d | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 391 | u32 prev_insn_processed, insn_processed; |
| 392 | /* number of jmps, calls, exits analyzed so far */ |
| 393 | u32 prev_jmps_processed, jmps_processed; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 394 | /* total verification time */ |
| 395 | u64 verification_time; |
| 396 | /* maximum number of verifier states kept in 'branching' instructions */ |
| 397 | u32 max_states_per_insn; |
| 398 | /* total number of allocated verifier states */ |
| 399 | u32 total_states; |
| 400 | /* some states are freed during program analysis. |
| 401 | * this is peak number of states. this number dominates kernel |
| 402 | * memory consumption during verification |
| 403 | */ |
| 404 | u32 peak_states; |
| 405 | /* longest register parentage chain walked for liveness marking */ |
| 406 | u32 longest_mark_read_walk; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 407 | }; |
| 408 | |
Mathieu Malaterre | be2d04d | 2018-05-16 22:27:41 +0200 | [diff] [blame] | 409 | __printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log, |
| 410 | const char *fmt, va_list args); |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 411 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 412 | const char *fmt, ...); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 413 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 414 | const char *fmt, ...); |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 415 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 416 | static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 417 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 418 | struct bpf_verifier_state *cur = env->cur_state; |
| 419 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 420 | return cur->frame[cur->curframe]; |
| 421 | } |
| 422 | |
| 423 | static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env) |
| 424 | { |
| 425 | return cur_func(env)->regs; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Quentin Monnet | a40a263 | 2018-11-09 13:03:31 +0000 | [diff] [blame] | 428 | int bpf_prog_offload_verifier_prep(struct bpf_prog *prog); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 429 | int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, |
| 430 | int insn_idx, int prev_insn_idx); |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 431 | int bpf_prog_offload_finalize(struct bpf_verifier_env *env); |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 432 | void |
| 433 | bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, |
| 434 | struct bpf_insn *insn); |
| 435 | void |
| 436 | bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt); |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 437 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 438 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 439 | const struct bpf_reg_state *reg, int regno); |
| 440 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 441 | #endif /* _LINUX_BPF_VERIFIER_H */ |