Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2016 Facebook |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 4 | * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5 | */ |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 6 | #include <uapi/linux/btf.h> |
Jakub Kicinski | aef2fed | 2021-12-15 18:55:37 -0800 | [diff] [blame] | 7 | #include <linux/bpf-cgroup.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/bpf.h> |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 12 | #include <linux/btf.h> |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13 | #include <linux/bpf_verifier.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14 | #include <linux/filter.h> |
| 15 | #include <net/netlink.h> |
| 16 | #include <linux/file.h> |
| 17 | #include <linux/vmalloc.h> |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 18 | #include <linux/stringify.h> |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 19 | #include <linux/bsearch.h> |
| 20 | #include <linux/sort.h> |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 21 | #include <linux/perf_event.h> |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 22 | #include <linux/ctype.h> |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 23 | #include <linux/error-injection.h> |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 24 | #include <linux/bpf_lsm.h> |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 25 | #include <linux/btf_ids.h> |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 26 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 27 | #include "disasm.h" |
| 28 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 29 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
Alexei Starovoitov | 91cc1a9 | 2019-11-14 10:57:15 -0800 | [diff] [blame] | 30 | #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \ |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 31 | [_id] = & _name ## _verifier_ops, |
| 32 | #define BPF_MAP_TYPE(_id, _ops) |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 33 | #define BPF_LINK_TYPE(_id, _name) |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 34 | #include <linux/bpf_types.h> |
| 35 | #undef BPF_PROG_TYPE |
| 36 | #undef BPF_MAP_TYPE |
Andrii Nakryiko | f2e10bf | 2020-04-28 17:16:08 -0700 | [diff] [blame] | 37 | #undef BPF_LINK_TYPE |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 40 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 41 | * instruction by instruction and updates register/stack state. |
| 42 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 43 | * |
| 44 | * The first pass is depth-first-search to check that the program is a DAG. |
| 45 | * It rejects the following programs: |
| 46 | * - larger than BPF_MAXINSNS insns |
| 47 | * - if loop is present (detected via back-edge) |
| 48 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 49 | * - out of bounds or malformed jumps |
| 50 | * The second pass is all possible path descent from the 1st insn. |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 51 | * Since it's analyzing all paths through the program, the length of the |
Gary Lin | eba38a9 | 2017-03-01 16:25:51 +0800 | [diff] [blame] | 52 | * analysis is limited to 64k insn, which may be hit even if total number of |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 53 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 54 | * Number of 'branches to be analyzed' is limited to 1k |
| 55 | * |
| 56 | * On entry to each instruction, each register has a type, and the instruction |
| 57 | * changes the types of the registers depending on instruction semantics. |
| 58 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 59 | * copied to R1. |
| 60 | * |
| 61 | * All registers are 64-bit. |
| 62 | * R0 - return register |
| 63 | * R1-R5 argument passing registers |
| 64 | * R6-R9 callee saved registers |
| 65 | * R10 - frame pointer read-only |
| 66 | * |
| 67 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 68 | * and has type PTR_TO_CTX. |
| 69 | * |
| 70 | * Verifier tracks arithmetic operations on pointers in case: |
| 71 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 72 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 73 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 74 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 75 | * that it wants to construct a pointer to some element within stack. |
| 76 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 77 | * (and -20 constant is saved for further stack bounds checking). |
| 78 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 79 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 80 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 81 | * means the register has some value, but it's not a valid pointer. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 82 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 83 | * |
| 84 | * When verifier sees load or store instructions the type of base register |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 85 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, PTR_TO_STACK, PTR_TO_SOCKET. These are |
| 86 | * four pointer types recognized by check_mem_access() function. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 87 | * |
| 88 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 89 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 90 | * |
| 91 | * registers used to pass values to function calls are checked against |
| 92 | * function argument constraints. |
| 93 | * |
| 94 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 95 | * It means that the register type passed to this function must be |
| 96 | * PTR_TO_STACK and it will be used inside the function as |
| 97 | * 'pointer to map element key' |
| 98 | * |
| 99 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 100 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 101 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 102 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 103 | * |
| 104 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 105 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 106 | * 2nd argument should be a pointer to stack, which will be used inside |
| 107 | * the helper function as a pointer to map element key. |
| 108 | * |
| 109 | * On the kernel side the helper function looks like: |
| 110 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 111 | * { |
| 112 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 113 | * void *key = (void *) (unsigned long) r2; |
| 114 | * void *value; |
| 115 | * |
| 116 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 117 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 118 | * the stack of eBPF program. |
| 119 | * } |
| 120 | * |
| 121 | * Corresponding eBPF program may look like: |
| 122 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 123 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 124 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 125 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 126 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 127 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 128 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 129 | * |
| 130 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 131 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 132 | * and were initialized prior to this call. |
| 133 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 134 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 135 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 136 | * returns either pointer to map value or NULL. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 137 | * |
| 138 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 139 | * insn, the register holding that pointer in the true branch changes state to |
| 140 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 141 | * branch. See check_cond_jmp_op(). |
| 142 | * |
| 143 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 144 | * are set to NOT_INIT to indicate that they are no longer readable. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 145 | * |
| 146 | * The following reference types represent a potential reference to a kernel |
| 147 | * resource which, after first being allocated, must be checked and freed by |
| 148 | * the BPF program: |
| 149 | * - PTR_TO_SOCKET_OR_NULL, PTR_TO_SOCKET |
| 150 | * |
| 151 | * When the verifier sees a helper call return a reference type, it allocates a |
| 152 | * pointer id for the reference and stores it in the current function state. |
| 153 | * Similar to the way that PTR_TO_MAP_VALUE_OR_NULL is converted into |
| 154 | * PTR_TO_MAP_VALUE, PTR_TO_SOCKET_OR_NULL becomes PTR_TO_SOCKET when the type |
| 155 | * passes through a NULL-check conditional. For the branch wherein the state is |
| 156 | * changed to CONST_IMM, the verifier releases the reference. |
Joe Stringer | 6acc9b4 | 2018-10-02 13:35:36 -0700 | [diff] [blame] | 157 | * |
| 158 | * For each helper function that allocates a reference, such as |
| 159 | * bpf_sk_lookup_tcp(), there is a corresponding release function, such as |
| 160 | * bpf_sk_release(). When a reference type passes into the release function, |
| 161 | * the verifier also releases the reference. If any unchecked or unreleased |
| 162 | * reference remains at the end of the program, the verifier rejects it. |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 163 | */ |
| 164 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 165 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 166 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 167 | /* verifer state is 'st' |
| 168 | * before processing instruction 'insn_idx' |
| 169 | * and after processing instruction 'prev_insn_idx' |
| 170 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 171 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 172 | int insn_idx; |
| 173 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 174 | struct bpf_verifier_stack_elem *next; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 175 | /* length of verifier log at the time this state was pushed on stack */ |
| 176 | u32 log_pos; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 179 | #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192 |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 180 | #define BPF_COMPLEXITY_LIMIT_STATES 64 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 181 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 182 | #define BPF_MAP_KEY_POISON (1ULL << 63) |
| 183 | #define BPF_MAP_KEY_SEEN (1ULL << 62) |
| 184 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 185 | #define BPF_MAP_PTR_UNPRIV 1UL |
| 186 | #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \ |
| 187 | POISON_POINTER_DELTA)) |
| 188 | #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV)) |
| 189 | |
| 190 | static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux) |
| 191 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 192 | return BPF_MAP_PTR(aux->map_ptr_state) == BPF_MAP_PTR_POISON; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux) |
| 196 | { |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 197 | return aux->map_ptr_state & BPF_MAP_PTR_UNPRIV; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux, |
| 201 | const struct bpf_map *map, bool unpriv) |
| 202 | { |
| 203 | BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV); |
| 204 | unpriv |= bpf_map_ptr_unpriv(aux); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 205 | aux->map_ptr_state = (unsigned long)map | |
| 206 | (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL); |
| 207 | } |
| 208 | |
| 209 | static bool bpf_map_key_poisoned(const struct bpf_insn_aux_data *aux) |
| 210 | { |
| 211 | return aux->map_key_state & BPF_MAP_KEY_POISON; |
| 212 | } |
| 213 | |
| 214 | static bool bpf_map_key_unseen(const struct bpf_insn_aux_data *aux) |
| 215 | { |
| 216 | return !(aux->map_key_state & BPF_MAP_KEY_SEEN); |
| 217 | } |
| 218 | |
| 219 | static u64 bpf_map_key_immediate(const struct bpf_insn_aux_data *aux) |
| 220 | { |
| 221 | return aux->map_key_state & ~(BPF_MAP_KEY_SEEN | BPF_MAP_KEY_POISON); |
| 222 | } |
| 223 | |
| 224 | static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) |
| 225 | { |
| 226 | bool poisoned = bpf_map_key_poisoned(aux); |
| 227 | |
| 228 | aux->map_key_state = state | BPF_MAP_KEY_SEEN | |
| 229 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 230 | } |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 231 | |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 232 | static bool bpf_pseudo_call(const struct bpf_insn *insn) |
| 233 | { |
| 234 | return insn->code == (BPF_JMP | BPF_CALL) && |
| 235 | insn->src_reg == BPF_PSEUDO_CALL; |
| 236 | } |
| 237 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 238 | static bool bpf_pseudo_kfunc_call(const struct bpf_insn *insn) |
| 239 | { |
| 240 | return insn->code == (BPF_JMP | BPF_CALL) && |
| 241 | insn->src_reg == BPF_PSEUDO_KFUNC_CALL; |
| 242 | } |
| 243 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 244 | struct bpf_call_arg_meta { |
| 245 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 246 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 247 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 248 | int regno; |
| 249 | int access_size; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 250 | int mem_size; |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 251 | u64 msize_max_value; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 252 | int ref_obj_id; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 253 | int map_uid; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 254 | int func_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 255 | struct btf *btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 256 | u32 btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 257 | struct btf *ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 258 | u32 ret_btf_id; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 259 | u32 subprogno; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 260 | }; |
| 261 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 262 | struct btf *btf_vmlinux; |
| 263 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 264 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 265 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 266 | static const struct bpf_line_info * |
| 267 | find_linfo(const struct bpf_verifier_env *env, u32 insn_off) |
| 268 | { |
| 269 | const struct bpf_line_info *linfo; |
| 270 | const struct bpf_prog *prog; |
| 271 | u32 i, nr_linfo; |
| 272 | |
| 273 | prog = env->prog; |
| 274 | nr_linfo = prog->aux->nr_linfo; |
| 275 | |
| 276 | if (!nr_linfo || insn_off >= prog->len) |
| 277 | return NULL; |
| 278 | |
| 279 | linfo = prog->aux->linfo; |
| 280 | for (i = 1; i < nr_linfo; i++) |
| 281 | if (insn_off < linfo[i].insn_off) |
| 282 | break; |
| 283 | |
| 284 | return &linfo[i - 1]; |
| 285 | } |
| 286 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 287 | void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt, |
| 288 | va_list args) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 289 | { |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 290 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 291 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 292 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 293 | |
| 294 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 295 | "verifier log line truncated - local buffer too short\n"); |
| 296 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 297 | if (log->level == BPF_LOG_KERNEL) { |
Hou Tao | 436d404 | 2021-12-01 15:34:57 +0800 | [diff] [blame] | 298 | bool newline = n > 0 && log->kbuf[n - 1] == '\n'; |
| 299 | |
| 300 | pr_err("BPF: %s%s", log->kbuf, newline ? "" : "\n"); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
Hou Tao | 436d404 | 2021-12-01 15:34:57 +0800 | [diff] [blame] | 303 | |
| 304 | n = min(log->len_total - log->len_used - 1, n); |
| 305 | log->kbuf[n] = '\0'; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 306 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 307 | log->len_used += n; |
| 308 | else |
| 309 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 310 | } |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 311 | |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 312 | static void bpf_vlog_reset(struct bpf_verifier_log *log, u32 new_pos) |
| 313 | { |
| 314 | char zero = 0; |
| 315 | |
| 316 | if (!bpf_verifier_log_needed(log)) |
| 317 | return; |
| 318 | |
| 319 | log->len_used = new_pos; |
| 320 | if (put_user(zero, log->ubuf + new_pos)) |
| 321 | log->ubuf = NULL; |
| 322 | } |
| 323 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 324 | /* log_level controls verbosity level of eBPF verifier. |
| 325 | * bpf_verifier_log_write() is used to dump the verification trace to the log, |
| 326 | * so the user can figure out what's wrong with the program |
Quentin Monnet | 430e68d | 2018-01-10 12:26:06 +0000 | [diff] [blame] | 327 | */ |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 328 | __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env, |
| 329 | const char *fmt, ...) |
| 330 | { |
| 331 | va_list args; |
| 332 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 333 | if (!bpf_verifier_log_needed(&env->log)) |
| 334 | return; |
| 335 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 336 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 337 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 338 | va_end(args); |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(bpf_verifier_log_write); |
| 341 | |
| 342 | __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...) |
| 343 | { |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 344 | struct bpf_verifier_env *env = private_data; |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 345 | va_list args; |
| 346 | |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 347 | if (!bpf_verifier_log_needed(&env->log)) |
| 348 | return; |
| 349 | |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 350 | va_start(args, fmt); |
Martin KaFai Lau | 77d2e05 | 2018-03-24 11:44:23 -0700 | [diff] [blame] | 351 | bpf_verifier_vlog(&env->log, fmt, args); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 352 | va_end(args); |
| 353 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 354 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 355 | __printf(2, 3) void bpf_log(struct bpf_verifier_log *log, |
| 356 | const char *fmt, ...) |
| 357 | { |
| 358 | va_list args; |
| 359 | |
| 360 | if (!bpf_verifier_log_needed(log)) |
| 361 | return; |
| 362 | |
| 363 | va_start(args, fmt); |
| 364 | bpf_verifier_vlog(log, fmt, args); |
| 365 | va_end(args); |
| 366 | } |
| 367 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 368 | static const char *ltrim(const char *s) |
| 369 | { |
| 370 | while (isspace(*s)) |
| 371 | s++; |
| 372 | |
| 373 | return s; |
| 374 | } |
| 375 | |
| 376 | __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env, |
| 377 | u32 insn_off, |
| 378 | const char *prefix_fmt, ...) |
| 379 | { |
| 380 | const struct bpf_line_info *linfo; |
| 381 | |
| 382 | if (!bpf_verifier_log_needed(&env->log)) |
| 383 | return; |
| 384 | |
| 385 | linfo = find_linfo(env, insn_off); |
| 386 | if (!linfo || linfo == env->prev_linfo) |
| 387 | return; |
| 388 | |
| 389 | if (prefix_fmt) { |
| 390 | va_list args; |
| 391 | |
| 392 | va_start(args, prefix_fmt); |
| 393 | bpf_verifier_vlog(&env->log, prefix_fmt, args); |
| 394 | va_end(args); |
| 395 | } |
| 396 | |
| 397 | verbose(env, "%s\n", |
| 398 | ltrim(btf_name_by_offset(env->prog->aux->btf, |
| 399 | linfo->line_off))); |
| 400 | |
| 401 | env->prev_linfo = linfo; |
| 402 | } |
| 403 | |
Yonghong Song | bc2591d | 2021-02-26 12:49:22 -0800 | [diff] [blame] | 404 | static void verbose_invalid_scalar(struct bpf_verifier_env *env, |
| 405 | struct bpf_reg_state *reg, |
| 406 | struct tnum *range, const char *ctx, |
| 407 | const char *reg_name) |
| 408 | { |
| 409 | char tn_buf[48]; |
| 410 | |
| 411 | verbose(env, "At %s the register %s ", ctx, reg_name); |
| 412 | if (!tnum_is_unknown(reg->var_off)) { |
| 413 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 414 | verbose(env, "has value %s", tn_buf); |
| 415 | } else { |
| 416 | verbose(env, "has unknown scalar value"); |
| 417 | } |
| 418 | tnum_strn(tn_buf, sizeof(tn_buf), *range); |
| 419 | verbose(env, " should have been in %s\n", tn_buf); |
| 420 | } |
| 421 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 422 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 423 | { |
| 424 | return type == PTR_TO_PACKET || |
| 425 | type == PTR_TO_PACKET_META; |
| 426 | } |
| 427 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 428 | static bool type_is_sk_pointer(enum bpf_reg_type type) |
| 429 | { |
| 430 | return type == PTR_TO_SOCKET || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 431 | type == PTR_TO_SOCK_COMMON || |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 432 | type == PTR_TO_TCP_SOCK || |
| 433 | type == PTR_TO_XDP_SOCK; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 434 | } |
| 435 | |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 436 | static bool reg_type_not_null(enum bpf_reg_type type) |
| 437 | { |
| 438 | return type == PTR_TO_SOCKET || |
| 439 | type == PTR_TO_TCP_SOCK || |
| 440 | type == PTR_TO_MAP_VALUE || |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 441 | type == PTR_TO_MAP_KEY || |
Yonghong Song | 01c66c4 | 2020-06-30 10:12:40 -0700 | [diff] [blame] | 442 | type == PTR_TO_SOCK_COMMON; |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 445 | static bool reg_type_may_be_null(enum bpf_reg_type type) |
| 446 | { |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 447 | return type == PTR_TO_MAP_VALUE_OR_NULL || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 448 | type == PTR_TO_SOCKET_OR_NULL || |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 449 | type == PTR_TO_SOCK_COMMON_OR_NULL || |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 450 | type == PTR_TO_TCP_SOCK_OR_NULL || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 451 | type == PTR_TO_BTF_ID_OR_NULL || |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 452 | type == PTR_TO_MEM_OR_NULL || |
| 453 | type == PTR_TO_RDONLY_BUF_OR_NULL || |
| 454 | type == PTR_TO_RDWR_BUF_OR_NULL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 457 | static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) |
| 458 | { |
| 459 | return reg->type == PTR_TO_MAP_VALUE && |
| 460 | map_value_has_spin_lock(reg->map_ptr); |
| 461 | } |
| 462 | |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 463 | static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type) |
| 464 | { |
| 465 | return type == PTR_TO_SOCKET || |
| 466 | type == PTR_TO_SOCKET_OR_NULL || |
| 467 | type == PTR_TO_TCP_SOCK || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 468 | type == PTR_TO_TCP_SOCK_OR_NULL || |
| 469 | type == PTR_TO_MEM || |
| 470 | type == PTR_TO_MEM_OR_NULL; |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 473 | static bool arg_type_may_be_refcounted(enum bpf_arg_type type) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 474 | { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 475 | return type == ARG_PTR_TO_SOCK_COMMON; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 478 | static bool arg_type_may_be_null(enum bpf_arg_type type) |
| 479 | { |
| 480 | return type == ARG_PTR_TO_MAP_VALUE_OR_NULL || |
| 481 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 482 | type == ARG_PTR_TO_CTX_OR_NULL || |
| 483 | type == ARG_PTR_TO_SOCKET_OR_NULL || |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 484 | type == ARG_PTR_TO_ALLOC_MEM_OR_NULL || |
| 485 | type == ARG_PTR_TO_STACK_OR_NULL; |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 488 | /* Determine whether the function releases some resources allocated by another |
| 489 | * function call. The first reference type argument will be assumed to be |
| 490 | * released by release_reference(). |
| 491 | */ |
| 492 | static bool is_release_function(enum bpf_func_id func_id) |
| 493 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 494 | return func_id == BPF_FUNC_sk_release || |
| 495 | func_id == BPF_FUNC_ringbuf_submit || |
| 496 | func_id == BPF_FUNC_ringbuf_discard; |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 499 | static bool may_be_acquire_function(enum bpf_func_id func_id) |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 500 | { |
| 501 | return func_id == BPF_FUNC_sk_lookup_tcp || |
Lorenz Bauer | edbf8c0 | 2019-03-22 09:54:01 +0800 | [diff] [blame] | 502 | func_id == BPF_FUNC_sk_lookup_udp || |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 503 | func_id == BPF_FUNC_skc_lookup_tcp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 504 | func_id == BPF_FUNC_map_lookup_elem || |
| 505 | func_id == BPF_FUNC_ringbuf_reserve; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static bool is_acquire_function(enum bpf_func_id func_id, |
| 509 | const struct bpf_map *map) |
| 510 | { |
| 511 | enum bpf_map_type map_type = map ? map->map_type : BPF_MAP_TYPE_UNSPEC; |
| 512 | |
| 513 | if (func_id == BPF_FUNC_sk_lookup_tcp || |
| 514 | func_id == BPF_FUNC_sk_lookup_udp || |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 515 | func_id == BPF_FUNC_skc_lookup_tcp || |
| 516 | func_id == BPF_FUNC_ringbuf_reserve) |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 517 | return true; |
| 518 | |
| 519 | if (func_id == BPF_FUNC_map_lookup_elem && |
| 520 | (map_type == BPF_MAP_TYPE_SOCKMAP || |
| 521 | map_type == BPF_MAP_TYPE_SOCKHASH)) |
| 522 | return true; |
| 523 | |
| 524 | return false; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 527 | static bool is_ptr_cast_function(enum bpf_func_id func_id) |
| 528 | { |
| 529 | return func_id == BPF_FUNC_tcp_sock || |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 530 | func_id == BPF_FUNC_sk_fullsock || |
| 531 | func_id == BPF_FUNC_skc_to_tcp_sock || |
| 532 | func_id == BPF_FUNC_skc_to_tcp6_sock || |
| 533 | func_id == BPF_FUNC_skc_to_udp6_sock || |
| 534 | func_id == BPF_FUNC_skc_to_tcp_timewait_sock || |
| 535 | func_id == BPF_FUNC_skc_to_tcp_request_sock; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Brendan Jackman | 3949186 | 2021-03-04 18:56:46 -0800 | [diff] [blame] | 538 | static bool is_cmpxchg_insn(const struct bpf_insn *insn) |
| 539 | { |
| 540 | return BPF_CLASS(insn->code) == BPF_STX && |
| 541 | BPF_MODE(insn->code) == BPF_ATOMIC && |
| 542 | insn->imm == BPF_CMPXCHG; |
| 543 | } |
| 544 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 545 | /* string representation of 'enum bpf_reg_type' */ |
| 546 | static const char * const reg_type_str[] = { |
| 547 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 548 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 549 | [PTR_TO_CTX] = "ctx", |
| 550 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 551 | [PTR_TO_MAP_VALUE] = "map_value", |
| 552 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 553 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 554 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 555 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 556 | [PTR_TO_PACKET_END] = "pkt_end", |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 557 | [PTR_TO_FLOW_KEYS] = "flow_keys", |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 558 | [PTR_TO_SOCKET] = "sock", |
| 559 | [PTR_TO_SOCKET_OR_NULL] = "sock_or_null", |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 560 | [PTR_TO_SOCK_COMMON] = "sock_common", |
| 561 | [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null", |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 562 | [PTR_TO_TCP_SOCK] = "tcp_sock", |
| 563 | [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 564 | [PTR_TO_TP_BUFFER] = "tp_buffer", |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 565 | [PTR_TO_XDP_SOCK] = "xdp_sock", |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 566 | [PTR_TO_BTF_ID] = "ptr_", |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 567 | [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_", |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 568 | [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 569 | [PTR_TO_MEM] = "mem", |
| 570 | [PTR_TO_MEM_OR_NULL] = "mem_or_null", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 571 | [PTR_TO_RDONLY_BUF] = "rdonly_buf", |
| 572 | [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null", |
| 573 | [PTR_TO_RDWR_BUF] = "rdwr_buf", |
| 574 | [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null", |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 575 | [PTR_TO_FUNC] = "func", |
| 576 | [PTR_TO_MAP_KEY] = "map_key", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 577 | }; |
| 578 | |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 579 | static char slot_type_char[] = { |
| 580 | [STACK_INVALID] = '?', |
| 581 | [STACK_SPILL] = 'r', |
| 582 | [STACK_MISC] = 'm', |
| 583 | [STACK_ZERO] = '0', |
| 584 | }; |
| 585 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 586 | static void print_liveness(struct bpf_verifier_env *env, |
| 587 | enum bpf_reg_liveness live) |
| 588 | { |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 589 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE)) |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 590 | verbose(env, "_"); |
| 591 | if (live & REG_LIVE_READ) |
| 592 | verbose(env, "r"); |
| 593 | if (live & REG_LIVE_WRITTEN) |
| 594 | verbose(env, "w"); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 595 | if (live & REG_LIVE_DONE) |
| 596 | verbose(env, "D"); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 599 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 600 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 601 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 602 | struct bpf_verifier_state *cur = env->cur_state; |
| 603 | |
| 604 | return cur->frame[reg->frameno]; |
| 605 | } |
| 606 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 607 | static const char *kernel_type_name(const struct btf* btf, u32 id) |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 608 | { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 609 | return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 612 | static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno) |
| 613 | { |
| 614 | env->scratched_regs |= 1U << regno; |
| 615 | } |
| 616 | |
| 617 | static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi) |
| 618 | { |
| 619 | env->scratched_stack_slots |= 1UL << spi; |
| 620 | } |
| 621 | |
| 622 | static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno) |
| 623 | { |
| 624 | return (env->scratched_regs >> regno) & 1; |
| 625 | } |
| 626 | |
| 627 | static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno) |
| 628 | { |
| 629 | return (env->scratched_stack_slots >> regno) & 1; |
| 630 | } |
| 631 | |
| 632 | static bool verifier_state_scratched(const struct bpf_verifier_env *env) |
| 633 | { |
| 634 | return env->scratched_regs || env->scratched_stack_slots; |
| 635 | } |
| 636 | |
| 637 | static void mark_verifier_state_clean(struct bpf_verifier_env *env) |
| 638 | { |
| 639 | env->scratched_regs = 0U; |
| 640 | env->scratched_stack_slots = 0UL; |
| 641 | } |
| 642 | |
| 643 | /* Used for printing the entire verifier state. */ |
| 644 | static void mark_verifier_state_scratched(struct bpf_verifier_env *env) |
| 645 | { |
| 646 | env->scratched_regs = ~0U; |
| 647 | env->scratched_stack_slots = ~0UL; |
| 648 | } |
| 649 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 650 | /* The reg state of a pointer or a bounded scalar was saved when |
| 651 | * it was spilled to the stack. |
| 652 | */ |
| 653 | static bool is_spilled_reg(const struct bpf_stack_state *stack) |
| 654 | { |
| 655 | return stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL; |
| 656 | } |
| 657 | |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 658 | static void scrub_spilled_slot(u8 *stype) |
| 659 | { |
| 660 | if (*stype != STACK_INVALID) |
| 661 | *stype = STACK_MISC; |
| 662 | } |
| 663 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 664 | static void print_verifier_state(struct bpf_verifier_env *env, |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 665 | const struct bpf_func_state *state, |
| 666 | bool print_all) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 667 | { |
| 668 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 669 | enum bpf_reg_type t; |
| 670 | int i; |
| 671 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 672 | if (state->frameno) |
| 673 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 674 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 675 | reg = &state->regs[i]; |
| 676 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 677 | if (t == NOT_INIT) |
| 678 | continue; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 679 | if (!print_all && !reg_scratched(env, i)) |
| 680 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 681 | verbose(env, " R%d", i); |
| 682 | print_liveness(env, reg->live); |
| 683 | verbose(env, "=%s", reg_type_str[t]); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 684 | if (t == SCALAR_VALUE && reg->precise) |
| 685 | verbose(env, "P"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 686 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 687 | tnum_is_const(reg->var_off)) { |
| 688 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 689 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 690 | } else { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 691 | if (t == PTR_TO_BTF_ID || |
| 692 | t == PTR_TO_BTF_ID_OR_NULL || |
| 693 | t == PTR_TO_PERCPU_BTF_ID) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 694 | verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id)); |
Martin KaFai Lau | cba368c | 2019-03-18 10:37:13 -0700 | [diff] [blame] | 695 | verbose(env, "(id=%d", reg->id); |
| 696 | if (reg_type_may_be_refcounted_or_null(t)) |
| 697 | verbose(env, ",ref_obj_id=%d", reg->ref_obj_id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 698 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 699 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 700 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 701 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 702 | else if (t == CONST_PTR_TO_MAP || |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 703 | t == PTR_TO_MAP_KEY || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 704 | t == PTR_TO_MAP_VALUE || |
| 705 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 706 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 707 | reg->map_ptr->key_size, |
| 708 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 709 | if (tnum_is_const(reg->var_off)) { |
| 710 | /* Typically an immediate SCALAR_VALUE, but |
| 711 | * could be a pointer whose offset is too big |
| 712 | * for reg->off |
| 713 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 714 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 715 | } else { |
| 716 | if (reg->smin_value != reg->umin_value && |
| 717 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 718 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 719 | (long long)reg->smin_value); |
| 720 | if (reg->smax_value != reg->umax_value && |
| 721 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 722 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 723 | (long long)reg->smax_value); |
| 724 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 725 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 726 | (unsigned long long)reg->umin_value); |
| 727 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 728 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 729 | (unsigned long long)reg->umax_value); |
| 730 | if (!tnum_is_unknown(reg->var_off)) { |
| 731 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 732 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 733 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 734 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 735 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 736 | if (reg->s32_min_value != reg->smin_value && |
| 737 | reg->s32_min_value != S32_MIN) |
| 738 | verbose(env, ",s32_min_value=%d", |
| 739 | (int)(reg->s32_min_value)); |
| 740 | if (reg->s32_max_value != reg->smax_value && |
| 741 | reg->s32_max_value != S32_MAX) |
| 742 | verbose(env, ",s32_max_value=%d", |
| 743 | (int)(reg->s32_max_value)); |
| 744 | if (reg->u32_min_value != reg->umin_value && |
| 745 | reg->u32_min_value != U32_MIN) |
| 746 | verbose(env, ",u32_min_value=%d", |
| 747 | (int)(reg->u32_min_value)); |
| 748 | if (reg->u32_max_value != reg->umax_value && |
| 749 | reg->u32_max_value != U32_MAX) |
| 750 | verbose(env, ",u32_max_value=%d", |
| 751 | (int)(reg->u32_max_value)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 752 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 753 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 754 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 755 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 756 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 757 | char types_buf[BPF_REG_SIZE + 1]; |
| 758 | bool valid = false; |
| 759 | int j; |
| 760 | |
| 761 | for (j = 0; j < BPF_REG_SIZE; j++) { |
| 762 | if (state->stack[i].slot_type[j] != STACK_INVALID) |
| 763 | valid = true; |
| 764 | types_buf[j] = slot_type_char[ |
| 765 | state->stack[i].slot_type[j]]; |
| 766 | } |
| 767 | types_buf[BPF_REG_SIZE] = 0; |
| 768 | if (!valid) |
| 769 | continue; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 770 | if (!print_all && !stack_slot_scratched(env, i)) |
| 771 | continue; |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 772 | verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE); |
| 773 | print_liveness(env, state->stack[i].spilled_ptr.live); |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 774 | if (is_spilled_reg(&state->stack[i])) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 775 | reg = &state->stack[i].spilled_ptr; |
| 776 | t = reg->type; |
| 777 | verbose(env, "=%s", reg_type_str[t]); |
| 778 | if (t == SCALAR_VALUE && reg->precise) |
| 779 | verbose(env, "P"); |
| 780 | if (t == SCALAR_VALUE && tnum_is_const(reg->var_off)) |
| 781 | verbose(env, "%lld", reg->var_off.value + reg->off); |
| 782 | } else { |
Edward Cree | 8efea21 | 2018-08-22 20:02:44 +0100 | [diff] [blame] | 783 | verbose(env, "=%s", types_buf); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 784 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 785 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 786 | if (state->acquired_refs && state->refs[0].id) { |
| 787 | verbose(env, " refs=%d", state->refs[0].id); |
| 788 | for (i = 1; i < state->acquired_refs; i++) |
| 789 | if (state->refs[i].id) |
| 790 | verbose(env, ",%d", state->refs[i].id); |
| 791 | } |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 792 | if (state->in_callback_fn) |
| 793 | verbose(env, " cb"); |
| 794 | if (state->in_async_callback_fn) |
| 795 | verbose(env, " async_cb"); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 796 | verbose(env, "\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 797 | mark_verifier_state_clean(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 800 | static inline u32 vlog_alignment(u32 pos) |
| 801 | { |
| 802 | return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT), |
| 803 | BPF_LOG_MIN_ALIGNMENT) - pos - 1; |
| 804 | } |
| 805 | |
| 806 | static void print_insn_state(struct bpf_verifier_env *env, |
| 807 | const struct bpf_func_state *state) |
| 808 | { |
| 809 | if (env->prev_log_len && env->prev_log_len == env->log.len_used) { |
| 810 | /* remove new line character */ |
| 811 | bpf_vlog_reset(&env->log, env->prev_log_len - 1); |
| 812 | verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' '); |
| 813 | } else { |
| 814 | verbose(env, "%d:", env->insn_idx); |
| 815 | } |
| 816 | print_verifier_state(env, state, false); |
| 817 | } |
| 818 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 819 | /* copy array src of length n * size bytes to dst. dst is reallocated if it's too |
| 820 | * small to hold src. This is different from krealloc since we don't want to preserve |
| 821 | * the contents of dst. |
| 822 | * |
| 823 | * Leaves dst untouched if src is NULL or length is zero. Returns NULL if memory could |
| 824 | * not be allocated. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 825 | */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 826 | static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t flags) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 827 | { |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 828 | size_t bytes; |
| 829 | |
| 830 | if (ZERO_OR_NULL_PTR(src)) |
| 831 | goto out; |
| 832 | |
| 833 | if (unlikely(check_mul_overflow(n, size, &bytes))) |
| 834 | return NULL; |
| 835 | |
| 836 | if (ksize(dst) < bytes) { |
| 837 | kfree(dst); |
| 838 | dst = kmalloc_track_caller(bytes, flags); |
| 839 | if (!dst) |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | memcpy(dst, src, bytes); |
| 844 | out: |
| 845 | return dst ? dst : ZERO_SIZE_PTR; |
| 846 | } |
| 847 | |
| 848 | /* resize an array from old_n items to new_n items. the array is reallocated if it's too |
| 849 | * small to hold new_n items. new items are zeroed out if the array grows. |
| 850 | * |
| 851 | * Contrary to krealloc_array, does not free arr if new_n is zero. |
| 852 | */ |
| 853 | static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size) |
| 854 | { |
| 855 | if (!new_n || old_n == new_n) |
| 856 | goto out; |
| 857 | |
| 858 | arr = krealloc_array(arr, new_n, size, GFP_KERNEL); |
| 859 | if (!arr) |
| 860 | return NULL; |
| 861 | |
| 862 | if (new_n > old_n) |
| 863 | memset(arr + old_n * size, 0, (new_n - old_n) * size); |
| 864 | |
| 865 | out: |
| 866 | return arr ? arr : ZERO_SIZE_PTR; |
| 867 | } |
| 868 | |
| 869 | static int copy_reference_state(struct bpf_func_state *dst, const struct bpf_func_state *src) |
| 870 | { |
| 871 | dst->refs = copy_array(dst->refs, src->refs, src->acquired_refs, |
| 872 | sizeof(struct bpf_reference_state), GFP_KERNEL); |
| 873 | if (!dst->refs) |
| 874 | return -ENOMEM; |
| 875 | |
| 876 | dst->acquired_refs = src->acquired_refs; |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_state *src) |
| 881 | { |
| 882 | size_t n = src->allocated_stack / BPF_REG_SIZE; |
| 883 | |
| 884 | dst->stack = copy_array(dst->stack, src->stack, n, sizeof(struct bpf_stack_state), |
| 885 | GFP_KERNEL); |
| 886 | if (!dst->stack) |
| 887 | return -ENOMEM; |
| 888 | |
| 889 | dst->allocated_stack = src->allocated_stack; |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static int resize_reference_state(struct bpf_func_state *state, size_t n) |
| 894 | { |
| 895 | state->refs = realloc_array(state->refs, state->acquired_refs, n, |
| 896 | sizeof(struct bpf_reference_state)); |
| 897 | if (!state->refs) |
| 898 | return -ENOMEM; |
| 899 | |
| 900 | state->acquired_refs = n; |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static int grow_stack_state(struct bpf_func_state *state, int size) |
| 905 | { |
| 906 | size_t old_n = state->allocated_stack / BPF_REG_SIZE, n = size / BPF_REG_SIZE; |
| 907 | |
| 908 | if (old_n >= n) |
| 909 | return 0; |
| 910 | |
| 911 | state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state)); |
| 912 | if (!state->stack) |
| 913 | return -ENOMEM; |
| 914 | |
| 915 | state->allocated_stack = size; |
| 916 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 917 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 918 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 919 | /* Acquire a pointer id from the env and update the state->refs to include |
| 920 | * this new pointer reference. |
| 921 | * On success, returns a valid pointer id to associate with the register |
| 922 | * On failure, returns a negative errno. |
| 923 | */ |
| 924 | static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx) |
| 925 | { |
| 926 | struct bpf_func_state *state = cur_func(env); |
| 927 | int new_ofs = state->acquired_refs; |
| 928 | int id, err; |
| 929 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 930 | err = resize_reference_state(state, state->acquired_refs + 1); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 931 | if (err) |
| 932 | return err; |
| 933 | id = ++env->id_gen; |
| 934 | state->refs[new_ofs].id = id; |
| 935 | state->refs[new_ofs].insn_idx = insn_idx; |
| 936 | |
| 937 | return id; |
| 938 | } |
| 939 | |
| 940 | /* release function corresponding to acquire_reference_state(). Idempotent. */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 941 | static int release_reference_state(struct bpf_func_state *state, int ptr_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 942 | { |
| 943 | int i, last_idx; |
| 944 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 945 | last_idx = state->acquired_refs - 1; |
| 946 | for (i = 0; i < state->acquired_refs; i++) { |
| 947 | if (state->refs[i].id == ptr_id) { |
| 948 | if (last_idx && i != last_idx) |
| 949 | memcpy(&state->refs[i], &state->refs[last_idx], |
| 950 | sizeof(*state->refs)); |
| 951 | memset(&state->refs[last_idx], 0, sizeof(*state->refs)); |
| 952 | state->acquired_refs--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 953 | return 0; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 954 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 955 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 956 | return -EINVAL; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 959 | static void free_func_state(struct bpf_func_state *state) |
| 960 | { |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 961 | if (!state) |
| 962 | return; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 963 | kfree(state->refs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 964 | kfree(state->stack); |
| 965 | kfree(state); |
| 966 | } |
| 967 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 968 | static void clear_jmp_history(struct bpf_verifier_state *state) |
| 969 | { |
| 970 | kfree(state->jmp_history); |
| 971 | state->jmp_history = NULL; |
| 972 | state->jmp_history_cnt = 0; |
| 973 | } |
| 974 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 975 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 976 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 977 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 978 | int i; |
| 979 | |
| 980 | for (i = 0; i <= state->curframe; i++) { |
| 981 | free_func_state(state->frame[i]); |
| 982 | state->frame[i] = NULL; |
| 983 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 984 | clear_jmp_history(state); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 985 | if (free_self) |
| 986 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* copy verifier state from src to dst growing dst stack space |
| 990 | * when necessary to accommodate larger src stack |
| 991 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 992 | static int copy_func_state(struct bpf_func_state *dst, |
| 993 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 994 | { |
| 995 | int err; |
| 996 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 997 | memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs)); |
| 998 | err = copy_reference_state(dst, src); |
| 999 | if (err) |
| 1000 | return err; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1001 | return copy_stack_state(dst, src); |
| 1002 | } |
| 1003 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1004 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 1005 | const struct bpf_verifier_state *src) |
| 1006 | { |
| 1007 | struct bpf_func_state *dst; |
| 1008 | int i, err; |
| 1009 | |
Lorenz Bauer | 06ab6a5 | 2021-04-29 14:46:55 +0100 | [diff] [blame] | 1010 | dst_state->jmp_history = copy_array(dst_state->jmp_history, src->jmp_history, |
| 1011 | src->jmp_history_cnt, sizeof(struct bpf_idx_pair), |
| 1012 | GFP_USER); |
| 1013 | if (!dst_state->jmp_history) |
| 1014 | return -ENOMEM; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1015 | dst_state->jmp_history_cnt = src->jmp_history_cnt; |
| 1016 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1017 | /* if dst has more stack frames then src frame, free them */ |
| 1018 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 1019 | free_func_state(dst_state->frame[i]); |
| 1020 | dst_state->frame[i] = NULL; |
| 1021 | } |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1022 | dst_state->speculative = src->speculative; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1023 | dst_state->curframe = src->curframe; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 1024 | dst_state->active_spin_lock = src->active_spin_lock; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1025 | dst_state->branches = src->branches; |
| 1026 | dst_state->parent = src->parent; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 1027 | dst_state->first_insn_idx = src->first_insn_idx; |
| 1028 | dst_state->last_insn_idx = src->last_insn_idx; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1029 | for (i = 0; i <= src->curframe; i++) { |
| 1030 | dst = dst_state->frame[i]; |
| 1031 | if (!dst) { |
| 1032 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 1033 | if (!dst) |
| 1034 | return -ENOMEM; |
| 1035 | dst_state->frame[i] = dst; |
| 1036 | } |
| 1037 | err = copy_func_state(dst, src->frame[i]); |
| 1038 | if (err) |
| 1039 | return err; |
| 1040 | } |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1044 | static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st) |
| 1045 | { |
| 1046 | while (st) { |
| 1047 | u32 br = --st->branches; |
| 1048 | |
| 1049 | /* WARN_ON(br > 1) technically makes sense here, |
| 1050 | * but see comment in push_stack(), hence: |
| 1051 | */ |
| 1052 | WARN_ONCE((int)br < 0, |
| 1053 | "BUG update_branch_counts:branches_to_explore=%d\n", |
| 1054 | br); |
| 1055 | if (br) |
| 1056 | break; |
| 1057 | st = st->parent; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1061 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1062 | int *insn_idx, bool pop_log) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1063 | { |
| 1064 | struct bpf_verifier_state *cur = env->cur_state; |
| 1065 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 1066 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1069 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1070 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1071 | if (cur) { |
| 1072 | err = copy_verifier_state(cur, &head->st); |
| 1073 | if (err) |
| 1074 | return err; |
| 1075 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1076 | if (pop_log) |
| 1077 | bpf_vlog_reset(&env->log, head->log_pos); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1078 | if (insn_idx) |
| 1079 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1080 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1081 | *prev_insn_idx = head->prev_insn_idx; |
| 1082 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 1083 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1084 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1085 | env->head = elem; |
| 1086 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1087 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1090 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1091 | int insn_idx, int prev_insn_idx, |
| 1092 | bool speculative) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1093 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1094 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1095 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1096 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1097 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1098 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1099 | if (!elem) |
| 1100 | goto err; |
| 1101 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1102 | elem->insn_idx = insn_idx; |
| 1103 | elem->prev_insn_idx = prev_insn_idx; |
| 1104 | elem->next = env->head; |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1105 | elem->log_pos = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1106 | env->head = elem; |
| 1107 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 1108 | err = copy_verifier_state(&elem->st, cur); |
| 1109 | if (err) |
| 1110 | goto err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 1111 | elem->st.speculative |= speculative; |
Alexei Starovoitov | b285fcb | 2019-05-21 20:14:19 -0700 | [diff] [blame] | 1112 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 1113 | verbose(env, "The sequence of %d jumps is too complex.\n", |
| 1114 | env->stack_size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1115 | goto err; |
| 1116 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 1117 | if (elem->st.parent) { |
| 1118 | ++elem->st.parent->branches; |
| 1119 | /* WARN_ON(branches > 2) technically makes sense here, |
| 1120 | * but |
| 1121 | * 1. speculative states will bump 'branches' for non-branch |
| 1122 | * instructions |
| 1123 | * 2. is_state_visited() heuristics may decide not to create |
| 1124 | * a new state for a sequence of branches and all such current |
| 1125 | * and cloned states will be pointing to a single parent state |
| 1126 | * which might have large 'branches' count. |
| 1127 | */ |
| 1128 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1129 | return &elem->st; |
| 1130 | err: |
Alexei Starovoitov | 5896351 | 2018-01-08 07:51:17 -0800 | [diff] [blame] | 1131 | free_verifier_state(env->cur_state, true); |
| 1132 | env->cur_state = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1133 | /* pop all elements and return */ |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 1134 | while (!pop_stack(env, NULL, NULL, false)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1135 | return NULL; |
| 1136 | } |
| 1137 | |
| 1138 | #define CALLER_SAVED_REGS 6 |
| 1139 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 1140 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 1141 | }; |
| 1142 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1143 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1144 | struct bpf_reg_state *reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1145 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1146 | /* This helper doesn't clear reg->id */ |
| 1147 | static void ___mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1148 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1149 | reg->var_off = tnum_const(imm); |
| 1150 | reg->smin_value = (s64)imm; |
| 1151 | reg->smax_value = (s64)imm; |
| 1152 | reg->umin_value = imm; |
| 1153 | reg->umax_value = imm; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1154 | |
| 1155 | reg->s32_min_value = (s32)imm; |
| 1156 | reg->s32_max_value = (s32)imm; |
| 1157 | reg->u32_min_value = (u32)imm; |
| 1158 | reg->u32_max_value = (u32)imm; |
| 1159 | } |
| 1160 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 1161 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 1162 | * known to have the value @imm. |
| 1163 | */ |
| 1164 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 1165 | { |
| 1166 | /* Clear id, off, and union(map_ptr, range) */ |
| 1167 | memset(((u8 *)reg) + sizeof(reg->type), 0, |
| 1168 | offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type)); |
| 1169 | ___mark_reg_known(reg, imm); |
| 1170 | } |
| 1171 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1172 | static void __mark_reg32_known(struct bpf_reg_state *reg, u64 imm) |
| 1173 | { |
| 1174 | reg->var_off = tnum_const_subreg(reg->var_off, imm); |
| 1175 | reg->s32_min_value = (s32)imm; |
| 1176 | reg->s32_max_value = (s32)imm; |
| 1177 | reg->u32_min_value = (u32)imm; |
| 1178 | reg->u32_max_value = (u32)imm; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1181 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 1182 | * used only on registers holding a pointer type. |
| 1183 | */ |
| 1184 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 1185 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1186 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1187 | } |
| 1188 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1189 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 1190 | { |
| 1191 | __mark_reg_known(reg, 0); |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1192 | reg->type = SCALAR_VALUE; |
| 1193 | } |
| 1194 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1195 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 1196 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1197 | { |
| 1198 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1199 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1200 | /* Something bad happened, let's kill all regs */ |
| 1201 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1202 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1203 | return; |
| 1204 | } |
| 1205 | __mark_reg_known_zero(regs + regno); |
| 1206 | } |
| 1207 | |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1208 | static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
| 1209 | { |
| 1210 | switch (reg->type) { |
| 1211 | case PTR_TO_MAP_VALUE_OR_NULL: { |
| 1212 | const struct bpf_map *map = reg->map_ptr; |
| 1213 | |
| 1214 | if (map->inner_map_meta) { |
| 1215 | reg->type = CONST_PTR_TO_MAP; |
| 1216 | reg->map_ptr = map->inner_map_meta; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 1217 | /* transfer reg's id which is unique for every map_lookup_elem |
| 1218 | * as UID of the inner map. |
| 1219 | */ |
Alexei Starovoitov | 34d11a4 | 2021-11-10 09:25:56 -0800 | [diff] [blame] | 1220 | if (map_value_has_timer(map->inner_map_meta)) |
| 1221 | reg->map_uid = reg->id; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1222 | } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
| 1223 | reg->type = PTR_TO_XDP_SOCK; |
| 1224 | } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 1225 | map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 1226 | reg->type = PTR_TO_SOCKET; |
| 1227 | } else { |
| 1228 | reg->type = PTR_TO_MAP_VALUE; |
| 1229 | } |
| 1230 | break; |
| 1231 | } |
| 1232 | case PTR_TO_SOCKET_OR_NULL: |
| 1233 | reg->type = PTR_TO_SOCKET; |
| 1234 | break; |
| 1235 | case PTR_TO_SOCK_COMMON_OR_NULL: |
| 1236 | reg->type = PTR_TO_SOCK_COMMON; |
| 1237 | break; |
| 1238 | case PTR_TO_TCP_SOCK_OR_NULL: |
| 1239 | reg->type = PTR_TO_TCP_SOCK; |
| 1240 | break; |
| 1241 | case PTR_TO_BTF_ID_OR_NULL: |
| 1242 | reg->type = PTR_TO_BTF_ID; |
| 1243 | break; |
| 1244 | case PTR_TO_MEM_OR_NULL: |
| 1245 | reg->type = PTR_TO_MEM; |
| 1246 | break; |
| 1247 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 1248 | reg->type = PTR_TO_RDONLY_BUF; |
| 1249 | break; |
| 1250 | case PTR_TO_RDWR_BUF_OR_NULL: |
| 1251 | reg->type = PTR_TO_RDWR_BUF; |
| 1252 | break; |
| 1253 | default: |
Dan Carpenter | 33ccec5 | 2021-02-17 10:45:25 +0300 | [diff] [blame] | 1254 | WARN_ONCE(1, "unknown nullable register type"); |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1258 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1259 | { |
| 1260 | return type_is_pkt_pointer(reg->type); |
| 1261 | } |
| 1262 | |
| 1263 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 1264 | { |
| 1265 | return reg_is_pkt_pointer(reg) || |
| 1266 | reg->type == PTR_TO_PACKET_END; |
| 1267 | } |
| 1268 | |
| 1269 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 1270 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 1271 | enum bpf_reg_type which) |
| 1272 | { |
| 1273 | /* The register can already have a range from prior markings. |
| 1274 | * This is fine as long as it hasn't been advanced from its |
| 1275 | * origin. |
| 1276 | */ |
| 1277 | return reg->type == which && |
| 1278 | reg->id == 0 && |
| 1279 | reg->off == 0 && |
| 1280 | tnum_equals_const(reg->var_off, 0); |
| 1281 | } |
| 1282 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1283 | /* Reset the min/max bounds of a register */ |
| 1284 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 1285 | { |
| 1286 | reg->smin_value = S64_MIN; |
| 1287 | reg->smax_value = S64_MAX; |
| 1288 | reg->umin_value = 0; |
| 1289 | reg->umax_value = U64_MAX; |
| 1290 | |
| 1291 | reg->s32_min_value = S32_MIN; |
| 1292 | reg->s32_max_value = S32_MAX; |
| 1293 | reg->u32_min_value = 0; |
| 1294 | reg->u32_max_value = U32_MAX; |
| 1295 | } |
| 1296 | |
| 1297 | static void __mark_reg64_unbounded(struct bpf_reg_state *reg) |
| 1298 | { |
| 1299 | reg->smin_value = S64_MIN; |
| 1300 | reg->smax_value = S64_MAX; |
| 1301 | reg->umin_value = 0; |
| 1302 | reg->umax_value = U64_MAX; |
| 1303 | } |
| 1304 | |
| 1305 | static void __mark_reg32_unbounded(struct bpf_reg_state *reg) |
| 1306 | { |
| 1307 | reg->s32_min_value = S32_MIN; |
| 1308 | reg->s32_max_value = S32_MAX; |
| 1309 | reg->u32_min_value = 0; |
| 1310 | reg->u32_max_value = U32_MAX; |
| 1311 | } |
| 1312 | |
| 1313 | static void __update_reg32_bounds(struct bpf_reg_state *reg) |
| 1314 | { |
| 1315 | struct tnum var32_off = tnum_subreg(reg->var_off); |
| 1316 | |
| 1317 | /* min signed is max(sign bit) | min(other bits) */ |
| 1318 | reg->s32_min_value = max_t(s32, reg->s32_min_value, |
| 1319 | var32_off.value | (var32_off.mask & S32_MIN)); |
| 1320 | /* max signed is min(sign bit) | max(other bits) */ |
| 1321 | reg->s32_max_value = min_t(s32, reg->s32_max_value, |
| 1322 | var32_off.value | (var32_off.mask & S32_MAX)); |
| 1323 | reg->u32_min_value = max_t(u32, reg->u32_min_value, (u32)var32_off.value); |
| 1324 | reg->u32_max_value = min(reg->u32_max_value, |
| 1325 | (u32)(var32_off.value | var32_off.mask)); |
| 1326 | } |
| 1327 | |
| 1328 | static void __update_reg64_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1329 | { |
| 1330 | /* min signed is max(sign bit) | min(other bits) */ |
| 1331 | reg->smin_value = max_t(s64, reg->smin_value, |
| 1332 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 1333 | /* max signed is min(sign bit) | max(other bits) */ |
| 1334 | reg->smax_value = min_t(s64, reg->smax_value, |
| 1335 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 1336 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 1337 | reg->umax_value = min(reg->umax_value, |
| 1338 | reg->var_off.value | reg->var_off.mask); |
| 1339 | } |
| 1340 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1341 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 1342 | { |
| 1343 | __update_reg32_bounds(reg); |
| 1344 | __update_reg64_bounds(reg); |
| 1345 | } |
| 1346 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1347 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1348 | static void __reg32_deduce_bounds(struct bpf_reg_state *reg) |
| 1349 | { |
| 1350 | /* Learn sign from signed bounds. |
| 1351 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1352 | * are the same, so combine. This works even in the negative case, e.g. |
| 1353 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1354 | */ |
| 1355 | if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) { |
| 1356 | reg->s32_min_value = reg->u32_min_value = |
| 1357 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1358 | reg->s32_max_value = reg->u32_max_value = |
| 1359 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1360 | return; |
| 1361 | } |
| 1362 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1363 | * boundary, so we must be careful. |
| 1364 | */ |
| 1365 | if ((s32)reg->u32_max_value >= 0) { |
| 1366 | /* Positive. We can't learn anything from the smin, but smax |
| 1367 | * is positive, hence safe. |
| 1368 | */ |
| 1369 | reg->s32_min_value = reg->u32_min_value; |
| 1370 | reg->s32_max_value = reg->u32_max_value = |
| 1371 | min_t(u32, reg->s32_max_value, reg->u32_max_value); |
| 1372 | } else if ((s32)reg->u32_min_value < 0) { |
| 1373 | /* Negative. We can't learn anything from the smax, but smin |
| 1374 | * is negative, hence safe. |
| 1375 | */ |
| 1376 | reg->s32_min_value = reg->u32_min_value = |
| 1377 | max_t(u32, reg->s32_min_value, reg->u32_min_value); |
| 1378 | reg->s32_max_value = reg->u32_max_value; |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | static void __reg64_deduce_bounds(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1383 | { |
| 1384 | /* Learn sign from signed bounds. |
| 1385 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 1386 | * are the same, so combine. This works even in the negative case, e.g. |
| 1387 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 1388 | */ |
| 1389 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 1390 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1391 | reg->umin_value); |
| 1392 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1393 | reg->umax_value); |
| 1394 | return; |
| 1395 | } |
| 1396 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 1397 | * boundary, so we must be careful. |
| 1398 | */ |
| 1399 | if ((s64)reg->umax_value >= 0) { |
| 1400 | /* Positive. We can't learn anything from the smin, but smax |
| 1401 | * is positive, hence safe. |
| 1402 | */ |
| 1403 | reg->smin_value = reg->umin_value; |
| 1404 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 1405 | reg->umax_value); |
| 1406 | } else if ((s64)reg->umin_value < 0) { |
| 1407 | /* Negative. We can't learn anything from the smax, but smin |
| 1408 | * is negative, hence safe. |
| 1409 | */ |
| 1410 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 1411 | reg->umin_value); |
| 1412 | reg->smax_value = reg->umax_value; |
| 1413 | } |
| 1414 | } |
| 1415 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1416 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 1417 | { |
| 1418 | __reg32_deduce_bounds(reg); |
| 1419 | __reg64_deduce_bounds(reg); |
| 1420 | } |
| 1421 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1422 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 1423 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 1424 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1425 | struct tnum var64_off = tnum_intersect(reg->var_off, |
| 1426 | tnum_range(reg->umin_value, |
| 1427 | reg->umax_value)); |
| 1428 | struct tnum var32_off = tnum_intersect(tnum_subreg(reg->var_off), |
| 1429 | tnum_range(reg->u32_min_value, |
| 1430 | reg->u32_max_value)); |
| 1431 | |
| 1432 | reg->var_off = tnum_or(tnum_clear_subreg(var64_off), var32_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1435 | static void __reg_assign_32_into_64(struct bpf_reg_state *reg) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1436 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1437 | reg->umin_value = reg->u32_min_value; |
| 1438 | reg->umax_value = reg->u32_max_value; |
| 1439 | /* Attempt to pull 32-bit signed bounds into 64-bit bounds |
| 1440 | * but must be positive otherwise set to worse case bounds |
| 1441 | * and refine later from tnum. |
| 1442 | */ |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1443 | if (reg->s32_min_value >= 0 && reg->s32_max_value >= 0) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1444 | reg->smax_value = reg->s32_max_value; |
| 1445 | else |
| 1446 | reg->smax_value = U32_MAX; |
John Fastabend | 3a71dc3 | 2020-05-29 10:28:40 -0700 | [diff] [blame] | 1447 | if (reg->s32_min_value >= 0) |
| 1448 | reg->smin_value = reg->s32_min_value; |
| 1449 | else |
| 1450 | reg->smin_value = 0; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | static void __reg_combine_32_into_64(struct bpf_reg_state *reg) |
| 1454 | { |
| 1455 | /* special case when 64-bit register has upper 32-bit register |
| 1456 | * zeroed. Typically happens after zext or <<32, >>32 sequence |
| 1457 | * allowing us to use 32-bit bounds directly, |
| 1458 | */ |
| 1459 | if (tnum_equals_const(tnum_clear_subreg(reg->var_off), 0)) { |
| 1460 | __reg_assign_32_into_64(reg); |
| 1461 | } else { |
| 1462 | /* Otherwise the best we can do is push lower 32bit known and |
| 1463 | * unknown bits into register (var_off set from jmp logic) |
| 1464 | * then learn as much as possible from the 64-bit tnum |
| 1465 | * known and unknown bits. The previous smin/smax bounds are |
| 1466 | * invalid here because of jmp32 compare so mark them unknown |
| 1467 | * so they do not impact tnum bounds calculation. |
| 1468 | */ |
| 1469 | __mark_reg64_unbounded(reg); |
| 1470 | __update_reg_bounds(reg); |
| 1471 | } |
| 1472 | |
| 1473 | /* Intersecting with the old var_off might have improved our bounds |
| 1474 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1475 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1476 | */ |
| 1477 | __reg_deduce_bounds(reg); |
| 1478 | __reg_bound_offset(reg); |
| 1479 | __update_reg_bounds(reg); |
| 1480 | } |
| 1481 | |
| 1482 | static bool __reg64_bound_s32(s64 a) |
| 1483 | { |
Alexei Starovoitov | 388e2c0 | 2021-11-01 15:21:52 -0700 | [diff] [blame] | 1484 | return a >= S32_MIN && a <= S32_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | static bool __reg64_bound_u32(u64 a) |
| 1488 | { |
Alexei Starovoitov | b9979db | 2021-11-01 15:21:51 -0700 | [diff] [blame] | 1489 | return a >= U32_MIN && a <= U32_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | static void __reg_combine_64_into_32(struct bpf_reg_state *reg) |
| 1493 | { |
| 1494 | __mark_reg32_unbounded(reg); |
| 1495 | |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1496 | if (__reg64_bound_s32(reg->smin_value) && __reg64_bound_s32(reg->smax_value)) { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1497 | reg->s32_min_value = (s32)reg->smin_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1498 | reg->s32_max_value = (s32)reg->smax_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 1499 | } |
Daniel Borkmann | 10bf4e8 | 2021-04-23 13:59:55 +0000 | [diff] [blame] | 1500 | if (__reg64_bound_u32(reg->umin_value) && __reg64_bound_u32(reg->umax_value)) { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1501 | reg->u32_min_value = (u32)reg->umin_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1502 | reg->u32_max_value = (u32)reg->umax_value; |
Daniel Borkmann | 10bf4e8 | 2021-04-23 13:59:55 +0000 | [diff] [blame] | 1503 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 1504 | |
| 1505 | /* Intersecting with the old var_off might have improved our bounds |
| 1506 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 1507 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 1508 | */ |
| 1509 | __reg_deduce_bounds(reg); |
| 1510 | __reg_bound_offset(reg); |
| 1511 | __update_reg_bounds(reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1514 | /* Mark a register as having a completely unknown (scalar) value. */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1515 | static void __mark_reg_unknown(const struct bpf_verifier_env *env, |
| 1516 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1517 | { |
Alexei Starovoitov | a9c676b | 2018-09-04 19:13:44 -0700 | [diff] [blame] | 1518 | /* |
| 1519 | * Clear type, id, off, and union(map_ptr, range) and |
| 1520 | * padding between 'type' and union |
| 1521 | */ |
| 1522 | memset(reg, 0, offsetof(struct bpf_reg_state, var_off)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1523 | reg->type = SCALAR_VALUE; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1524 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1525 | reg->frameno = 0; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 1526 | reg->precise = env->subprog_cnt > 1 || !env->bpf_capable; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1527 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1528 | } |
| 1529 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1530 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 1531 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1532 | { |
| 1533 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1534 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1535 | /* Something bad happened, let's kill all regs except FP */ |
| 1536 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1537 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1538 | return; |
| 1539 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1540 | __mark_reg_unknown(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1543 | static void __mark_reg_not_init(const struct bpf_verifier_env *env, |
| 1544 | struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1545 | { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1546 | __mark_reg_unknown(env, reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1547 | reg->type = NOT_INIT; |
| 1548 | } |
| 1549 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1550 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 1551 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1552 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1553 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1554 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 1555 | /* Something bad happened, let's kill all regs except FP */ |
| 1556 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1557 | __mark_reg_not_init(env, regs + regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1558 | return; |
| 1559 | } |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 1560 | __mark_reg_not_init(env, regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 1561 | } |
| 1562 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1563 | static void mark_btf_ld_reg(struct bpf_verifier_env *env, |
| 1564 | struct bpf_reg_state *regs, u32 regno, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1565 | enum bpf_reg_type reg_type, |
| 1566 | struct btf *btf, u32 btf_id) |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1567 | { |
| 1568 | if (reg_type == SCALAR_VALUE) { |
| 1569 | mark_reg_unknown(env, regs, regno); |
| 1570 | return; |
| 1571 | } |
| 1572 | mark_reg_known_zero(env, regs, regno); |
| 1573 | regs[regno].type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 1574 | regs[regno].btf = btf; |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 1575 | regs[regno].btf_id = btf_id; |
| 1576 | } |
| 1577 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1578 | #define DEF_NOT_SUBREG (0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1579 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1580 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1581 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1582 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1583 | int i; |
| 1584 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1585 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1586 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1587 | regs[i].live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 1588 | regs[i].parent = NULL; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 1589 | regs[i].subreg_def = DEF_NOT_SUBREG; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1590 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1591 | |
| 1592 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1593 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1594 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1595 | regs[BPF_REG_FP].frameno = state->frameno; |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1598 | #define BPF_MAIN_FUNC (-1) |
| 1599 | static void init_func_state(struct bpf_verifier_env *env, |
| 1600 | struct bpf_func_state *state, |
| 1601 | int callsite, int frameno, int subprogno) |
| 1602 | { |
| 1603 | state->callsite = callsite; |
| 1604 | state->frameno = frameno; |
| 1605 | state->subprogno = subprogno; |
| 1606 | init_reg_state(env, state); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 1607 | mark_verifier_state_scratched(env); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 1610 | /* Similar to push_stack(), but for async callbacks */ |
| 1611 | static struct bpf_verifier_state *push_async_cb(struct bpf_verifier_env *env, |
| 1612 | int insn_idx, int prev_insn_idx, |
| 1613 | int subprog) |
| 1614 | { |
| 1615 | struct bpf_verifier_stack_elem *elem; |
| 1616 | struct bpf_func_state *frame; |
| 1617 | |
| 1618 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
| 1619 | if (!elem) |
| 1620 | goto err; |
| 1621 | |
| 1622 | elem->insn_idx = insn_idx; |
| 1623 | elem->prev_insn_idx = prev_insn_idx; |
| 1624 | elem->next = env->head; |
| 1625 | elem->log_pos = env->log.len_used; |
| 1626 | env->head = elem; |
| 1627 | env->stack_size++; |
| 1628 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) { |
| 1629 | verbose(env, |
| 1630 | "The sequence of %d jumps is too complex for async cb.\n", |
| 1631 | env->stack_size); |
| 1632 | goto err; |
| 1633 | } |
| 1634 | /* Unlike push_stack() do not copy_verifier_state(). |
| 1635 | * The caller state doesn't matter. |
| 1636 | * This is async callback. It starts in a fresh stack. |
| 1637 | * Initialize it similar to do_check_common(). |
| 1638 | */ |
| 1639 | elem->st.branches = 1; |
| 1640 | frame = kzalloc(sizeof(*frame), GFP_KERNEL); |
| 1641 | if (!frame) |
| 1642 | goto err; |
| 1643 | init_func_state(env, frame, |
| 1644 | BPF_MAIN_FUNC /* callsite */, |
| 1645 | 0 /* frameno within this callchain */, |
| 1646 | subprog /* subprog number within this prog */); |
| 1647 | elem->st.frame[0] = frame; |
| 1648 | return &elem->st; |
| 1649 | err: |
| 1650 | free_verifier_state(env->cur_state, true); |
| 1651 | env->cur_state = NULL; |
| 1652 | /* pop all elements and return */ |
| 1653 | while (!pop_stack(env, NULL, NULL, false)); |
| 1654 | return NULL; |
| 1655 | } |
| 1656 | |
| 1657 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1658 | enum reg_arg_type { |
| 1659 | SRC_OP, /* register is used as source operand */ |
| 1660 | DST_OP, /* register is used as destination operand */ |
| 1661 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 1662 | }; |
| 1663 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1664 | static int cmp_subprogs(const void *a, const void *b) |
| 1665 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1666 | return ((struct bpf_subprog_info *)a)->start - |
| 1667 | ((struct bpf_subprog_info *)b)->start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 1671 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1672 | struct bpf_subprog_info *p; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1673 | |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1674 | p = bsearch(&off, env->subprog_info, env->subprog_cnt, |
| 1675 | sizeof(env->subprog_info[0]), cmp_subprogs); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1676 | if (!p) |
| 1677 | return -ENOENT; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1678 | return p - env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1679 | |
| 1680 | } |
| 1681 | |
| 1682 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 1683 | { |
| 1684 | int insn_cnt = env->prog->len; |
| 1685 | int ret; |
| 1686 | |
| 1687 | if (off >= insn_cnt || off < 0) { |
| 1688 | verbose(env, "call to invalid destination\n"); |
| 1689 | return -EINVAL; |
| 1690 | } |
| 1691 | ret = find_subprog(env, off); |
| 1692 | if (ret >= 0) |
Yonghong Song | 282a0f4 | 2021-02-26 12:49:24 -0800 | [diff] [blame] | 1693 | return ret; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 1694 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1695 | verbose(env, "too many subprograms\n"); |
| 1696 | return -E2BIG; |
| 1697 | } |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1698 | /* determine subprog starts. The end is one before the next starts */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 1699 | env->subprog_info[env->subprog_cnt++].start = off; |
| 1700 | sort(env->subprog_info, env->subprog_cnt, |
| 1701 | sizeof(env->subprog_info[0]), cmp_subprogs, NULL); |
Yonghong Song | 282a0f4 | 2021-02-26 12:49:24 -0800 | [diff] [blame] | 1702 | return env->subprog_cnt - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1705 | #define MAX_KFUNC_DESCS 256 |
| 1706 | #define MAX_KFUNC_BTFS 256 |
| 1707 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1708 | struct bpf_kfunc_desc { |
| 1709 | struct btf_func_model func_model; |
| 1710 | u32 func_id; |
| 1711 | s32 imm; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1712 | u16 offset; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1713 | }; |
| 1714 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1715 | struct bpf_kfunc_btf { |
| 1716 | struct btf *btf; |
| 1717 | struct module *module; |
| 1718 | u16 offset; |
| 1719 | }; |
| 1720 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1721 | struct bpf_kfunc_desc_tab { |
| 1722 | struct bpf_kfunc_desc descs[MAX_KFUNC_DESCS]; |
| 1723 | u32 nr_descs; |
| 1724 | }; |
| 1725 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1726 | struct bpf_kfunc_btf_tab { |
| 1727 | struct bpf_kfunc_btf descs[MAX_KFUNC_BTFS]; |
| 1728 | u32 nr_descs; |
| 1729 | }; |
| 1730 | |
| 1731 | static int kfunc_desc_cmp_by_id_off(const void *a, const void *b) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 1732 | { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1733 | const struct bpf_kfunc_desc *d0 = a; |
| 1734 | const struct bpf_kfunc_desc *d1 = b; |
| 1735 | |
| 1736 | /* func_id is not greater than BTF_MAX_TYPE */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1737 | return d0->func_id - d1->func_id ?: d0->offset - d1->offset; |
| 1738 | } |
| 1739 | |
| 1740 | static int kfunc_btf_cmp_by_off(const void *a, const void *b) |
| 1741 | { |
| 1742 | const struct bpf_kfunc_btf *d0 = a; |
| 1743 | const struct bpf_kfunc_btf *d1 = b; |
| 1744 | |
| 1745 | return d0->offset - d1->offset; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | static const struct bpf_kfunc_desc * |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1749 | find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1750 | { |
| 1751 | struct bpf_kfunc_desc desc = { |
| 1752 | .func_id = func_id, |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1753 | .offset = offset, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1754 | }; |
| 1755 | struct bpf_kfunc_desc_tab *tab; |
| 1756 | |
| 1757 | tab = prog->aux->kfunc_tab; |
| 1758 | return bsearch(&desc, tab->descs, tab->nr_descs, |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1759 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_id_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1762 | static struct btf *__find_kfunc_desc_btf(struct bpf_verifier_env *env, |
| 1763 | s16 offset, struct module **btf_modp) |
| 1764 | { |
| 1765 | struct bpf_kfunc_btf kf_btf = { .offset = offset }; |
| 1766 | struct bpf_kfunc_btf_tab *tab; |
| 1767 | struct bpf_kfunc_btf *b; |
| 1768 | struct module *mod; |
| 1769 | struct btf *btf; |
| 1770 | int btf_fd; |
| 1771 | |
| 1772 | tab = env->prog->aux->kfunc_btf_tab; |
| 1773 | b = bsearch(&kf_btf, tab->descs, tab->nr_descs, |
| 1774 | sizeof(tab->descs[0]), kfunc_btf_cmp_by_off); |
| 1775 | if (!b) { |
| 1776 | if (tab->nr_descs == MAX_KFUNC_BTFS) { |
| 1777 | verbose(env, "too many different module BTFs\n"); |
| 1778 | return ERR_PTR(-E2BIG); |
| 1779 | } |
| 1780 | |
| 1781 | if (bpfptr_is_null(env->fd_array)) { |
| 1782 | verbose(env, "kfunc offset > 0 without fd_array is invalid\n"); |
| 1783 | return ERR_PTR(-EPROTO); |
| 1784 | } |
| 1785 | |
| 1786 | if (copy_from_bpfptr_offset(&btf_fd, env->fd_array, |
| 1787 | offset * sizeof(btf_fd), |
| 1788 | sizeof(btf_fd))) |
| 1789 | return ERR_PTR(-EFAULT); |
| 1790 | |
| 1791 | btf = btf_get_by_fd(btf_fd); |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1792 | if (IS_ERR(btf)) { |
| 1793 | verbose(env, "invalid module BTF fd specified\n"); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1794 | return btf; |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1795 | } |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1796 | |
| 1797 | if (!btf_is_module(btf)) { |
| 1798 | verbose(env, "BTF fd for kfunc is not a module BTF\n"); |
| 1799 | btf_put(btf); |
| 1800 | return ERR_PTR(-EINVAL); |
| 1801 | } |
| 1802 | |
| 1803 | mod = btf_try_get_module(btf); |
| 1804 | if (!mod) { |
| 1805 | btf_put(btf); |
| 1806 | return ERR_PTR(-ENXIO); |
| 1807 | } |
| 1808 | |
| 1809 | b = &tab->descs[tab->nr_descs++]; |
| 1810 | b->btf = btf; |
| 1811 | b->module = mod; |
| 1812 | b->offset = offset; |
| 1813 | |
| 1814 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
| 1815 | kfunc_btf_cmp_by_off, NULL); |
| 1816 | } |
| 1817 | if (btf_modp) |
| 1818 | *btf_modp = b->module; |
| 1819 | return b->btf; |
| 1820 | } |
| 1821 | |
| 1822 | void bpf_free_kfunc_btf_tab(struct bpf_kfunc_btf_tab *tab) |
| 1823 | { |
| 1824 | if (!tab) |
| 1825 | return; |
| 1826 | |
| 1827 | while (tab->nr_descs--) { |
| 1828 | module_put(tab->descs[tab->nr_descs].module); |
| 1829 | btf_put(tab->descs[tab->nr_descs].btf); |
| 1830 | } |
| 1831 | kfree(tab); |
| 1832 | } |
| 1833 | |
| 1834 | static struct btf *find_kfunc_desc_btf(struct bpf_verifier_env *env, |
| 1835 | u32 func_id, s16 offset, |
| 1836 | struct module **btf_modp) |
| 1837 | { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1838 | if (offset) { |
| 1839 | if (offset < 0) { |
| 1840 | /* In the future, this can be allowed to increase limit |
| 1841 | * of fd index into fd_array, interpreted as u16. |
| 1842 | */ |
| 1843 | verbose(env, "negative offset disallowed for kernel module function call\n"); |
| 1844 | return ERR_PTR(-EINVAL); |
| 1845 | } |
| 1846 | |
Kumar Kartikeya Dwivedi | 588cd7ef | 2021-10-09 09:39:00 +0530 | [diff] [blame] | 1847 | return __find_kfunc_desc_btf(env, offset, btf_modp); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1848 | } |
| 1849 | return btf_vmlinux ?: ERR_PTR(-ENOENT); |
| 1850 | } |
| 1851 | |
| 1852 | static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1853 | { |
| 1854 | const struct btf_type *func, *func_proto; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1855 | struct bpf_kfunc_btf_tab *btf_tab; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1856 | struct bpf_kfunc_desc_tab *tab; |
| 1857 | struct bpf_prog_aux *prog_aux; |
| 1858 | struct bpf_kfunc_desc *desc; |
| 1859 | const char *func_name; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1860 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1861 | unsigned long addr; |
| 1862 | int err; |
| 1863 | |
| 1864 | prog_aux = env->prog->aux; |
| 1865 | tab = prog_aux->kfunc_tab; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1866 | btf_tab = prog_aux->kfunc_btf_tab; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1867 | if (!tab) { |
| 1868 | if (!btf_vmlinux) { |
| 1869 | verbose(env, "calling kernel function is not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 1870 | return -ENOTSUPP; |
| 1871 | } |
| 1872 | |
| 1873 | if (!env->prog->jit_requested) { |
| 1874 | verbose(env, "JIT is required for calling kernel function\n"); |
| 1875 | return -ENOTSUPP; |
| 1876 | } |
| 1877 | |
| 1878 | if (!bpf_jit_supports_kfunc_call()) { |
| 1879 | verbose(env, "JIT does not support calling kernel function\n"); |
| 1880 | return -ENOTSUPP; |
| 1881 | } |
| 1882 | |
| 1883 | if (!env->prog->gpl_compatible) { |
| 1884 | verbose(env, "cannot call kernel function from non-GPL compatible program\n"); |
| 1885 | return -EINVAL; |
| 1886 | } |
| 1887 | |
| 1888 | tab = kzalloc(sizeof(*tab), GFP_KERNEL); |
| 1889 | if (!tab) |
| 1890 | return -ENOMEM; |
| 1891 | prog_aux->kfunc_tab = tab; |
| 1892 | } |
| 1893 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 1894 | /* func_id == 0 is always invalid, but instead of returning an error, be |
| 1895 | * conservative and wait until the code elimination pass before returning |
| 1896 | * error, so that invalid calls that get pruned out can be in BPF programs |
| 1897 | * loaded from userspace. It is also required that offset be untouched |
| 1898 | * for such calls. |
| 1899 | */ |
| 1900 | if (!func_id && !offset) |
| 1901 | return 0; |
| 1902 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1903 | if (!btf_tab && offset) { |
| 1904 | btf_tab = kzalloc(sizeof(*btf_tab), GFP_KERNEL); |
| 1905 | if (!btf_tab) |
| 1906 | return -ENOMEM; |
| 1907 | prog_aux->kfunc_btf_tab = btf_tab; |
| 1908 | } |
| 1909 | |
| 1910 | desc_btf = find_kfunc_desc_btf(env, func_id, offset, NULL); |
| 1911 | if (IS_ERR(desc_btf)) { |
| 1912 | verbose(env, "failed to find BTF for kernel function\n"); |
| 1913 | return PTR_ERR(desc_btf); |
| 1914 | } |
| 1915 | |
| 1916 | if (find_kfunc_desc(env->prog, func_id, offset)) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1917 | return 0; |
| 1918 | |
| 1919 | if (tab->nr_descs == MAX_KFUNC_DESCS) { |
| 1920 | verbose(env, "too many different kernel function calls\n"); |
| 1921 | return -E2BIG; |
| 1922 | } |
| 1923 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1924 | func = btf_type_by_id(desc_btf, func_id); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1925 | if (!func || !btf_type_is_func(func)) { |
| 1926 | verbose(env, "kernel btf_id %u is not a function\n", |
| 1927 | func_id); |
| 1928 | return -EINVAL; |
| 1929 | } |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1930 | func_proto = btf_type_by_id(desc_btf, func->type); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1931 | if (!func_proto || !btf_type_is_func_proto(func_proto)) { |
| 1932 | verbose(env, "kernel function btf_id %u does not have a valid func_proto\n", |
| 1933 | func_id); |
| 1934 | return -EINVAL; |
| 1935 | } |
| 1936 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1937 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1938 | addr = kallsyms_lookup_name(func_name); |
| 1939 | if (!addr) { |
| 1940 | verbose(env, "cannot find address for kernel function %s\n", |
| 1941 | func_name); |
| 1942 | return -EINVAL; |
| 1943 | } |
| 1944 | |
| 1945 | desc = &tab->descs[tab->nr_descs++]; |
| 1946 | desc->func_id = func_id; |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 1947 | desc->imm = BPF_CALL_IMM(addr); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1948 | desc->offset = offset; |
| 1949 | err = btf_distill_func_proto(&env->log, desc_btf, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1950 | func_proto, func_name, |
| 1951 | &desc->func_model); |
| 1952 | if (!err) |
| 1953 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 1954 | kfunc_desc_cmp_by_id_off, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 1955 | return err; |
| 1956 | } |
| 1957 | |
| 1958 | static int kfunc_desc_cmp_by_imm(const void *a, const void *b) |
| 1959 | { |
| 1960 | const struct bpf_kfunc_desc *d0 = a; |
| 1961 | const struct bpf_kfunc_desc *d1 = b; |
| 1962 | |
| 1963 | if (d0->imm > d1->imm) |
| 1964 | return 1; |
| 1965 | else if (d0->imm < d1->imm) |
| 1966 | return -1; |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | static void sort_kfunc_descs_by_imm(struct bpf_prog *prog) |
| 1971 | { |
| 1972 | struct bpf_kfunc_desc_tab *tab; |
| 1973 | |
| 1974 | tab = prog->aux->kfunc_tab; |
| 1975 | if (!tab) |
| 1976 | return; |
| 1977 | |
| 1978 | sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]), |
| 1979 | kfunc_desc_cmp_by_imm, NULL); |
| 1980 | } |
| 1981 | |
| 1982 | bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog) |
| 1983 | { |
| 1984 | return !!prog->aux->kfunc_tab; |
| 1985 | } |
| 1986 | |
| 1987 | const struct btf_func_model * |
| 1988 | bpf_jit_find_kfunc_model(const struct bpf_prog *prog, |
| 1989 | const struct bpf_insn *insn) |
| 1990 | { |
| 1991 | const struct bpf_kfunc_desc desc = { |
| 1992 | .imm = insn->imm, |
| 1993 | }; |
| 1994 | const struct bpf_kfunc_desc *res; |
| 1995 | struct bpf_kfunc_desc_tab *tab; |
| 1996 | |
| 1997 | tab = prog->aux->kfunc_tab; |
| 1998 | res = bsearch(&desc, tab->descs, tab->nr_descs, |
| 1999 | sizeof(tab->descs[0]), kfunc_desc_cmp_by_imm); |
| 2000 | |
| 2001 | return res ? &res->func_model : NULL; |
| 2002 | } |
| 2003 | |
| 2004 | static int add_subprog_and_kfunc(struct bpf_verifier_env *env) |
| 2005 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2006 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2007 | struct bpf_insn *insn = env->prog->insnsi; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2008 | int i, ret, insn_cnt = env->prog->len; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2009 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2010 | /* Add entry function. */ |
| 2011 | ret = add_subprog(env, 0); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2012 | if (ret) |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 2013 | return ret; |
| 2014 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2015 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 2016 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn) && |
| 2017 | !bpf_pseudo_kfunc_call(insn)) |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 2018 | continue; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2019 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2020 | if (!env->bpf_capable) { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2021 | verbose(env, "loading/calling other bpf or kernel functions are allowed for CAP_BPF and CAP_SYS_ADMIN\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2022 | return -EPERM; |
| 2023 | } |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2024 | |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 2025 | if (bpf_pseudo_func(insn) || bpf_pseudo_call(insn)) |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2026 | ret = add_subprog(env, i + insn->imm + 1); |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 2027 | else |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2028 | ret = add_kfunc_call(env, insn->imm, insn->off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2029 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2030 | if (ret < 0) |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2034 | /* Add a fake 'exit' subprog which could simplify subprog iteration |
| 2035 | * logic. 'subprog_cnt' should not be increased. |
| 2036 | */ |
| 2037 | subprog[env->subprog_cnt].start = insn_cnt; |
| 2038 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2039 | if (env->log.level & BPF_LOG_LEVEL2) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2040 | for (i = 0; i < env->subprog_cnt; i++) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2041 | verbose(env, "func#%d @%d\n", i, subprog[i].start); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2042 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2043 | return 0; |
| 2044 | } |
| 2045 | |
| 2046 | static int check_subprogs(struct bpf_verifier_env *env) |
| 2047 | { |
| 2048 | int i, subprog_start, subprog_end, off, cur_subprog = 0; |
| 2049 | struct bpf_subprog_info *subprog = env->subprog_info; |
| 2050 | struct bpf_insn *insn = env->prog->insnsi; |
| 2051 | int insn_cnt = env->prog->len; |
| 2052 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2053 | /* now check that all jumps are within the same subprog */ |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2054 | subprog_start = subprog[cur_subprog].start; |
| 2055 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2056 | for (i = 0; i < insn_cnt; i++) { |
| 2057 | u8 code = insn[i].code; |
| 2058 | |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 2059 | if (code == (BPF_JMP | BPF_CALL) && |
| 2060 | insn[i].imm == BPF_FUNC_tail_call && |
| 2061 | insn[i].src_reg != BPF_PSEUDO_CALL) |
| 2062 | subprog[cur_subprog].has_tail_call = true; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 2063 | if (BPF_CLASS(code) == BPF_LD && |
| 2064 | (BPF_MODE(code) == BPF_ABS || BPF_MODE(code) == BPF_IND)) |
| 2065 | subprog[cur_subprog].has_ld_abs = true; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 2066 | if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2067 | goto next; |
| 2068 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 2069 | goto next; |
| 2070 | off = i + insn[i].off + 1; |
| 2071 | if (off < subprog_start || off >= subprog_end) { |
| 2072 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 2073 | return -EINVAL; |
| 2074 | } |
| 2075 | next: |
| 2076 | if (i == subprog_end - 1) { |
| 2077 | /* to avoid fall-through from one subprog into another |
| 2078 | * the last insn of the subprog should be either exit |
| 2079 | * or unconditional jump back |
| 2080 | */ |
| 2081 | if (code != (BPF_JMP | BPF_EXIT) && |
| 2082 | code != (BPF_JMP | BPF_JA)) { |
| 2083 | verbose(env, "last insn is not an exit or jmp\n"); |
| 2084 | return -EINVAL; |
| 2085 | } |
| 2086 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 2087 | cur_subprog++; |
| 2088 | if (cur_subprog < env->subprog_cnt) |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 2089 | subprog_end = subprog[cur_subprog + 1].start; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2095 | /* Parentage chain of this register (or stack slot) should take care of all |
| 2096 | * issues like callee-saved registers, stack slot allocation time, etc. |
| 2097 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2098 | static int mark_reg_read(struct bpf_verifier_env *env, |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2099 | const struct bpf_reg_state *state, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2100 | struct bpf_reg_state *parent, u8 flag) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2101 | { |
| 2102 | bool writes = parent == state->parent; /* Observe write marks */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2103 | int cnt = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2104 | |
| 2105 | while (parent) { |
| 2106 | /* if read wasn't screened by an earlier write ... */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2107 | if (writes && state->live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2108 | break; |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 2109 | if (parent->live & REG_LIVE_DONE) { |
| 2110 | verbose(env, "verifier BUG type %s var_off %lld off %d\n", |
| 2111 | reg_type_str[parent->type], |
| 2112 | parent->var_off.value, parent->off); |
| 2113 | return -EFAULT; |
| 2114 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2115 | /* The first condition is more likely to be true than the |
| 2116 | * second, checked it first. |
| 2117 | */ |
| 2118 | if ((parent->live & REG_LIVE_READ) == flag || |
| 2119 | parent->live & REG_LIVE_READ64) |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 2120 | /* The parentage chain never changes and |
| 2121 | * this parent was already marked as LIVE_READ. |
| 2122 | * There is no need to keep walking the chain again and |
| 2123 | * keep re-marking all parents as LIVE_READ. |
| 2124 | * This case happens when the same register is read |
| 2125 | * multiple times without writes into it in-between. |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2126 | * Also, if parent has the stronger REG_LIVE_READ64 set, |
| 2127 | * then no need to set the weak REG_LIVE_READ32. |
Alexei Starovoitov | 25af32d | 2019-04-01 21:27:42 -0700 | [diff] [blame] | 2128 | */ |
| 2129 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2130 | /* ... then we depend on parent's value */ |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2131 | parent->live |= flag; |
| 2132 | /* REG_LIVE_READ64 overrides REG_LIVE_READ32. */ |
| 2133 | if (flag == REG_LIVE_READ64) |
| 2134 | parent->live &= ~REG_LIVE_READ32; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2135 | state = parent; |
| 2136 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2137 | writes = true; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2138 | cnt++; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2139 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 2140 | |
| 2141 | if (env->longest_mark_read_walk < cnt) |
| 2142 | env->longest_mark_read_walk = cnt; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2143 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2146 | /* This function is supposed to be used by the following 32-bit optimization |
| 2147 | * code only. It returns TRUE if the source or destination register operates |
| 2148 | * on 64-bit, otherwise return FALSE. |
| 2149 | */ |
| 2150 | static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2151 | u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t) |
| 2152 | { |
| 2153 | u8 code, class, op; |
| 2154 | |
| 2155 | code = insn->code; |
| 2156 | class = BPF_CLASS(code); |
| 2157 | op = BPF_OP(code); |
| 2158 | if (class == BPF_JMP) { |
| 2159 | /* BPF_EXIT for "main" will reach here. Return TRUE |
| 2160 | * conservatively. |
| 2161 | */ |
| 2162 | if (op == BPF_EXIT) |
| 2163 | return true; |
| 2164 | if (op == BPF_CALL) { |
| 2165 | /* BPF to BPF call will reach here because of marking |
| 2166 | * caller saved clobber with DST_OP_NO_MARK for which we |
| 2167 | * don't care the register def because they are anyway |
| 2168 | * marked as NOT_INIT already. |
| 2169 | */ |
| 2170 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 2171 | return false; |
| 2172 | /* Helper call will reach here because of arg type |
| 2173 | * check, conservatively return TRUE. |
| 2174 | */ |
| 2175 | if (t == SRC_OP) |
| 2176 | return true; |
| 2177 | |
| 2178 | return false; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | if (class == BPF_ALU64 || class == BPF_JMP || |
| 2183 | /* BPF_END always use BPF_ALU class. */ |
| 2184 | (class == BPF_ALU && op == BPF_END && insn->imm == 64)) |
| 2185 | return true; |
| 2186 | |
| 2187 | if (class == BPF_ALU || class == BPF_JMP32) |
| 2188 | return false; |
| 2189 | |
| 2190 | if (class == BPF_LDX) { |
| 2191 | if (t != SRC_OP) |
| 2192 | return BPF_SIZE(code) == BPF_DW; |
| 2193 | /* LDX source must be ptr. */ |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
| 2197 | if (class == BPF_STX) { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2198 | /* BPF_STX (including atomic variants) has multiple source |
| 2199 | * operands, one of which is a ptr. Check whether the caller is |
| 2200 | * asking about it. |
| 2201 | */ |
| 2202 | if (t == SRC_OP && reg->type != SCALAR_VALUE) |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2203 | return true; |
| 2204 | return BPF_SIZE(code) == BPF_DW; |
| 2205 | } |
| 2206 | |
| 2207 | if (class == BPF_LD) { |
| 2208 | u8 mode = BPF_MODE(code); |
| 2209 | |
| 2210 | /* LD_IMM64 */ |
| 2211 | if (mode == BPF_IMM) |
| 2212 | return true; |
| 2213 | |
| 2214 | /* Both LD_IND and LD_ABS return 32-bit data. */ |
| 2215 | if (t != SRC_OP) |
| 2216 | return false; |
| 2217 | |
| 2218 | /* Implicit ctx ptr. */ |
| 2219 | if (regno == BPF_REG_6) |
| 2220 | return true; |
| 2221 | |
| 2222 | /* Explicit source could be any width. */ |
| 2223 | return true; |
| 2224 | } |
| 2225 | |
| 2226 | if (class == BPF_ST) |
| 2227 | /* The only source register for BPF_ST is a ptr. */ |
| 2228 | return true; |
| 2229 | |
| 2230 | /* Conservatively return true at default. */ |
| 2231 | return true; |
| 2232 | } |
| 2233 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2234 | /* Return the regno defined by the insn, or -1. */ |
| 2235 | static int insn_def_regno(const struct bpf_insn *insn) |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2236 | { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2237 | switch (BPF_CLASS(insn->code)) { |
| 2238 | case BPF_JMP: |
| 2239 | case BPF_JMP32: |
| 2240 | case BPF_ST: |
| 2241 | return -1; |
| 2242 | case BPF_STX: |
| 2243 | if (BPF_MODE(insn->code) == BPF_ATOMIC && |
| 2244 | (insn->imm & BPF_FETCH)) { |
| 2245 | if (insn->imm == BPF_CMPXCHG) |
| 2246 | return BPF_REG_0; |
| 2247 | else |
| 2248 | return insn->src_reg; |
| 2249 | } else { |
| 2250 | return -1; |
| 2251 | } |
| 2252 | default: |
| 2253 | return insn->dst_reg; |
| 2254 | } |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* Return TRUE if INSN has defined any 32-bit value explicitly. */ |
| 2258 | static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 2259 | { |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2260 | int dst_reg = insn_def_regno(insn); |
| 2261 | |
| 2262 | if (dst_reg == -1) |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2263 | return false; |
| 2264 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 2265 | return !is_reg64(env, insn, dst_reg, NULL, DST_OP); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2268 | static void mark_insn_zext(struct bpf_verifier_env *env, |
| 2269 | struct bpf_reg_state *reg) |
| 2270 | { |
| 2271 | s32 def_idx = reg->subreg_def; |
| 2272 | |
| 2273 | if (def_idx == DEF_NOT_SUBREG) |
| 2274 | return; |
| 2275 | |
| 2276 | env->insn_aux_data[def_idx - 1].zext_dst = true; |
| 2277 | /* The dst will be zero extended, so won't be sub-register anymore. */ |
| 2278 | reg->subreg_def = DEF_NOT_SUBREG; |
| 2279 | } |
| 2280 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2281 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2282 | enum reg_arg_type t) |
| 2283 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2284 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2285 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2286 | struct bpf_insn *insn = env->prog->insnsi + env->insn_idx; |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2287 | struct bpf_reg_state *reg, *regs = state->regs; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2288 | bool rw64; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2289 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2290 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2291 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2292 | return -EINVAL; |
| 2293 | } |
| 2294 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 2295 | mark_reg_scratched(env, regno); |
| 2296 | |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2297 | reg = ®s[regno]; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2298 | rw64 = is_reg64(env, insn, regno, reg, t); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2299 | if (t == SRC_OP) { |
| 2300 | /* check whether register used as source operand can be read */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2301 | if (reg->type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2302 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2303 | return -EACCES; |
| 2304 | } |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2305 | /* We don't need to worry about FP liveness because it's read-only */ |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2306 | if (regno == BPF_REG_FP) |
| 2307 | return 0; |
| 2308 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2309 | if (rw64) |
| 2310 | mark_insn_zext(env, reg); |
| 2311 | |
| 2312 | return mark_reg_read(env, reg, reg->parent, |
| 2313 | rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2314 | } else { |
| 2315 | /* check whether register used as dest operand can be written to */ |
| 2316 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2317 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2318 | return -EACCES; |
| 2319 | } |
Jiong Wang | c342dc1 | 2019-04-12 22:59:37 +0100 | [diff] [blame] | 2320 | reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 2321 | reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2322 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2323 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2324 | } |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2328 | /* for any branch, call, exit record the history of jmps in the given state */ |
| 2329 | static int push_jmp_history(struct bpf_verifier_env *env, |
| 2330 | struct bpf_verifier_state *cur) |
| 2331 | { |
| 2332 | u32 cnt = cur->jmp_history_cnt; |
| 2333 | struct bpf_idx_pair *p; |
| 2334 | |
| 2335 | cnt++; |
| 2336 | p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER); |
| 2337 | if (!p) |
| 2338 | return -ENOMEM; |
| 2339 | p[cnt - 1].idx = env->insn_idx; |
| 2340 | p[cnt - 1].prev_idx = env->prev_insn_idx; |
| 2341 | cur->jmp_history = p; |
| 2342 | cur->jmp_history_cnt = cnt; |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
| 2346 | /* Backtrack one insn at a time. If idx is not at the top of recorded |
| 2347 | * history then previous instruction came from straight line execution. |
| 2348 | */ |
| 2349 | static int get_prev_insn_idx(struct bpf_verifier_state *st, int i, |
| 2350 | u32 *history) |
| 2351 | { |
| 2352 | u32 cnt = *history; |
| 2353 | |
| 2354 | if (cnt && st->jmp_history[cnt - 1].idx == i) { |
| 2355 | i = st->jmp_history[cnt - 1].prev_idx; |
| 2356 | (*history)--; |
| 2357 | } else { |
| 2358 | i--; |
| 2359 | } |
| 2360 | return i; |
| 2361 | } |
| 2362 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2363 | static const char *disasm_kfunc_name(void *data, const struct bpf_insn *insn) |
| 2364 | { |
| 2365 | const struct btf_type *func; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2366 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2367 | |
| 2368 | if (insn->src_reg != BPF_PSEUDO_KFUNC_CALL) |
| 2369 | return NULL; |
| 2370 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 2371 | desc_btf = find_kfunc_desc_btf(data, insn->imm, insn->off, NULL); |
| 2372 | if (IS_ERR(desc_btf)) |
| 2373 | return "<error>"; |
| 2374 | |
| 2375 | func = btf_type_by_id(desc_btf, insn->imm); |
| 2376 | return btf_name_by_offset(desc_btf, func->name_off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2379 | /* For given verifier state backtrack_insn() is called from the last insn to |
| 2380 | * the first insn. Its purpose is to compute a bitmask of registers and |
| 2381 | * stack slots that needs precision in the parent verifier state. |
| 2382 | */ |
| 2383 | static int backtrack_insn(struct bpf_verifier_env *env, int idx, |
| 2384 | u32 *reg_mask, u64 *stack_mask) |
| 2385 | { |
| 2386 | const struct bpf_insn_cbs cbs = { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 2387 | .cb_call = disasm_kfunc_name, |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2388 | .cb_print = verbose, |
| 2389 | .private_data = env, |
| 2390 | }; |
| 2391 | struct bpf_insn *insn = env->prog->insnsi + idx; |
| 2392 | u8 class = BPF_CLASS(insn->code); |
| 2393 | u8 opcode = BPF_OP(insn->code); |
| 2394 | u8 mode = BPF_MODE(insn->code); |
| 2395 | u32 dreg = 1u << insn->dst_reg; |
| 2396 | u32 sreg = 1u << insn->src_reg; |
| 2397 | u32 spi; |
| 2398 | |
| 2399 | if (insn->code == 0) |
| 2400 | return 0; |
| 2401 | if (env->log.level & BPF_LOG_LEVEL) { |
| 2402 | verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask); |
| 2403 | verbose(env, "%d: ", idx); |
| 2404 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
| 2405 | } |
| 2406 | |
| 2407 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 2408 | if (!(*reg_mask & dreg)) |
| 2409 | return 0; |
| 2410 | if (opcode == BPF_MOV) { |
| 2411 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2412 | /* dreg = sreg |
| 2413 | * dreg needs precision after this insn |
| 2414 | * sreg needs precision before this insn |
| 2415 | */ |
| 2416 | *reg_mask &= ~dreg; |
| 2417 | *reg_mask |= sreg; |
| 2418 | } else { |
| 2419 | /* dreg = K |
| 2420 | * dreg needs precision after this insn. |
| 2421 | * Corresponding register is already marked |
| 2422 | * as precise=true in this verifier state. |
| 2423 | * No further markings in parent are necessary |
| 2424 | */ |
| 2425 | *reg_mask &= ~dreg; |
| 2426 | } |
| 2427 | } else { |
| 2428 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2429 | /* dreg += sreg |
| 2430 | * both dreg and sreg need precision |
| 2431 | * before this insn |
| 2432 | */ |
| 2433 | *reg_mask |= sreg; |
| 2434 | } /* else dreg += K |
| 2435 | * dreg still needs precision before this insn |
| 2436 | */ |
| 2437 | } |
| 2438 | } else if (class == BPF_LDX) { |
| 2439 | if (!(*reg_mask & dreg)) |
| 2440 | return 0; |
| 2441 | *reg_mask &= ~dreg; |
| 2442 | |
| 2443 | /* scalars can only be spilled into stack w/o losing precision. |
| 2444 | * Load from any other memory can be zero extended. |
| 2445 | * The desire to keep that precision is already indicated |
| 2446 | * by 'precise' mark in corresponding register of this state. |
| 2447 | * No further tracking necessary. |
| 2448 | */ |
| 2449 | if (insn->src_reg != BPF_REG_FP) |
| 2450 | return 0; |
| 2451 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 2452 | return 0; |
| 2453 | |
| 2454 | /* dreg = *(u64 *)[fp - off] was a fill from the stack. |
| 2455 | * that [fp - off] slot contains scalar that needs to be |
| 2456 | * tracked with precision |
| 2457 | */ |
| 2458 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 2459 | if (spi >= 64) { |
| 2460 | verbose(env, "BUG spi %d\n", spi); |
| 2461 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2462 | return -EFAULT; |
| 2463 | } |
| 2464 | *stack_mask |= 1ull << spi; |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2465 | } else if (class == BPF_STX || class == BPF_ST) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2466 | if (*reg_mask & dreg) |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2467 | /* stx & st shouldn't be using _scalar_ dst_reg |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2468 | * to access memory. It means backtracking |
| 2469 | * encountered a case of pointer subtraction. |
| 2470 | */ |
| 2471 | return -ENOTSUPP; |
| 2472 | /* scalars can only be spilled into stack */ |
| 2473 | if (insn->dst_reg != BPF_REG_FP) |
| 2474 | return 0; |
| 2475 | if (BPF_SIZE(insn->code) != BPF_DW) |
| 2476 | return 0; |
| 2477 | spi = (-insn->off - 1) / BPF_REG_SIZE; |
| 2478 | if (spi >= 64) { |
| 2479 | verbose(env, "BUG spi %d\n", spi); |
| 2480 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2481 | return -EFAULT; |
| 2482 | } |
| 2483 | if (!(*stack_mask & (1ull << spi))) |
| 2484 | return 0; |
| 2485 | *stack_mask &= ~(1ull << spi); |
Andrii Nakryiko | b3b50f0 | 2019-07-08 20:32:44 -0700 | [diff] [blame] | 2486 | if (class == BPF_STX) |
| 2487 | *reg_mask |= sreg; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2488 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
| 2489 | if (opcode == BPF_CALL) { |
| 2490 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 2491 | return -ENOTSUPP; |
| 2492 | /* regular helper call sets R0 */ |
| 2493 | *reg_mask &= ~1; |
| 2494 | if (*reg_mask & 0x3f) { |
| 2495 | /* if backtracing was looking for registers R1-R5 |
| 2496 | * they should have been found already. |
| 2497 | */ |
| 2498 | verbose(env, "BUG regs %x\n", *reg_mask); |
| 2499 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2500 | return -EFAULT; |
| 2501 | } |
| 2502 | } else if (opcode == BPF_EXIT) { |
| 2503 | return -ENOTSUPP; |
| 2504 | } |
| 2505 | } else if (class == BPF_LD) { |
| 2506 | if (!(*reg_mask & dreg)) |
| 2507 | return 0; |
| 2508 | *reg_mask &= ~dreg; |
| 2509 | /* It's ld_imm64 or ld_abs or ld_ind. |
| 2510 | * For ld_imm64 no further tracking of precision |
| 2511 | * into parent is necessary |
| 2512 | */ |
| 2513 | if (mode == BPF_IND || mode == BPF_ABS) |
| 2514 | /* to be analyzed */ |
| 2515 | return -ENOTSUPP; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2516 | } |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
| 2520 | /* the scalar precision tracking algorithm: |
| 2521 | * . at the start all registers have precise=false. |
| 2522 | * . scalar ranges are tracked as normal through alu and jmp insns. |
| 2523 | * . once precise value of the scalar register is used in: |
| 2524 | * . ptr + scalar alu |
| 2525 | * . if (scalar cond K|scalar) |
| 2526 | * . helper_call(.., scalar, ...) where ARG_CONST is expected |
| 2527 | * backtrack through the verifier states and mark all registers and |
| 2528 | * stack slots with spilled constants that these scalar regisers |
| 2529 | * should be precise. |
| 2530 | * . during state pruning two registers (or spilled stack slots) |
| 2531 | * are equivalent if both are not precise. |
| 2532 | * |
| 2533 | * Note the verifier cannot simply walk register parentage chain, |
| 2534 | * since many different registers and stack slots could have been |
| 2535 | * used to compute single precise scalar. |
| 2536 | * |
| 2537 | * The approach of starting with precise=true for all registers and then |
| 2538 | * backtrack to mark a register as not precise when the verifier detects |
| 2539 | * that program doesn't care about specific value (e.g., when helper |
| 2540 | * takes register as ARG_ANYTHING parameter) is not safe. |
| 2541 | * |
| 2542 | * It's ok to walk single parentage chain of the verifier states. |
| 2543 | * It's possible that this backtracking will go all the way till 1st insn. |
| 2544 | * All other branches will be explored for needing precision later. |
| 2545 | * |
| 2546 | * The backtracking needs to deal with cases like: |
| 2547 | * R8=map_value(id=0,off=0,ks=4,vs=1952,imm=0) R9_w=map_value(id=0,off=40,ks=4,vs=1952,imm=0) |
| 2548 | * r9 -= r8 |
| 2549 | * r5 = r9 |
| 2550 | * if r5 > 0x79f goto pc+7 |
| 2551 | * R5_w=inv(id=0,umax_value=1951,var_off=(0x0; 0x7ff)) |
| 2552 | * r5 += 1 |
| 2553 | * ... |
| 2554 | * call bpf_perf_event_output#25 |
| 2555 | * where .arg5_type = ARG_CONST_SIZE_OR_ZERO |
| 2556 | * |
| 2557 | * and this case: |
| 2558 | * r6 = 1 |
| 2559 | * call foo // uses callee's r6 inside to compute r0 |
| 2560 | * r0 += r6 |
| 2561 | * if r0 == 0 goto |
| 2562 | * |
| 2563 | * to track above reg_mask/stack_mask needs to be independent for each frame. |
| 2564 | * |
| 2565 | * Also if parent's curframe > frame where backtracking started, |
| 2566 | * the verifier need to mark registers in both frames, otherwise callees |
| 2567 | * may incorrectly prune callers. This is similar to |
| 2568 | * commit 7640ead93924 ("bpf: verifier: make sure callees don't prune with caller differences") |
| 2569 | * |
| 2570 | * For now backtracking falls back into conservative marking. |
| 2571 | */ |
| 2572 | static void mark_all_scalars_precise(struct bpf_verifier_env *env, |
| 2573 | struct bpf_verifier_state *st) |
| 2574 | { |
| 2575 | struct bpf_func_state *func; |
| 2576 | struct bpf_reg_state *reg; |
| 2577 | int i, j; |
| 2578 | |
| 2579 | /* big hammer: mark all scalars precise in this path. |
| 2580 | * pop_stack may still get !precise scalars. |
| 2581 | */ |
| 2582 | for (; st; st = st->parent) |
| 2583 | for (i = 0; i <= st->curframe; i++) { |
| 2584 | func = st->frame[i]; |
| 2585 | for (j = 0; j < BPF_REG_FP; j++) { |
| 2586 | reg = &func->regs[j]; |
| 2587 | if (reg->type != SCALAR_VALUE) |
| 2588 | continue; |
| 2589 | reg->precise = true; |
| 2590 | } |
| 2591 | for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2592 | if (!is_spilled_reg(&func->stack[j])) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2593 | continue; |
| 2594 | reg = &func->stack[j].spilled_ptr; |
| 2595 | if (reg->type != SCALAR_VALUE) |
| 2596 | continue; |
| 2597 | reg->precise = true; |
| 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2602 | static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, |
| 2603 | int spi) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2604 | { |
| 2605 | struct bpf_verifier_state *st = env->cur_state; |
| 2606 | int first_idx = st->first_insn_idx; |
| 2607 | int last_idx = env->insn_idx; |
| 2608 | struct bpf_func_state *func; |
| 2609 | struct bpf_reg_state *reg; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2610 | u32 reg_mask = regno >= 0 ? 1u << regno : 0; |
| 2611 | u64 stack_mask = spi >= 0 ? 1ull << spi : 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2612 | bool skip_first = true; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2613 | bool new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2614 | int i, err; |
| 2615 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2616 | if (!env->bpf_capable) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2617 | return 0; |
| 2618 | |
| 2619 | func = st->frame[st->curframe]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2620 | if (regno >= 0) { |
| 2621 | reg = &func->regs[regno]; |
| 2622 | if (reg->type != SCALAR_VALUE) { |
| 2623 | WARN_ONCE(1, "backtracing misuse"); |
| 2624 | return -EFAULT; |
| 2625 | } |
| 2626 | if (!reg->precise) |
| 2627 | new_marks = true; |
| 2628 | else |
| 2629 | reg_mask = 0; |
| 2630 | reg->precise = true; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2631 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2632 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2633 | while (spi >= 0) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2634 | if (!is_spilled_reg(&func->stack[spi])) { |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2635 | stack_mask = 0; |
| 2636 | break; |
| 2637 | } |
| 2638 | reg = &func->stack[spi].spilled_ptr; |
| 2639 | if (reg->type != SCALAR_VALUE) { |
| 2640 | stack_mask = 0; |
| 2641 | break; |
| 2642 | } |
| 2643 | if (!reg->precise) |
| 2644 | new_marks = true; |
| 2645 | else |
| 2646 | stack_mask = 0; |
| 2647 | reg->precise = true; |
| 2648 | break; |
| 2649 | } |
| 2650 | |
| 2651 | if (!new_marks) |
| 2652 | return 0; |
| 2653 | if (!reg_mask && !stack_mask) |
| 2654 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2655 | for (;;) { |
| 2656 | DECLARE_BITMAP(mask, 64); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2657 | u32 history = st->jmp_history_cnt; |
| 2658 | |
| 2659 | if (env->log.level & BPF_LOG_LEVEL) |
| 2660 | verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx); |
| 2661 | for (i = last_idx;;) { |
| 2662 | if (skip_first) { |
| 2663 | err = 0; |
| 2664 | skip_first = false; |
| 2665 | } else { |
| 2666 | err = backtrack_insn(env, i, ®_mask, &stack_mask); |
| 2667 | } |
| 2668 | if (err == -ENOTSUPP) { |
| 2669 | mark_all_scalars_precise(env, st); |
| 2670 | return 0; |
| 2671 | } else if (err) { |
| 2672 | return err; |
| 2673 | } |
| 2674 | if (!reg_mask && !stack_mask) |
| 2675 | /* Found assignment(s) into tracked register in this state. |
| 2676 | * Since this state is already marked, just return. |
| 2677 | * Nothing to be tracked further in the parent state. |
| 2678 | */ |
| 2679 | return 0; |
| 2680 | if (i == first_idx) |
| 2681 | break; |
| 2682 | i = get_prev_insn_idx(st, i, &history); |
| 2683 | if (i >= env->prog->len) { |
| 2684 | /* This can happen if backtracking reached insn 0 |
| 2685 | * and there are still reg_mask or stack_mask |
| 2686 | * to backtrack. |
| 2687 | * It means the backtracking missed the spot where |
| 2688 | * particular register was initialized with a constant. |
| 2689 | */ |
| 2690 | verbose(env, "BUG backtracking idx %d\n", i); |
| 2691 | WARN_ONCE(1, "verifier backtracking bug"); |
| 2692 | return -EFAULT; |
| 2693 | } |
| 2694 | } |
| 2695 | st = st->parent; |
| 2696 | if (!st) |
| 2697 | break; |
| 2698 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2699 | new_marks = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2700 | func = st->frame[st->curframe]; |
| 2701 | bitmap_from_u64(mask, reg_mask); |
| 2702 | for_each_set_bit(i, mask, 32) { |
| 2703 | reg = &func->regs[i]; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2704 | if (reg->type != SCALAR_VALUE) { |
| 2705 | reg_mask &= ~(1u << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2706 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2707 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2708 | if (!reg->precise) |
| 2709 | new_marks = true; |
| 2710 | reg->precise = true; |
| 2711 | } |
| 2712 | |
| 2713 | bitmap_from_u64(mask, stack_mask); |
| 2714 | for_each_set_bit(i, mask, 64) { |
| 2715 | if (i >= func->allocated_stack / BPF_REG_SIZE) { |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2716 | /* the sequence of instructions: |
| 2717 | * 2: (bf) r3 = r10 |
| 2718 | * 3: (7b) *(u64 *)(r3 -8) = r0 |
| 2719 | * 4: (79) r4 = *(u64 *)(r10 -8) |
| 2720 | * doesn't contain jmps. It's backtracked |
| 2721 | * as a single block. |
| 2722 | * During backtracking insn 3 is not recognized as |
| 2723 | * stack access, so at the end of backtracking |
| 2724 | * stack slot fp-8 is still marked in stack_mask. |
| 2725 | * However the parent state may not have accessed |
| 2726 | * fp-8 and it's "unallocated" stack space. |
| 2727 | * In such case fallback to conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2728 | */ |
Alexei Starovoitov | 2339cd6 | 2019-09-03 15:16:17 -0700 | [diff] [blame] | 2729 | mark_all_scalars_precise(env, st); |
| 2730 | return 0; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2733 | if (!is_spilled_reg(&func->stack[i])) { |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2734 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2735 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2736 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2737 | reg = &func->stack[i].spilled_ptr; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2738 | if (reg->type != SCALAR_VALUE) { |
| 2739 | stack_mask &= ~(1ull << i); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2740 | continue; |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2741 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2742 | if (!reg->precise) |
| 2743 | new_marks = true; |
| 2744 | reg->precise = true; |
| 2745 | } |
| 2746 | if (env->log.level & BPF_LOG_LEVEL) { |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 2747 | verbose(env, "parent %s regs=%x stack=%llx marks:", |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2748 | new_marks ? "didn't have" : "already had", |
| 2749 | reg_mask, stack_mask); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 2750 | print_verifier_state(env, func, true); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2753 | if (!reg_mask && !stack_mask) |
| 2754 | break; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2755 | if (!new_marks) |
| 2756 | break; |
| 2757 | |
| 2758 | last_idx = st->last_insn_idx; |
| 2759 | first_idx = st->first_insn_idx; |
| 2760 | } |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 2764 | static int mark_chain_precision(struct bpf_verifier_env *env, int regno) |
| 2765 | { |
| 2766 | return __mark_chain_precision(env, regno, -1); |
| 2767 | } |
| 2768 | |
| 2769 | static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) |
| 2770 | { |
| 2771 | return __mark_chain_precision(env, -1, spi); |
| 2772 | } |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2773 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2774 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 2775 | { |
| 2776 | switch (type) { |
| 2777 | case PTR_TO_MAP_VALUE: |
| 2778 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 2779 | case PTR_TO_STACK: |
| 2780 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2781 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2782 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2783 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 2784 | case PTR_TO_FLOW_KEYS: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2785 | case CONST_PTR_TO_MAP: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 2786 | case PTR_TO_SOCKET: |
| 2787 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 2788 | case PTR_TO_SOCK_COMMON: |
| 2789 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 2790 | case PTR_TO_TCP_SOCK: |
| 2791 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 2792 | case PTR_TO_XDP_SOCK: |
Martin KaFai Lau | 65726b5 | 2020-01-08 16:34:54 -0800 | [diff] [blame] | 2793 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 2794 | case PTR_TO_BTF_ID_OR_NULL: |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 2795 | case PTR_TO_RDONLY_BUF: |
| 2796 | case PTR_TO_RDONLY_BUF_OR_NULL: |
| 2797 | case PTR_TO_RDWR_BUF: |
| 2798 | case PTR_TO_RDWR_BUF_OR_NULL: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 2799 | case PTR_TO_PERCPU_BTF_ID: |
Gilad Reti | 744ea4e | 2021-01-13 07:38:07 +0200 | [diff] [blame] | 2800 | case PTR_TO_MEM: |
| 2801 | case PTR_TO_MEM_OR_NULL: |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 2802 | case PTR_TO_FUNC: |
| 2803 | case PTR_TO_MAP_KEY: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2804 | return true; |
| 2805 | default: |
| 2806 | return false; |
| 2807 | } |
| 2808 | } |
| 2809 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2810 | /* Does this register contain a constant zero? */ |
| 2811 | static bool register_is_null(struct bpf_reg_state *reg) |
| 2812 | { |
| 2813 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 2814 | } |
| 2815 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2816 | static bool register_is_const(struct bpf_reg_state *reg) |
| 2817 | { |
| 2818 | return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off); |
| 2819 | } |
| 2820 | |
Yonghong Song | 5689d49 | 2020-10-08 18:12:38 -0700 | [diff] [blame] | 2821 | static bool __is_scalar_unbounded(struct bpf_reg_state *reg) |
| 2822 | { |
| 2823 | return tnum_is_unknown(reg->var_off) && |
| 2824 | reg->smin_value == S64_MIN && reg->smax_value == S64_MAX && |
| 2825 | reg->umin_value == 0 && reg->umax_value == U64_MAX && |
| 2826 | reg->s32_min_value == S32_MIN && reg->s32_max_value == S32_MAX && |
| 2827 | reg->u32_min_value == 0 && reg->u32_max_value == U32_MAX; |
| 2828 | } |
| 2829 | |
| 2830 | static bool register_is_bounded(struct bpf_reg_state *reg) |
| 2831 | { |
| 2832 | return reg->type == SCALAR_VALUE && !__is_scalar_unbounded(reg); |
| 2833 | } |
| 2834 | |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 2835 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 2836 | const struct bpf_reg_state *reg) |
| 2837 | { |
| 2838 | if (allow_ptr_leaks) |
| 2839 | return false; |
| 2840 | |
| 2841 | return reg->type != SCALAR_VALUE; |
| 2842 | } |
| 2843 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2844 | static void save_register_state(struct bpf_func_state *state, |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2845 | int spi, struct bpf_reg_state *reg, |
| 2846 | int size) |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2847 | { |
| 2848 | int i; |
| 2849 | |
| 2850 | state->stack[spi].spilled_ptr = *reg; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2851 | if (size == BPF_REG_SIZE) |
| 2852 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2853 | |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2854 | for (i = BPF_REG_SIZE; i > BPF_REG_SIZE - size; i--) |
| 2855 | state->stack[spi].slot_type[i - 1] = STACK_SPILL; |
| 2856 | |
| 2857 | /* size < 8 bytes spill */ |
| 2858 | for (; i; i--) |
| 2859 | scrub_spilled_slot(&state->stack[spi].slot_type[i - 1]); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2860 | } |
| 2861 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2862 | /* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2863 | * stack boundary and alignment are checked in check_mem_access() |
| 2864 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2865 | static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
| 2866 | /* stack frame we're writing to */ |
| 2867 | struct bpf_func_state *state, |
| 2868 | int off, int size, int value_regno, |
| 2869 | int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2870 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2871 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2872 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2873 | u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2874 | struct bpf_reg_state *reg = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2875 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 2876 | err = grow_stack_state(state, round_up(slot + 1, BPF_REG_SIZE)); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2877 | if (err) |
| 2878 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2879 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 2880 | * so it's aligned access and [off, off + size) are within stack limits |
| 2881 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2882 | if (!env->allow_ptr_leaks && |
| 2883 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 2884 | size != BPF_REG_SIZE) { |
| 2885 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 2886 | return -EACCES; |
| 2887 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2888 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2889 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2890 | if (value_regno >= 0) |
| 2891 | reg = &cur->regs[value_regno]; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 2892 | if (!env->bypass_spec_v4) { |
| 2893 | bool sanitize = reg && is_spillable_regtype(reg->type); |
| 2894 | |
| 2895 | for (i = 0; i < size; i++) { |
| 2896 | if (state->stack[spi].slot_type[i] == STACK_INVALID) { |
| 2897 | sanitize = true; |
| 2898 | break; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | if (sanitize) |
| 2903 | env->insn_aux_data[insn_idx].sanitize_stack_spill = true; |
| 2904 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2905 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 2906 | mark_stack_slot_scratched(env, spi); |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2907 | if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) && |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 2908 | !register_is_null(reg) && env->bpf_capable) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2909 | if (dst_reg != BPF_REG_FP) { |
| 2910 | /* The backtracking logic can only recognize explicit |
| 2911 | * stack slot address like [fp - 8]. Other spill of |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 2912 | * scalar via different register has to be conservative. |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2913 | * Backtrack from here and mark all registers as precise |
| 2914 | * that contributed into 'reg' being a constant. |
| 2915 | */ |
| 2916 | err = mark_chain_precision(env, value_regno); |
| 2917 | if (err) |
| 2918 | return err; |
| 2919 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2920 | save_register_state(state, spi, reg, size); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2921 | } else if (reg && is_spillable_regtype(reg->type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2922 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2923 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2924 | verbose_linfo(env, insn_idx, "; "); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2925 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2926 | return -EACCES; |
| 2927 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 2928 | if (state != cur && reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2929 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 2930 | return -EINVAL; |
| 2931 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2932 | save_register_state(state, spi, reg, size); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2933 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2934 | u8 type = STACK_MISC; |
| 2935 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 2936 | /* regular write of data into stack destroys any spilled ptr */ |
| 2937 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2938 | /* Mark slots as STACK_MISC if they belonged to spilled ptr. */ |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 2939 | if (is_spilled_reg(&state->stack[spi])) |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2940 | for (i = 0; i < BPF_REG_SIZE; i++) |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 2941 | scrub_spilled_slot(&state->stack[spi].slot_type[i]); |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2942 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2943 | /* only mark the slot as written if all 8 bytes were written |
| 2944 | * otherwise read propagation may incorrectly stop too soon |
| 2945 | * when stack slots are partially written. |
| 2946 | * This heuristic means that read propagation will be |
| 2947 | * conservative, since it will add reg_live_read marks |
| 2948 | * to stack slots all the way to first state when programs |
| 2949 | * writes+reads less than 8 bytes |
| 2950 | */ |
| 2951 | if (size == BPF_REG_SIZE) |
| 2952 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 2953 | |
| 2954 | /* when we zero initialize stack slots mark them as such */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2955 | if (reg && register_is_null(reg)) { |
| 2956 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2957 | err = mark_chain_precision(env, value_regno); |
| 2958 | if (err) |
| 2959 | return err; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2960 | type = STACK_ZERO; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 2961 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2962 | |
Jiong Wang | 0bae2d4 | 2018-12-15 03:34:40 -0500 | [diff] [blame] | 2963 | /* Mark slots affected by this stack write. */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2964 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2965 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 2966 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2967 | } |
| 2968 | return 0; |
| 2969 | } |
| 2970 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 2971 | /* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
| 2972 | * known to contain a variable offset. |
| 2973 | * This function checks whether the write is permitted and conservatively |
| 2974 | * tracks the effects of the write, considering that each stack slot in the |
| 2975 | * dynamic range is potentially written to. |
| 2976 | * |
| 2977 | * 'off' includes 'regno->off'. |
| 2978 | * 'value_regno' can be -1, meaning that an unknown value is being written to |
| 2979 | * the stack. |
| 2980 | * |
| 2981 | * Spilled pointers in range are not marked as written because we don't know |
| 2982 | * what's going to be actually written. This means that read propagation for |
| 2983 | * future reads cannot be terminated by this write. |
| 2984 | * |
| 2985 | * For privileged programs, uninitialized stack slots are considered |
| 2986 | * initialized by this write (even though we don't know exactly what offsets |
| 2987 | * are going to be written to). The idea is that we don't want the verifier to |
| 2988 | * reject future reads that access slots written to through variable offsets. |
| 2989 | */ |
| 2990 | static int check_stack_write_var_off(struct bpf_verifier_env *env, |
| 2991 | /* func where register points to */ |
| 2992 | struct bpf_func_state *state, |
| 2993 | int ptr_regno, int off, int size, |
| 2994 | int value_regno, int insn_idx) |
| 2995 | { |
| 2996 | struct bpf_func_state *cur; /* state of the current function */ |
| 2997 | int min_off, max_off; |
| 2998 | int i, err; |
| 2999 | struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; |
| 3000 | bool writing_zero = false; |
| 3001 | /* set if the fact that we're writing a zero is used to let any |
| 3002 | * stack slots remain STACK_ZERO |
| 3003 | */ |
| 3004 | bool zero_used = false; |
| 3005 | |
| 3006 | cur = env->cur_state->frame[env->cur_state->curframe]; |
| 3007 | ptr_reg = &cur->regs[ptr_regno]; |
| 3008 | min_off = ptr_reg->smin_value + off; |
| 3009 | max_off = ptr_reg->smax_value + off + size; |
| 3010 | if (value_regno >= 0) |
| 3011 | value_reg = &cur->regs[value_regno]; |
| 3012 | if (value_reg && register_is_null(value_reg)) |
| 3013 | writing_zero = true; |
| 3014 | |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 3015 | err = grow_stack_state(state, round_up(-min_off, BPF_REG_SIZE)); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3016 | if (err) |
| 3017 | return err; |
| 3018 | |
| 3019 | |
| 3020 | /* Variable offset writes destroy any spilled pointers in range. */ |
| 3021 | for (i = min_off; i < max_off; i++) { |
| 3022 | u8 new_type, *stype; |
| 3023 | int slot, spi; |
| 3024 | |
| 3025 | slot = -i - 1; |
| 3026 | spi = slot / BPF_REG_SIZE; |
| 3027 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 3028 | mark_stack_slot_scratched(env, spi); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3029 | |
| 3030 | if (!env->allow_ptr_leaks |
| 3031 | && *stype != NOT_INIT |
| 3032 | && *stype != SCALAR_VALUE) { |
| 3033 | /* Reject the write if there's are spilled pointers in |
| 3034 | * range. If we didn't reject here, the ptr status |
| 3035 | * would be erased below (even though not all slots are |
| 3036 | * actually overwritten), possibly opening the door to |
| 3037 | * leaks. |
| 3038 | */ |
| 3039 | verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", |
| 3040 | insn_idx, i); |
| 3041 | return -EINVAL; |
| 3042 | } |
| 3043 | |
| 3044 | /* Erase all spilled pointers. */ |
| 3045 | state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 3046 | |
| 3047 | /* Update the slot type. */ |
| 3048 | new_type = STACK_MISC; |
| 3049 | if (writing_zero && *stype == STACK_ZERO) { |
| 3050 | new_type = STACK_ZERO; |
| 3051 | zero_used = true; |
| 3052 | } |
| 3053 | /* If the slot is STACK_INVALID, we check whether it's OK to |
| 3054 | * pretend that it will be initialized by this write. The slot |
| 3055 | * might not actually be written to, and so if we mark it as |
| 3056 | * initialized future reads might leak uninitialized memory. |
| 3057 | * For privileged programs, we will accept such reads to slots |
| 3058 | * that may or may not be written because, if we're reject |
| 3059 | * them, the error would be too confusing. |
| 3060 | */ |
| 3061 | if (*stype == STACK_INVALID && !env->allow_uninit_stack) { |
| 3062 | verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", |
| 3063 | insn_idx, i); |
| 3064 | return -EINVAL; |
| 3065 | } |
| 3066 | *stype = new_type; |
| 3067 | } |
| 3068 | if (zero_used) { |
| 3069 | /* backtracking doesn't work for STACK_ZERO yet. */ |
| 3070 | err = mark_chain_precision(env, value_regno); |
| 3071 | if (err) |
| 3072 | return err; |
| 3073 | } |
| 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | /* When register 'dst_regno' is assigned some values from stack[min_off, |
| 3078 | * max_off), we set the register's type according to the types of the |
| 3079 | * respective stack slots. If all the stack values are known to be zeros, then |
| 3080 | * so is the destination reg. Otherwise, the register is considered to be |
| 3081 | * SCALAR. This function does not deal with register filling; the caller must |
| 3082 | * ensure that all spilled registers in the stack range have been marked as |
| 3083 | * read. |
| 3084 | */ |
| 3085 | static void mark_reg_stack_read(struct bpf_verifier_env *env, |
| 3086 | /* func where src register points to */ |
| 3087 | struct bpf_func_state *ptr_state, |
| 3088 | int min_off, int max_off, int dst_regno) |
| 3089 | { |
| 3090 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3091 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3092 | int i, slot, spi; |
| 3093 | u8 *stype; |
| 3094 | int zeros = 0; |
| 3095 | |
| 3096 | for (i = min_off; i < max_off; i++) { |
| 3097 | slot = -i - 1; |
| 3098 | spi = slot / BPF_REG_SIZE; |
| 3099 | stype = ptr_state->stack[spi].slot_type; |
| 3100 | if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) |
| 3101 | break; |
| 3102 | zeros++; |
| 3103 | } |
| 3104 | if (zeros == max_off - min_off) { |
| 3105 | /* any access_size read into register is zero extended, |
| 3106 | * so the whole register == const_zero |
| 3107 | */ |
| 3108 | __mark_reg_const_zero(&state->regs[dst_regno]); |
| 3109 | /* backtracking doesn't support STACK_ZERO yet, |
| 3110 | * so mark it precise here, so that later |
| 3111 | * backtracking can stop here. |
| 3112 | * Backtracking may not need this if this register |
| 3113 | * doesn't participate in pointer adjustment. |
| 3114 | * Forward propagation of precise flag is not |
| 3115 | * necessary either. This mark is only to stop |
| 3116 | * backtracking. Any register that contributed |
| 3117 | * to const 0 was marked precise before spill. |
| 3118 | */ |
| 3119 | state->regs[dst_regno].precise = true; |
| 3120 | } else { |
| 3121 | /* have read misc data from the stack */ |
| 3122 | mark_reg_unknown(env, state->regs, dst_regno); |
| 3123 | } |
| 3124 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 3125 | } |
| 3126 | |
| 3127 | /* Read the stack at 'off' and put the results into the register indicated by |
| 3128 | * 'dst_regno'. It handles reg filling if the addressed stack slot is a |
| 3129 | * spilled reg. |
| 3130 | * |
| 3131 | * 'dst_regno' can be -1, meaning that the read value is not going to a |
| 3132 | * register. |
| 3133 | * |
| 3134 | * The access is assumed to be within the current stack bounds. |
| 3135 | */ |
| 3136 | static int check_stack_read_fixed_off(struct bpf_verifier_env *env, |
| 3137 | /* func where src register points to */ |
| 3138 | struct bpf_func_state *reg_state, |
| 3139 | int off, int size, int dst_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3140 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3141 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3142 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3143 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3144 | struct bpf_reg_state *reg; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3145 | u8 *stype, type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3146 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3147 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3148 | reg = ®_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3149 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 3150 | if (is_spilled_reg(®_state->stack[spi])) { |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3151 | u8 spill_size = 1; |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3152 | |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3153 | for (i = BPF_REG_SIZE - 1; i > 0 && stype[i - 1] == STACK_SPILL; i--) |
| 3154 | spill_size++; |
| 3155 | |
| 3156 | if (size != BPF_REG_SIZE || spill_size != BPF_REG_SIZE) { |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3157 | if (reg->type != SCALAR_VALUE) { |
| 3158 | verbose_linfo(env, env->insn_idx, "; "); |
| 3159 | verbose(env, "invalid size of register fill\n"); |
| 3160 | return -EACCES; |
| 3161 | } |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3162 | |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3163 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3164 | if (dst_regno < 0) |
| 3165 | return 0; |
| 3166 | |
Martin KaFai Lau | f30d4968 | 2021-11-01 23:45:35 -0700 | [diff] [blame] | 3167 | if (!(off % BPF_REG_SIZE) && size == spill_size) { |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 3168 | /* The earlier check_reg_arg() has decided the |
| 3169 | * subreg_def for this insn. Save it first. |
| 3170 | */ |
| 3171 | s32 subreg_def = state->regs[dst_regno].subreg_def; |
| 3172 | |
| 3173 | state->regs[dst_regno] = *reg; |
| 3174 | state->regs[dst_regno].subreg_def = subreg_def; |
| 3175 | } else { |
| 3176 | for (i = 0; i < size; i++) { |
| 3177 | type = stype[(slot - i) % BPF_REG_SIZE]; |
| 3178 | if (type == STACK_SPILL) |
| 3179 | continue; |
| 3180 | if (type == STACK_MISC) |
| 3181 | continue; |
| 3182 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 3183 | off, i, size); |
| 3184 | return -EACCES; |
| 3185 | } |
| 3186 | mark_reg_unknown(env, state->regs, dst_regno); |
| 3187 | } |
| 3188 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3189 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3190 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3191 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3192 | if (dst_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3193 | /* restore register state from stack */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3194 | state->regs[dst_regno] = *reg; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 3195 | /* mark reg as written since spilled pointer state likely |
| 3196 | * has its liveness marks cleared by is_state_visited() |
| 3197 | * which resets stack/reg liveness for state transitions |
| 3198 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3199 | state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 3200 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3201 | /* If dst_regno==-1, the caller is asking us whether |
Jann Horn | 6e7e63c | 2020-04-17 02:00:06 +0200 | [diff] [blame] | 3202 | * it is acceptable to use this value as a SCALAR_VALUE |
| 3203 | * (e.g. for XADD). |
| 3204 | * We must not allow unprivileged callers to do that |
| 3205 | * with spilled pointers. |
| 3206 | */ |
| 3207 | verbose(env, "leaking pointer from stack off %d\n", |
| 3208 | off); |
| 3209 | return -EACCES; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3210 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3211 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3212 | } else { |
| 3213 | for (i = 0; i < size; i++) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3214 | type = stype[(slot - i) % BPF_REG_SIZE]; |
| 3215 | if (type == STACK_MISC) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3216 | continue; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3217 | if (type == STACK_ZERO) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3218 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 3219 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 3220 | off, i, size); |
| 3221 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3222 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3223 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3224 | if (dst_regno >= 0) |
| 3225 | mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3226 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 3227 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3230 | enum stack_access_src { |
| 3231 | ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
| 3232 | ACCESS_HELPER = 2, /* the access is performed by a helper */ |
| 3233 | }; |
| 3234 | |
| 3235 | static int check_stack_range_initialized(struct bpf_verifier_env *env, |
| 3236 | int regno, int off, int access_size, |
| 3237 | bool zero_size_allowed, |
| 3238 | enum stack_access_src type, |
| 3239 | struct bpf_call_arg_meta *meta); |
| 3240 | |
| 3241 | static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3242 | { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3243 | return cur_regs(env) + regno; |
| 3244 | } |
| 3245 | |
| 3246 | /* Read the stack at 'ptr_regno + off' and put the result into the register |
| 3247 | * 'dst_regno'. |
| 3248 | * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), |
| 3249 | * but not its variable offset. |
| 3250 | * 'size' is assumed to be <= reg size and the access is assumed to be aligned. |
| 3251 | * |
| 3252 | * As opposed to check_stack_read_fixed_off, this function doesn't deal with |
| 3253 | * filling registers (i.e. reads of spilled register cannot be detected when |
| 3254 | * the offset is not fixed). We conservatively mark 'dst_regno' as containing |
| 3255 | * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable |
| 3256 | * offset; for a fixed offset check_stack_read_fixed_off should be used |
| 3257 | * instead. |
| 3258 | */ |
| 3259 | static int check_stack_read_var_off(struct bpf_verifier_env *env, |
| 3260 | int ptr_regno, int off, int size, int dst_regno) |
| 3261 | { |
| 3262 | /* The state of the source register. */ |
| 3263 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3264 | struct bpf_func_state *ptr_state = func(env, reg); |
| 3265 | int err; |
| 3266 | int min_off, max_off; |
| 3267 | |
| 3268 | /* Note that we pass a NULL meta, so raw access will not be permitted. |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3269 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3270 | err = check_stack_range_initialized(env, ptr_regno, off, size, |
| 3271 | false, ACCESS_DIRECT, NULL); |
| 3272 | if (err) |
| 3273 | return err; |
| 3274 | |
| 3275 | min_off = reg->smin_value + off; |
| 3276 | max_off = reg->smax_value + off; |
| 3277 | mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); |
| 3278 | return 0; |
| 3279 | } |
| 3280 | |
| 3281 | /* check_stack_read dispatches to check_stack_read_fixed_off or |
| 3282 | * check_stack_read_var_off. |
| 3283 | * |
| 3284 | * The caller must ensure that the offset falls within the allocated stack |
| 3285 | * bounds. |
| 3286 | * |
| 3287 | * 'dst_regno' is a register which will receive the value from the stack. It |
| 3288 | * can be -1, meaning that the read value is not going to a register. |
| 3289 | */ |
| 3290 | static int check_stack_read(struct bpf_verifier_env *env, |
| 3291 | int ptr_regno, int off, int size, |
| 3292 | int dst_regno) |
| 3293 | { |
| 3294 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3295 | struct bpf_func_state *state = func(env, reg); |
| 3296 | int err; |
| 3297 | /* Some accesses are only permitted with a static offset. */ |
| 3298 | bool var_off = !tnum_is_const(reg->var_off); |
| 3299 | |
| 3300 | /* The offset is required to be static when reads don't go to a |
| 3301 | * register, in order to not leak pointers (see |
| 3302 | * check_stack_read_fixed_off). |
| 3303 | */ |
| 3304 | if (dst_regno < 0 && var_off) { |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3305 | char tn_buf[48]; |
| 3306 | |
| 3307 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3308 | verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3309 | tn_buf, off, size); |
| 3310 | return -EACCES; |
| 3311 | } |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3312 | /* Variable offset is prohibited for unprivileged mode for simplicity |
| 3313 | * since it requires corresponding support in Spectre masking for stack |
| 3314 | * ALU. See also retrieve_ptr_limit(). |
| 3315 | */ |
| 3316 | if (!env->bypass_spec_v1 && var_off) { |
| 3317 | char tn_buf[48]; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3318 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3319 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3320 | verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", |
| 3321 | ptr_regno, tn_buf); |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3322 | return -EACCES; |
| 3323 | } |
| 3324 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3325 | if (!var_off) { |
| 3326 | off += reg->var_off.value; |
| 3327 | err = check_stack_read_fixed_off(env, state, off, size, |
| 3328 | dst_regno); |
| 3329 | } else { |
| 3330 | /* Variable offset stack reads need more conservative handling |
| 3331 | * than fixed offset ones. Note that dst_regno >= 0 on this |
| 3332 | * branch. |
| 3333 | */ |
| 3334 | err = check_stack_read_var_off(env, ptr_regno, off, size, |
| 3335 | dst_regno); |
| 3336 | } |
| 3337 | return err; |
| 3338 | } |
| 3339 | |
| 3340 | |
| 3341 | /* check_stack_write dispatches to check_stack_write_fixed_off or |
| 3342 | * check_stack_write_var_off. |
| 3343 | * |
| 3344 | * 'ptr_regno' is the register used as a pointer into the stack. |
| 3345 | * 'off' includes 'ptr_regno->off', but not its variable offset (if any). |
| 3346 | * 'value_regno' is the register whose value we're writing to the stack. It can |
| 3347 | * be -1, meaning that we're not writing from a register. |
| 3348 | * |
| 3349 | * The caller must ensure that the offset falls within the maximum stack size. |
| 3350 | */ |
| 3351 | static int check_stack_write(struct bpf_verifier_env *env, |
| 3352 | int ptr_regno, int off, int size, |
| 3353 | int value_regno, int insn_idx) |
| 3354 | { |
| 3355 | struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 3356 | struct bpf_func_state *state = func(env, reg); |
| 3357 | int err; |
| 3358 | |
| 3359 | if (tnum_is_const(reg->var_off)) { |
| 3360 | off += reg->var_off.value; |
| 3361 | err = check_stack_write_fixed_off(env, state, off, size, |
| 3362 | value_regno, insn_idx); |
| 3363 | } else { |
| 3364 | /* Variable offset stack reads need more conservative handling |
| 3365 | * than fixed offset ones. |
| 3366 | */ |
| 3367 | err = check_stack_write_var_off(env, state, |
| 3368 | ptr_regno, off, size, |
| 3369 | value_regno, insn_idx); |
| 3370 | } |
| 3371 | return err; |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 3372 | } |
| 3373 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 3374 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| 3375 | int off, int size, enum bpf_access_type type) |
| 3376 | { |
| 3377 | struct bpf_reg_state *regs = cur_regs(env); |
| 3378 | struct bpf_map *map = regs[regno].map_ptr; |
| 3379 | u32 cap = bpf_map_flags_to_cap(map); |
| 3380 | |
| 3381 | if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { |
| 3382 | verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", |
| 3383 | map->value_size, off, size); |
| 3384 | return -EACCES; |
| 3385 | } |
| 3386 | |
| 3387 | if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) { |
| 3388 | verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n", |
| 3389 | map->value_size, off, size); |
| 3390 | return -EACCES; |
| 3391 | } |
| 3392 | |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3396 | /* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ |
| 3397 | static int __check_mem_access(struct bpf_verifier_env *env, int regno, |
| 3398 | int off, int size, u32 mem_size, |
| 3399 | bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3400 | { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3401 | bool size_ok = size > 0 || (size == 0 && zero_size_allowed); |
| 3402 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3403 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3404 | if (off >= 0 && size_ok && (u64)off + size <= mem_size) |
| 3405 | return 0; |
| 3406 | |
| 3407 | reg = &cur_regs(env)[regno]; |
| 3408 | switch (reg->type) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3409 | case PTR_TO_MAP_KEY: |
| 3410 | verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n", |
| 3411 | mem_size, off, size); |
| 3412 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3413 | case PTR_TO_MAP_VALUE: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3414 | verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3415 | mem_size, off, size); |
| 3416 | break; |
| 3417 | case PTR_TO_PACKET: |
| 3418 | case PTR_TO_PACKET_META: |
| 3419 | case PTR_TO_PACKET_END: |
| 3420 | verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 3421 | off, size, regno, reg->id, off, mem_size); |
| 3422 | break; |
| 3423 | case PTR_TO_MEM: |
| 3424 | default: |
| 3425 | verbose(env, "invalid access to memory, mem_size=%u off=%d size=%d\n", |
| 3426 | mem_size, off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3427 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3428 | |
| 3429 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3430 | } |
| 3431 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3432 | /* check read/write into a memory region with possible variable offset */ |
| 3433 | static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, |
| 3434 | int off, int size, u32 mem_size, |
| 3435 | bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3436 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3437 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3438 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3439 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 3440 | int err; |
| 3441 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3442 | /* We may have adjusted the register pointing to memory region, so we |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3443 | * need to try adding each of min_value and max_value to off |
| 3444 | * to make sure our theoretical access will be safe. |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 3445 | * |
| 3446 | * The minimum value is only important with signed |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3447 | * comparisons where we can't assume the floor of a |
| 3448 | * value is 0. If we are using signed variables for our |
| 3449 | * index'es we need to make sure that whatever we use |
| 3450 | * will have a set floor within our range. |
| 3451 | */ |
Daniel Borkmann | b7137c4 | 2019-01-03 00:58:33 +0100 | [diff] [blame] | 3452 | if (reg->smin_value < 0 && |
| 3453 | (reg->smin_value == S64_MIN || |
| 3454 | (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) || |
| 3455 | reg->smin_value + off < 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3456 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3457 | regno); |
| 3458 | return -EACCES; |
| 3459 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3460 | err = __check_mem_access(env, regno, reg->smin_value + off, size, |
| 3461 | mem_size, zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3462 | if (err) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3463 | verbose(env, "R%d min value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3464 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3465 | return err; |
| 3466 | } |
| 3467 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3468 | /* If we haven't set a max value then we need to bail since we can't be |
| 3469 | * sure we won't do bad things. |
| 3470 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3471 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3472 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3473 | verbose(env, "R%d unbounded memory access, make sure to bounds check any such access\n", |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3474 | regno); |
| 3475 | return -EACCES; |
| 3476 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3477 | err = __check_mem_access(env, regno, reg->umax_value + off, size, |
| 3478 | mem_size, zero_size_allowed); |
| 3479 | if (err) { |
| 3480 | verbose(env, "R%d max value is outside of the allowed memory range\n", |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3481 | regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3482 | return err; |
| 3483 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3484 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3485 | return 0; |
| 3486 | } |
| 3487 | |
| 3488 | /* check read/write into a map element with possible variable offset */ |
| 3489 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
| 3490 | int off, int size, bool zero_size_allowed) |
| 3491 | { |
| 3492 | struct bpf_verifier_state *vstate = env->cur_state; |
| 3493 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 3494 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 3495 | struct bpf_map *map = reg->map_ptr; |
| 3496 | int err; |
| 3497 | |
| 3498 | err = check_mem_region_access(env, regno, off, size, map->value_size, |
| 3499 | zero_size_allowed); |
| 3500 | if (err) |
| 3501 | return err; |
| 3502 | |
| 3503 | if (map_value_has_spin_lock(map)) { |
| 3504 | u32 lock = map->spin_lock_off; |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 3505 | |
| 3506 | /* if any part of struct bpf_spin_lock can be touched by |
| 3507 | * load/store reject this program. |
| 3508 | * To check that [x1, x2) overlaps with [y1, y2) |
| 3509 | * it is sufficient to check x1 < y2 && y1 < x2. |
| 3510 | */ |
| 3511 | if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) && |
| 3512 | lock < reg->umax_value + off + size) { |
| 3513 | verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n"); |
| 3514 | return -EACCES; |
| 3515 | } |
| 3516 | } |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 3517 | if (map_value_has_timer(map)) { |
| 3518 | u32 t = map->timer_off; |
| 3519 | |
| 3520 | if (reg->smin_value + off < t + sizeof(struct bpf_timer) && |
| 3521 | t < reg->umax_value + off + size) { |
| 3522 | verbose(env, "bpf_timer cannot be accessed directly by load/store\n"); |
| 3523 | return -EACCES; |
| 3524 | } |
| 3525 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3526 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 3527 | } |
| 3528 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3529 | #define MAX_PACKET_OFF 0xffff |
| 3530 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3531 | static enum bpf_prog_type resolve_prog_type(struct bpf_prog *prog) |
| 3532 | { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 3533 | return prog->aux->dst_prog ? prog->aux->dst_prog->type : prog->type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3534 | } |
| 3535 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3536 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3537 | const struct bpf_call_arg_meta *meta, |
| 3538 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3539 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 3540 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
| 3541 | |
| 3542 | switch (prog_type) { |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3543 | /* Program types only with direct read access go here! */ |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3544 | case BPF_PROG_TYPE_LWT_IN: |
| 3545 | case BPF_PROG_TYPE_LWT_OUT: |
Mathieu Xhonneux | 004d4b2 | 2018-05-20 14:58:16 +0100 | [diff] [blame] | 3546 | case BPF_PROG_TYPE_LWT_SEG6LOCAL: |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 3547 | case BPF_PROG_TYPE_SK_REUSEPORT: |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3548 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
Daniel Borkmann | d5563d3 | 2018-10-24 22:05:46 +0200 | [diff] [blame] | 3549 | case BPF_PROG_TYPE_CGROUP_SKB: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3550 | if (t == BPF_WRITE) |
| 3551 | return false; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 3552 | fallthrough; |
Daniel Borkmann | 5d66fa7 | 2018-10-24 22:05:45 +0200 | [diff] [blame] | 3553 | |
| 3554 | /* Program types with direct read + write access go here! */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3555 | case BPF_PROG_TYPE_SCHED_CLS: |
| 3556 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3557 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 3558 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 3559 | case BPF_PROG_TYPE_SK_SKB: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 3560 | case BPF_PROG_TYPE_SK_MSG: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3561 | if (meta) |
| 3562 | return meta->pkt_access; |
| 3563 | |
| 3564 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3565 | return true; |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 3566 | |
| 3567 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
| 3568 | if (t == BPF_WRITE) |
| 3569 | env->seen_direct_write = true; |
| 3570 | |
| 3571 | return true; |
| 3572 | |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 3573 | default: |
| 3574 | return false; |
| 3575 | } |
| 3576 | } |
| 3577 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3578 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 3579 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3580 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3581 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3582 | struct bpf_reg_state *reg = ®s[regno]; |
| 3583 | int err; |
| 3584 | |
| 3585 | /* We may have added a variable offset to the packet pointer; but any |
| 3586 | * reg->range we have comes after that. We are only checking the fixed |
| 3587 | * offset. |
| 3588 | */ |
| 3589 | |
| 3590 | /* We don't allow negative numbers, because we aren't tracking enough |
| 3591 | * detail to prove they're safe. |
| 3592 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3593 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3594 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3595 | regno); |
| 3596 | return -EACCES; |
| 3597 | } |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 3598 | |
| 3599 | err = reg->range < 0 ? -EINVAL : |
| 3600 | __check_mem_access(env, regno, off, size, reg->range, |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3601 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3602 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3603 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3604 | return err; |
| 3605 | } |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3606 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3607 | /* __check_mem_access has made sure "off + size - 1" is within u16. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3608 | * reg->umax_value can't be bigger than MAX_PACKET_OFF which is 0xffff, |
| 3609 | * otherwise find_good_pkt_pointers would have refused to set range info |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 3610 | * that __check_mem_access would have rejected this pkt access. |
Jiong Wang | e647815 | 2018-11-08 04:08:42 -0500 | [diff] [blame] | 3611 | * Therefore, "off + reg->umax_value + size - 1" won't overflow u32. |
| 3612 | */ |
| 3613 | env->prog->aux->max_pkt_offset = |
| 3614 | max_t(u32, env->prog->aux->max_pkt_offset, |
| 3615 | off + reg->umax_value + size - 1); |
| 3616 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3617 | return err; |
| 3618 | } |
| 3619 | |
| 3620 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3621 | static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3622 | enum bpf_access_type t, enum bpf_reg_type *reg_type, |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3623 | struct btf **btf, u32 *btf_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3624 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3625 | struct bpf_insn_access_aux info = { |
| 3626 | .reg_type = *reg_type, |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3627 | .log = &env->log, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3628 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3629 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 3630 | if (env->ops->is_valid_access && |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 3631 | env->ops->is_valid_access(off, size, t, env->prog, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 3632 | /* A non zero info.ctx_field_size indicates that this field is a |
| 3633 | * candidate for later verifier transformation to load the whole |
| 3634 | * field and then apply a mask when accessed with a narrower |
| 3635 | * access than actual ctx access size. A zero info.ctx_field_size |
| 3636 | * will only allow for whole field access and rejects any other |
| 3637 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3638 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 3639 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3640 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3641 | if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) { |
| 3642 | *btf = info.btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3643 | *btf_id = info.btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3644 | } else { |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 3645 | env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 3646 | } |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 3647 | /* remember the offset of last byte accessed in ctx */ |
| 3648 | if (env->prog->aux->max_ctx_offset < off + size) |
| 3649 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3650 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 3651 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3652 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3653 | verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3654 | return -EACCES; |
| 3655 | } |
| 3656 | |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3657 | static int check_flow_keys_access(struct bpf_verifier_env *env, int off, |
| 3658 | int size) |
| 3659 | { |
| 3660 | if (size < 0 || off < 0 || |
| 3661 | (u64)off + size > sizeof(struct bpf_flow_keys)) { |
| 3662 | verbose(env, "invalid access to flow keys off=%d size=%d\n", |
| 3663 | off, size); |
| 3664 | return -EACCES; |
| 3665 | } |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3669 | static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, |
| 3670 | u32 regno, int off, int size, |
| 3671 | enum bpf_access_type t) |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3672 | { |
| 3673 | struct bpf_reg_state *regs = cur_regs(env); |
| 3674 | struct bpf_reg_state *reg = ®s[regno]; |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3675 | struct bpf_insn_access_aux info = {}; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3676 | bool valid; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3677 | |
| 3678 | if (reg->smin_value < 0) { |
| 3679 | verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 3680 | regno); |
| 3681 | return -EACCES; |
| 3682 | } |
| 3683 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3684 | switch (reg->type) { |
| 3685 | case PTR_TO_SOCK_COMMON: |
| 3686 | valid = bpf_sock_common_is_valid_access(off, size, t, &info); |
| 3687 | break; |
| 3688 | case PTR_TO_SOCKET: |
| 3689 | valid = bpf_sock_is_valid_access(off, size, t, &info); |
| 3690 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3691 | case PTR_TO_TCP_SOCK: |
| 3692 | valid = bpf_tcp_sock_is_valid_access(off, size, t, &info); |
| 3693 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3694 | case PTR_TO_XDP_SOCK: |
| 3695 | valid = bpf_xdp_sock_is_valid_access(off, size, t, &info); |
| 3696 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3697 | default: |
| 3698 | valid = false; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3699 | } |
| 3700 | |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 3701 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3702 | if (valid) { |
| 3703 | env->insn_aux_data[insn_idx].ctx_field_size = |
| 3704 | info.ctx_field_size; |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
| 3708 | verbose(env, "R%d invalid %s access off=%d size=%d\n", |
| 3709 | regno, reg_type_str[reg->type], off, size); |
| 3710 | |
| 3711 | return -EACCES; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3712 | } |
| 3713 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3714 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 3715 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3716 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3717 | } |
| 3718 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3719 | static bool is_ctx_reg(struct bpf_verifier_env *env, int regno) |
| 3720 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3721 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3722 | |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3723 | return reg->type == PTR_TO_CTX; |
| 3724 | } |
| 3725 | |
| 3726 | static bool is_sk_reg(struct bpf_verifier_env *env, int regno) |
| 3727 | { |
| 3728 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3729 | |
| 3730 | return type_is_sk_pointer(reg->type); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 3731 | } |
| 3732 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3733 | static bool is_pkt_reg(struct bpf_verifier_env *env, int regno) |
| 3734 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 3735 | const struct bpf_reg_state *reg = reg_state(env, regno); |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3736 | |
| 3737 | return type_is_pkt_pointer(reg->type); |
| 3738 | } |
| 3739 | |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 3740 | static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno) |
| 3741 | { |
| 3742 | const struct bpf_reg_state *reg = reg_state(env, regno); |
| 3743 | |
| 3744 | /* Separate to is_ctx_reg() since we still want to allow BPF_ST here. */ |
| 3745 | return reg->type == PTR_TO_FLOW_KEYS; |
| 3746 | } |
| 3747 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3748 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 3749 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3750 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3751 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3752 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3753 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3754 | |
| 3755 | /* Byte size accesses are always allowed. */ |
| 3756 | if (!strict || size == 1) |
| 3757 | return 0; |
| 3758 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 3759 | /* For platforms that do not have a Kconfig enabling |
| 3760 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 3761 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 3762 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 3763 | * to this code only in strict mode where we want to emulate |
| 3764 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 3765 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3766 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 3767 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3768 | |
| 3769 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 3770 | if (!tnum_is_aligned(reg_off, size)) { |
| 3771 | char tn_buf[48]; |
| 3772 | |
| 3773 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3774 | verbose(env, |
| 3775 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3776 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3777 | return -EACCES; |
| 3778 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3779 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3780 | return 0; |
| 3781 | } |
| 3782 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3783 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 3784 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3785 | const char *pointer_desc, |
| 3786 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3787 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3788 | struct tnum reg_off; |
| 3789 | |
| 3790 | /* Byte size accesses are always allowed. */ |
| 3791 | if (!strict || size == 1) |
| 3792 | return 0; |
| 3793 | |
| 3794 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 3795 | if (!tnum_is_aligned(reg_off, size)) { |
| 3796 | char tn_buf[48]; |
| 3797 | |
| 3798 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3799 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3800 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3801 | return -EACCES; |
| 3802 | } |
| 3803 | |
| 3804 | return 0; |
| 3805 | } |
| 3806 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 3807 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3808 | const struct bpf_reg_state *reg, int off, |
| 3809 | int size, bool strict_alignment_once) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3810 | { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 3811 | bool strict = env->strict_alignment || strict_alignment_once; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3812 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 3813 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3814 | switch (reg->type) { |
| 3815 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3816 | case PTR_TO_PACKET_META: |
| 3817 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 3818 | * right in front, treat it the very same way. |
| 3819 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3820 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 3821 | case PTR_TO_FLOW_KEYS: |
| 3822 | pointer_desc = "flow keys "; |
| 3823 | break; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3824 | case PTR_TO_MAP_KEY: |
| 3825 | pointer_desc = "key "; |
| 3826 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3827 | case PTR_TO_MAP_VALUE: |
| 3828 | pointer_desc = "value "; |
| 3829 | break; |
| 3830 | case PTR_TO_CTX: |
| 3831 | pointer_desc = "context "; |
| 3832 | break; |
| 3833 | case PTR_TO_STACK: |
| 3834 | pointer_desc = "stack "; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 3835 | /* The stack spill tracking logic in check_stack_write_fixed_off() |
| 3836 | * and check_stack_read_fixed_off() relies on stack accesses being |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 3837 | * aligned. |
| 3838 | */ |
| 3839 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3840 | break; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 3841 | case PTR_TO_SOCKET: |
| 3842 | pointer_desc = "sock "; |
| 3843 | break; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 3844 | case PTR_TO_SOCK_COMMON: |
| 3845 | pointer_desc = "sock_common "; |
| 3846 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 3847 | case PTR_TO_TCP_SOCK: |
| 3848 | pointer_desc = "tcp_sock "; |
| 3849 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 3850 | case PTR_TO_XDP_SOCK: |
| 3851 | pointer_desc = "xdp_sock "; |
| 3852 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3853 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3854 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3855 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3856 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 3857 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 3858 | } |
| 3859 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3860 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 3861 | const struct bpf_func_state *func, |
| 3862 | int off) |
| 3863 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3864 | u16 stack = env->subprog_info[func->subprogno].stack_depth; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3865 | |
| 3866 | if (stack >= -off) |
| 3867 | return 0; |
| 3868 | |
| 3869 | /* update known max for given subprogram */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3870 | env->subprog_info[func->subprogno].stack_depth = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3871 | return 0; |
| 3872 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3873 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3874 | /* starting from main bpf function walk all instructions of the function |
| 3875 | * and recursively walk all callees that given function can call. |
| 3876 | * Ignore jump and exit insns. |
| 3877 | * Since recursion is prevented by check_cfg() this algorithm |
| 3878 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 3879 | */ |
| 3880 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 3881 | { |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3882 | int depth = 0, frame = 0, idx = 0, i = 0, subprog_end; |
| 3883 | struct bpf_subprog_info *subprog = env->subprog_info; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3884 | struct bpf_insn *insn = env->prog->insnsi; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3885 | bool tail_call_reachable = false; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3886 | int ret_insn[MAX_CALL_FRAMES]; |
| 3887 | int ret_prog[MAX_CALL_FRAMES]; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3888 | int j; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3889 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3890 | process_func: |
Maciej Fijalkowski | 7f6e431 | 2020-09-16 23:10:07 +0200 | [diff] [blame] | 3891 | /* protect against potential stack overflow that might happen when |
| 3892 | * bpf2bpf calls get combined with tailcalls. Limit the caller's stack |
| 3893 | * depth for such case down to 256 so that the worst case scenario |
| 3894 | * would result in 8k stack size (32 which is tailcall limit * 256 = |
| 3895 | * 8k). |
| 3896 | * |
| 3897 | * To get the idea what might happen, see an example: |
| 3898 | * func1 -> sub rsp, 128 |
| 3899 | * subfunc1 -> sub rsp, 256 |
| 3900 | * tailcall1 -> add rsp, 256 |
| 3901 | * func2 -> sub rsp, 192 (total stack size = 128 + 192 = 320) |
| 3902 | * subfunc2 -> sub rsp, 64 |
| 3903 | * subfunc22 -> sub rsp, 128 |
| 3904 | * tailcall2 -> add rsp, 128 |
| 3905 | * func3 -> sub rsp, 32 (total stack size 128 + 192 + 64 + 32 = 416) |
| 3906 | * |
| 3907 | * tailcall will unwind the current stack frame but it will not get rid |
| 3908 | * of caller's stack as shown on the example above. |
| 3909 | */ |
| 3910 | if (idx && subprog[idx].has_tail_call && depth >= 256) { |
| 3911 | verbose(env, |
| 3912 | "tail_calls are not allowed when call stack of previous frames is %d bytes. Too large\n", |
| 3913 | depth); |
| 3914 | return -EACCES; |
| 3915 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3916 | /* round up to 32-bytes, since this is granularity |
| 3917 | * of interpreter stack size |
| 3918 | */ |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3919 | depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3920 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3921 | verbose(env, "combined stack size of %d calls is %d. Too large\n", |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3922 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3923 | return -EACCES; |
| 3924 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3925 | continue_func: |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 3926 | subprog_end = subprog[idx + 1].start; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3927 | for (; i < subprog_end; i++) { |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3928 | int next_insn; |
| 3929 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 3930 | if (!bpf_pseudo_call(insn + i) && !bpf_pseudo_func(insn + i)) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3931 | continue; |
| 3932 | /* remember insn and function to return to */ |
| 3933 | ret_insn[frame] = i + 1; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3934 | ret_prog[frame] = idx; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3935 | |
| 3936 | /* find the callee */ |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3937 | next_insn = i + insn[i].imm + 1; |
| 3938 | idx = find_subprog(env, next_insn); |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3939 | if (idx < 0) { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3940 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3941 | next_insn); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3942 | return -EFAULT; |
| 3943 | } |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 3944 | if (subprog[idx].is_async_cb) { |
| 3945 | if (subprog[idx].has_tail_call) { |
| 3946 | verbose(env, "verifier bug. subprog has tail_call and async cb\n"); |
| 3947 | return -EFAULT; |
| 3948 | } |
| 3949 | /* async callbacks don't increase bpf prog stack size */ |
| 3950 | continue; |
| 3951 | } |
| 3952 | i = next_insn; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3953 | |
| 3954 | if (subprog[idx].has_tail_call) |
| 3955 | tail_call_reachable = true; |
| 3956 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3957 | frame++; |
| 3958 | if (frame >= MAX_CALL_FRAMES) { |
Paul Chaignon | 927cb78 | 2019-03-20 13:58:27 +0100 | [diff] [blame] | 3959 | verbose(env, "the call stack of %d frames is too deep !\n", |
| 3960 | frame); |
| 3961 | return -E2BIG; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3962 | } |
| 3963 | goto process_func; |
| 3964 | } |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3965 | /* if tail call got detected across bpf2bpf calls then mark each of the |
| 3966 | * currently present subprog frames as tail call reachable subprogs; |
| 3967 | * this info will be utilized by JIT so that we will be preserving the |
| 3968 | * tail call counter throughout bpf2bpf calls combined with tailcalls |
| 3969 | */ |
| 3970 | if (tail_call_reachable) |
| 3971 | for (j = 0; j < frame; j++) |
| 3972 | subprog[ret_prog[j]].tail_call_reachable = true; |
Daniel Borkmann | 5dd0a6b | 2021-07-12 22:57:35 +0200 | [diff] [blame] | 3973 | if (subprog[0].tail_call_reachable) |
| 3974 | env->prog->aux->tail_call_reachable = true; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 3975 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3976 | /* end of for() loop means the last insn of the 'subprog' |
| 3977 | * was reached. Doesn't matter whether it was JA or EXIT |
| 3978 | */ |
| 3979 | if (frame == 0) |
| 3980 | return 0; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3981 | depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32); |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3982 | frame--; |
| 3983 | i = ret_insn[frame]; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 3984 | idx = ret_prog[frame]; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 3985 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3986 | } |
| 3987 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 3988 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 3989 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 3990 | const struct bpf_insn *insn, int idx) |
| 3991 | { |
| 3992 | int start = idx + insn->imm + 1, subprog; |
| 3993 | |
| 3994 | subprog = find_subprog(env, start); |
| 3995 | if (subprog < 0) { |
| 3996 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 3997 | start); |
| 3998 | return -EFAULT; |
| 3999 | } |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 4000 | return env->subprog_info[subprog].stack_depth; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 4001 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 4002 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 4003 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 4004 | int check_ctx_reg(struct bpf_verifier_env *env, |
| 4005 | const struct bpf_reg_state *reg, int regno) |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 4006 | { |
| 4007 | /* Access to ctx or passing it to a helper is only allowed in |
| 4008 | * its original, unmodified form. |
| 4009 | */ |
| 4010 | |
| 4011 | if (reg->off) { |
| 4012 | verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n", |
| 4013 | regno, reg->off); |
| 4014 | return -EACCES; |
| 4015 | } |
| 4016 | |
| 4017 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4018 | char tn_buf[48]; |
| 4019 | |
| 4020 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4021 | verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf); |
| 4022 | return -EACCES; |
| 4023 | } |
| 4024 | |
| 4025 | return 0; |
| 4026 | } |
| 4027 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4028 | static int __check_buffer_access(struct bpf_verifier_env *env, |
| 4029 | const char *buf_info, |
| 4030 | const struct bpf_reg_state *reg, |
| 4031 | int regno, int off, int size) |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4032 | { |
| 4033 | if (off < 0) { |
| 4034 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 4035 | "R%d invalid %s buffer access: off=%d, size=%d\n", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4036 | regno, buf_info, off, size); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4037 | return -EACCES; |
| 4038 | } |
| 4039 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4040 | char tn_buf[48]; |
| 4041 | |
| 4042 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4043 | verbose(env, |
Yonghong Song | 4fc00b7 | 2020-07-28 15:18:01 -0700 | [diff] [blame] | 4044 | "R%d invalid variable buffer offset: off=%d, var_off=%s\n", |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4045 | regno, off, tn_buf); |
| 4046 | return -EACCES; |
| 4047 | } |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4048 | |
| 4049 | return 0; |
| 4050 | } |
| 4051 | |
| 4052 | static int check_tp_buffer_access(struct bpf_verifier_env *env, |
| 4053 | const struct bpf_reg_state *reg, |
| 4054 | int regno, int off, int size) |
| 4055 | { |
| 4056 | int err; |
| 4057 | |
| 4058 | err = __check_buffer_access(env, "tracepoint", reg, regno, off, size); |
| 4059 | if (err) |
| 4060 | return err; |
| 4061 | |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4062 | if (off + size > env->prog->aux->max_tp_access) |
| 4063 | env->prog->aux->max_tp_access = off + size; |
| 4064 | |
| 4065 | return 0; |
| 4066 | } |
| 4067 | |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4068 | static int check_buffer_access(struct bpf_verifier_env *env, |
| 4069 | const struct bpf_reg_state *reg, |
| 4070 | int regno, int off, int size, |
| 4071 | bool zero_size_allowed, |
| 4072 | const char *buf_info, |
| 4073 | u32 *max_access) |
| 4074 | { |
| 4075 | int err; |
| 4076 | |
| 4077 | err = __check_buffer_access(env, buf_info, reg, regno, off, size); |
| 4078 | if (err) |
| 4079 | return err; |
| 4080 | |
| 4081 | if (off + size > *max_access) |
| 4082 | *max_access = off + size; |
| 4083 | |
| 4084 | return 0; |
| 4085 | } |
| 4086 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 4087 | /* BPF architecture zero extends alu32 ops into 64-bit registesr */ |
| 4088 | static void zext_32_to_64(struct bpf_reg_state *reg) |
| 4089 | { |
| 4090 | reg->var_off = tnum_subreg(reg->var_off); |
| 4091 | __reg_assign_32_into_64(reg); |
| 4092 | } |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4093 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4094 | /* truncate register to smaller size (in bytes) |
| 4095 | * must be called with size < BPF_REG_SIZE |
| 4096 | */ |
| 4097 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 4098 | { |
| 4099 | u64 mask; |
| 4100 | |
| 4101 | /* clear high bits in bit representation */ |
| 4102 | reg->var_off = tnum_cast(reg->var_off, size); |
| 4103 | |
| 4104 | /* fix arithmetic bounds */ |
| 4105 | mask = ((u64)1 << (size * 8)) - 1; |
| 4106 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 4107 | reg->umin_value &= mask; |
| 4108 | reg->umax_value &= mask; |
| 4109 | } else { |
| 4110 | reg->umin_value = 0; |
| 4111 | reg->umax_value = mask; |
| 4112 | } |
| 4113 | reg->smin_value = reg->umin_value; |
| 4114 | reg->smax_value = reg->umax_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 4115 | |
| 4116 | /* If size is smaller than 32bit register the 32bit register |
| 4117 | * values are also truncated so we push 64-bit bounds into |
| 4118 | * 32-bit bounds. Above were truncated < 32-bits already. |
| 4119 | */ |
| 4120 | if (size >= 4) |
| 4121 | return; |
| 4122 | __reg_combine_64_into_32(reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4125 | static bool bpf_map_is_rdonly(const struct bpf_map *map) |
| 4126 | { |
Daniel Borkmann | 353050b | 2021-11-09 18:48:08 +0000 | [diff] [blame] | 4127 | /* A map is considered read-only if the following condition are true: |
| 4128 | * |
| 4129 | * 1) BPF program side cannot change any of the map content. The |
| 4130 | * BPF_F_RDONLY_PROG flag is throughout the lifetime of a map |
| 4131 | * and was set at map creation time. |
| 4132 | * 2) The map value(s) have been initialized from user space by a |
| 4133 | * loader and then "frozen", such that no new map update/delete |
| 4134 | * operations from syscall side are possible for the rest of |
| 4135 | * the map's lifetime from that point onwards. |
| 4136 | * 3) Any parallel/pending map update/delete operations from syscall |
| 4137 | * side have been completed. Only after that point, it's safe to |
| 4138 | * assume that map value(s) are immutable. |
| 4139 | */ |
| 4140 | return (map->map_flags & BPF_F_RDONLY_PROG) && |
| 4141 | READ_ONCE(map->frozen) && |
| 4142 | !bpf_map_write_active(map); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | static int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val) |
| 4146 | { |
| 4147 | void *ptr; |
| 4148 | u64 addr; |
| 4149 | int err; |
| 4150 | |
| 4151 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 4152 | if (err) |
| 4153 | return err; |
Andrii Nakryiko | 2dedd7d | 2019-10-11 10:20:53 -0700 | [diff] [blame] | 4154 | ptr = (void *)(long)addr + off; |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4155 | |
| 4156 | switch (size) { |
| 4157 | case sizeof(u8): |
| 4158 | *val = (u64)*(u8 *)ptr; |
| 4159 | break; |
| 4160 | case sizeof(u16): |
| 4161 | *val = (u64)*(u16 *)ptr; |
| 4162 | break; |
| 4163 | case sizeof(u32): |
| 4164 | *val = (u64)*(u32 *)ptr; |
| 4165 | break; |
| 4166 | case sizeof(u64): |
| 4167 | *val = *(u64 *)ptr; |
| 4168 | break; |
| 4169 | default: |
| 4170 | return -EINVAL; |
| 4171 | } |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4175 | static int check_ptr_to_btf_access(struct bpf_verifier_env *env, |
| 4176 | struct bpf_reg_state *regs, |
| 4177 | int regno, int off, int size, |
| 4178 | enum bpf_access_type atype, |
| 4179 | int value_regno) |
| 4180 | { |
| 4181 | struct bpf_reg_state *reg = regs + regno; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4182 | const struct btf_type *t = btf_type_by_id(reg->btf, reg->btf_id); |
| 4183 | const char *tname = btf_name_by_offset(reg->btf, t->name_off); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4184 | u32 btf_id; |
| 4185 | int ret; |
| 4186 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4187 | if (off < 0) { |
| 4188 | verbose(env, |
| 4189 | "R%d is ptr_%s invalid negative access: off=%d\n", |
| 4190 | regno, tname, off); |
| 4191 | return -EACCES; |
| 4192 | } |
| 4193 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 4194 | char tn_buf[48]; |
| 4195 | |
| 4196 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4197 | verbose(env, |
| 4198 | "R%d is ptr_%s invalid variable offset: off=%d, var_off=%s\n", |
| 4199 | regno, tname, off, tn_buf); |
| 4200 | return -EACCES; |
| 4201 | } |
| 4202 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4203 | if (env->ops->btf_struct_access) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4204 | ret = env->ops->btf_struct_access(&env->log, reg->btf, t, |
| 4205 | off, size, atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4206 | } else { |
| 4207 | if (atype != BPF_READ) { |
| 4208 | verbose(env, "only read is supported\n"); |
| 4209 | return -EACCES; |
| 4210 | } |
| 4211 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4212 | ret = btf_struct_access(&env->log, reg->btf, t, off, size, |
| 4213 | atype, &btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4214 | } |
| 4215 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4216 | if (ret < 0) |
| 4217 | return ret; |
| 4218 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4219 | if (atype == BPF_READ && value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4220 | mark_btf_ld_reg(env, regs, value_regno, ret, reg->btf, btf_id); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 4221 | |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4222 | return 0; |
| 4223 | } |
| 4224 | |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4225 | static int check_ptr_to_map_access(struct bpf_verifier_env *env, |
| 4226 | struct bpf_reg_state *regs, |
| 4227 | int regno, int off, int size, |
| 4228 | enum bpf_access_type atype, |
| 4229 | int value_regno) |
| 4230 | { |
| 4231 | struct bpf_reg_state *reg = regs + regno; |
| 4232 | struct bpf_map *map = reg->map_ptr; |
| 4233 | const struct btf_type *t; |
| 4234 | const char *tname; |
| 4235 | u32 btf_id; |
| 4236 | int ret; |
| 4237 | |
| 4238 | if (!btf_vmlinux) { |
| 4239 | verbose(env, "map_ptr access not supported without CONFIG_DEBUG_INFO_BTF\n"); |
| 4240 | return -ENOTSUPP; |
| 4241 | } |
| 4242 | |
| 4243 | if (!map->ops->map_btf_id || !*map->ops->map_btf_id) { |
| 4244 | verbose(env, "map_ptr access not supported for map type %d\n", |
| 4245 | map->map_type); |
| 4246 | return -ENOTSUPP; |
| 4247 | } |
| 4248 | |
| 4249 | t = btf_type_by_id(btf_vmlinux, *map->ops->map_btf_id); |
| 4250 | tname = btf_name_by_offset(btf_vmlinux, t->name_off); |
| 4251 | |
| 4252 | if (!env->allow_ptr_to_map_access) { |
| 4253 | verbose(env, |
| 4254 | "%s access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN\n", |
| 4255 | tname); |
| 4256 | return -EPERM; |
| 4257 | } |
| 4258 | |
| 4259 | if (off < 0) { |
| 4260 | verbose(env, "R%d is %s invalid negative access: off=%d\n", |
| 4261 | regno, tname, off); |
| 4262 | return -EACCES; |
| 4263 | } |
| 4264 | |
| 4265 | if (atype != BPF_READ) { |
| 4266 | verbose(env, "only read from %s is supported\n", tname); |
| 4267 | return -EACCES; |
| 4268 | } |
| 4269 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4270 | ret = btf_struct_access(&env->log, btf_vmlinux, t, off, size, atype, &btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4271 | if (ret < 0) |
| 4272 | return ret; |
| 4273 | |
| 4274 | if (value_regno >= 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4275 | mark_btf_ld_reg(env, regs, value_regno, ret, btf_vmlinux, btf_id); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4276 | |
| 4277 | return 0; |
| 4278 | } |
| 4279 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4280 | /* Check that the stack access at the given offset is within bounds. The |
| 4281 | * maximum valid offset is -1. |
| 4282 | * |
| 4283 | * The minimum valid offset is -MAX_BPF_STACK for writes, and |
| 4284 | * -state->allocated_stack for reads. |
| 4285 | */ |
| 4286 | static int check_stack_slot_within_bounds(int off, |
| 4287 | struct bpf_func_state *state, |
| 4288 | enum bpf_access_type t) |
| 4289 | { |
| 4290 | int min_valid_off; |
| 4291 | |
| 4292 | if (t == BPF_WRITE) |
| 4293 | min_valid_off = -MAX_BPF_STACK; |
| 4294 | else |
| 4295 | min_valid_off = -state->allocated_stack; |
| 4296 | |
| 4297 | if (off < min_valid_off || off > -1) |
| 4298 | return -EACCES; |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | /* Check that the stack access at 'regno + off' falls within the maximum stack |
| 4303 | * bounds. |
| 4304 | * |
| 4305 | * 'off' includes `regno->offset`, but not its dynamic part (if any). |
| 4306 | */ |
| 4307 | static int check_stack_access_within_bounds( |
| 4308 | struct bpf_verifier_env *env, |
| 4309 | int regno, int off, int access_size, |
| 4310 | enum stack_access_src src, enum bpf_access_type type) |
| 4311 | { |
| 4312 | struct bpf_reg_state *regs = cur_regs(env); |
| 4313 | struct bpf_reg_state *reg = regs + regno; |
| 4314 | struct bpf_func_state *state = func(env, reg); |
| 4315 | int min_off, max_off; |
| 4316 | int err; |
| 4317 | char *err_extra; |
| 4318 | |
| 4319 | if (src == ACCESS_HELPER) |
| 4320 | /* We don't know if helpers are reading or writing (or both). */ |
| 4321 | err_extra = " indirect access to"; |
| 4322 | else if (type == BPF_READ) |
| 4323 | err_extra = " read from"; |
| 4324 | else |
| 4325 | err_extra = " write to"; |
| 4326 | |
| 4327 | if (tnum_is_const(reg->var_off)) { |
| 4328 | min_off = reg->var_off.value + off; |
| 4329 | if (access_size > 0) |
| 4330 | max_off = min_off + access_size - 1; |
| 4331 | else |
| 4332 | max_off = min_off; |
| 4333 | } else { |
| 4334 | if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 4335 | reg->smin_value <= -BPF_MAX_VAR_OFF) { |
| 4336 | verbose(env, "invalid unbounded variable-offset%s stack R%d\n", |
| 4337 | err_extra, regno); |
| 4338 | return -EACCES; |
| 4339 | } |
| 4340 | min_off = reg->smin_value + off; |
| 4341 | if (access_size > 0) |
| 4342 | max_off = reg->smax_value + off + access_size - 1; |
| 4343 | else |
| 4344 | max_off = min_off; |
| 4345 | } |
| 4346 | |
| 4347 | err = check_stack_slot_within_bounds(min_off, state, type); |
| 4348 | if (!err) |
| 4349 | err = check_stack_slot_within_bounds(max_off, state, type); |
| 4350 | |
| 4351 | if (err) { |
| 4352 | if (tnum_is_const(reg->var_off)) { |
| 4353 | verbose(env, "invalid%s stack R%d off=%d size=%d\n", |
| 4354 | err_extra, regno, off, access_size); |
| 4355 | } else { |
| 4356 | char tn_buf[48]; |
| 4357 | |
| 4358 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 4359 | verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", |
| 4360 | err_extra, regno, tn_buf, access_size); |
| 4361 | } |
| 4362 | } |
| 4363 | return err; |
| 4364 | } |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4365 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4366 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 4367 | * if t==write, value_regno is a register which value is stored into memory |
| 4368 | * if t==read, value_regno is a register which will receive the value from memory |
| 4369 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 4370 | * if t==read && value_regno==-1, don't care what we read from memory |
| 4371 | */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4372 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, |
| 4373 | int off, int bpf_size, enum bpf_access_type t, |
| 4374 | int value_regno, bool strict_alignment_once) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4375 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4376 | struct bpf_reg_state *regs = cur_regs(env); |
| 4377 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4378 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4379 | int size, err = 0; |
| 4380 | |
| 4381 | size = bpf_size_to_bytes(bpf_size); |
| 4382 | if (size < 0) |
| 4383 | return size; |
| 4384 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4385 | /* alignment checks will add in reg->off themselves */ |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4386 | err = check_ptr_alignment(env, reg, off, size, strict_alignment_once); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4387 | if (err) |
| 4388 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4389 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4390 | /* for access checks, reg->off is just part of off */ |
| 4391 | off += reg->off; |
| 4392 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 4393 | if (reg->type == PTR_TO_MAP_KEY) { |
| 4394 | if (t == BPF_WRITE) { |
| 4395 | verbose(env, "write to change key R%d not allowed\n", regno); |
| 4396 | return -EACCES; |
| 4397 | } |
| 4398 | |
| 4399 | err = check_mem_region_access(env, regno, off, size, |
| 4400 | reg->map_ptr->key_size, false); |
| 4401 | if (err) |
| 4402 | return err; |
| 4403 | if (value_regno >= 0) |
| 4404 | mark_reg_unknown(env, regs, value_regno); |
| 4405 | } else if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4406 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4407 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4408 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4409 | return -EACCES; |
| 4410 | } |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4411 | err = check_map_access_type(env, regno, off, size, t); |
| 4412 | if (err) |
| 4413 | return err; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4414 | err = check_map_access(env, regno, off, size, false); |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4415 | if (!err && t == BPF_READ && value_regno >= 0) { |
| 4416 | struct bpf_map *map = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4417 | |
Andrii Nakryiko | a23740e | 2019-10-09 13:14:57 -0700 | [diff] [blame] | 4418 | /* if map is read-only, track its contents as scalars */ |
| 4419 | if (tnum_is_const(reg->var_off) && |
| 4420 | bpf_map_is_rdonly(map) && |
| 4421 | map->ops->map_direct_value_addr) { |
| 4422 | int map_off = off + reg->var_off.value; |
| 4423 | u64 val = 0; |
| 4424 | |
| 4425 | err = bpf_map_direct_read(map, map_off, size, |
| 4426 | &val); |
| 4427 | if (err) |
| 4428 | return err; |
| 4429 | |
| 4430 | regs[value_regno].type = SCALAR_VALUE; |
| 4431 | __mark_reg_known(®s[value_regno], val); |
| 4432 | } else { |
| 4433 | mark_reg_unknown(env, regs, value_regno); |
| 4434 | } |
| 4435 | } |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4436 | } else if (reg->type == PTR_TO_MEM) { |
| 4437 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4438 | is_pointer_value(env, value_regno)) { |
| 4439 | verbose(env, "R%d leaks addr into mem\n", value_regno); |
| 4440 | return -EACCES; |
| 4441 | } |
| 4442 | err = check_mem_region_access(env, regno, off, size, |
| 4443 | reg->mem_size, false); |
| 4444 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4445 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4446 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4447 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4448 | struct btf *btf = NULL; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4449 | u32 btf_id = 0; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 4450 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4451 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4452 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4453 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4454 | return -EACCES; |
| 4455 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4456 | |
Daniel Borkmann | 58990d1 | 2018-06-07 17:40:03 +0200 | [diff] [blame] | 4457 | err = check_ctx_reg(env, reg, regno); |
| 4458 | if (err < 0) |
| 4459 | return err; |
| 4460 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4461 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type, &btf, &btf_id); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4462 | if (err) |
| 4463 | verbose_linfo(env, insn_idx, "; "); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4464 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4465 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4466 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 4467 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4468 | */ |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4469 | if (reg_type == SCALAR_VALUE) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4470 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4471 | } else { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4472 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4473 | value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4474 | if (reg_type_may_be_null(reg_type)) |
| 4475 | regs[value_regno].id = ++env->id_gen; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4476 | /* A load of ctx field could have different |
| 4477 | * actual load size with the one encoded in the |
| 4478 | * insn. When the dst is PTR, it is for sure not |
| 4479 | * a sub-register. |
| 4480 | */ |
| 4481 | regs[value_regno].subreg_def = DEF_NOT_SUBREG; |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 4482 | if (reg_type == PTR_TO_BTF_ID || |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4483 | reg_type == PTR_TO_BTF_ID_OR_NULL) { |
| 4484 | regs[value_regno].btf = btf; |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4485 | regs[value_regno].btf_id = btf_id; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 4486 | } |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4487 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4488 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4489 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4490 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4491 | } else if (reg->type == PTR_TO_STACK) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4492 | /* Basic bounds checks. */ |
| 4493 | err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); |
Daniel Borkmann | e4298d2 | 2019-01-03 00:58:31 +0100 | [diff] [blame] | 4494 | if (err) |
| 4495 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 4496 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4497 | state = func(env, reg); |
| 4498 | err = update_stack_depth(env, state, off); |
| 4499 | if (err) |
| 4500 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 4501 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4502 | if (t == BPF_READ) |
| 4503 | err = check_stack_read(env, regno, off, size, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4504 | value_regno); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4505 | else |
| 4506 | err = check_stack_write(env, regno, off, size, |
| 4507 | value_regno, insn_idx); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4508 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 4509 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4510 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4511 | return -EACCES; |
| 4512 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 4513 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4514 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4515 | verbose(env, "R%d leaks addr into packet\n", |
| 4516 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 4517 | return -EACCES; |
| 4518 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4519 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4520 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4521 | mark_reg_unknown(env, regs, value_regno); |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 4522 | } else if (reg->type == PTR_TO_FLOW_KEYS) { |
| 4523 | if (t == BPF_WRITE && value_regno >= 0 && |
| 4524 | is_pointer_value(env, value_regno)) { |
| 4525 | verbose(env, "R%d leaks addr into flow keys\n", |
| 4526 | value_regno); |
| 4527 | return -EACCES; |
| 4528 | } |
| 4529 | |
| 4530 | err = check_flow_keys_access(env, off, size); |
| 4531 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4532 | mark_reg_unknown(env, regs, value_regno); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4533 | } else if (type_is_sk_pointer(reg->type)) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4534 | if (t == BPF_WRITE) { |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4535 | verbose(env, "R%d cannot write into %s\n", |
| 4536 | regno, reg_type_str[reg->type]); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4537 | return -EACCES; |
| 4538 | } |
Martin KaFai Lau | 5f45664 | 2019-02-08 22:25:54 -0800 | [diff] [blame] | 4539 | err = check_sock_access(env, insn_idx, regno, off, size, t); |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 4540 | if (!err && value_regno >= 0) |
| 4541 | mark_reg_unknown(env, regs, value_regno); |
Matt Mullins | 9df1c28 | 2019-04-26 11:49:47 -0700 | [diff] [blame] | 4542 | } else if (reg->type == PTR_TO_TP_BUFFER) { |
| 4543 | err = check_tp_buffer_access(env, reg, regno, off, size); |
| 4544 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4545 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 9e15db6 | 2019-10-15 20:25:00 -0700 | [diff] [blame] | 4546 | } else if (reg->type == PTR_TO_BTF_ID) { |
| 4547 | err = check_ptr_to_btf_access(env, regs, regno, off, size, t, |
| 4548 | value_regno); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 4549 | } else if (reg->type == CONST_PTR_TO_MAP) { |
| 4550 | err = check_ptr_to_map_access(env, regs, regno, off, size, t, |
| 4551 | value_regno); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4552 | } else if (reg->type == PTR_TO_RDONLY_BUF) { |
| 4553 | if (t == BPF_WRITE) { |
| 4554 | verbose(env, "R%d cannot write into %s\n", |
| 4555 | regno, reg_type_str[reg->type]); |
| 4556 | return -EACCES; |
| 4557 | } |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 4558 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 4559 | "rdonly", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4560 | &env->prog->aux->max_rdonly_access); |
| 4561 | if (!err && value_regno >= 0) |
| 4562 | mark_reg_unknown(env, regs, value_regno); |
| 4563 | } else if (reg->type == PTR_TO_RDWR_BUF) { |
Colin Ian King | f6dfbe31 | 2020-07-27 18:54:11 +0100 | [diff] [blame] | 4564 | err = check_buffer_access(env, reg, regno, off, size, false, |
| 4565 | "rdwr", |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4566 | &env->prog->aux->max_rdwr_access); |
| 4567 | if (!err && t == BPF_READ && value_regno >= 0) |
| 4568 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4569 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4570 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 4571 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4572 | return -EACCES; |
| 4573 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4574 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4575 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4576 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4577 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 4578 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4579 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4580 | return err; |
| 4581 | } |
| 4582 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4583 | static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4584 | { |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4585 | int load_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4586 | int err; |
| 4587 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4588 | switch (insn->imm) { |
| 4589 | case BPF_ADD: |
| 4590 | case BPF_ADD | BPF_FETCH: |
Brendan Jackman | 981f94c | 2021-01-14 18:17:49 +0000 | [diff] [blame] | 4591 | case BPF_AND: |
| 4592 | case BPF_AND | BPF_FETCH: |
| 4593 | case BPF_OR: |
| 4594 | case BPF_OR | BPF_FETCH: |
| 4595 | case BPF_XOR: |
| 4596 | case BPF_XOR | BPF_FETCH: |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4597 | case BPF_XCHG: |
| 4598 | case BPF_CMPXCHG: |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4599 | break; |
| 4600 | default: |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4601 | verbose(env, "BPF_ATOMIC uses invalid atomic opcode %02x\n", insn->imm); |
| 4602 | return -EINVAL; |
| 4603 | } |
| 4604 | |
| 4605 | if (BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) { |
| 4606 | verbose(env, "invalid atomic operand size\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4607 | return -EINVAL; |
| 4608 | } |
| 4609 | |
| 4610 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4611 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4612 | if (err) |
| 4613 | return err; |
| 4614 | |
| 4615 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4616 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4617 | if (err) |
| 4618 | return err; |
| 4619 | |
Brendan Jackman | 5ffa255 | 2021-01-14 18:17:47 +0000 | [diff] [blame] | 4620 | if (insn->imm == BPF_CMPXCHG) { |
| 4621 | /* Check comparison of R0 with memory location */ |
| 4622 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 4623 | if (err) |
| 4624 | return err; |
| 4625 | } |
| 4626 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 4627 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4628 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 4629 | return -EACCES; |
| 4630 | } |
| 4631 | |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 4632 | if (is_ctx_reg(env, insn->dst_reg) || |
Daniel Borkmann | 4b5defd | 2018-10-21 02:09:25 +0200 | [diff] [blame] | 4633 | is_pkt_reg(env, insn->dst_reg) || |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 4634 | is_flow_key_reg(env, insn->dst_reg) || |
| 4635 | is_sk_reg(env, insn->dst_reg)) { |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4636 | verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 4637 | insn->dst_reg, |
| 4638 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 4639 | return -EACCES; |
| 4640 | } |
| 4641 | |
Brendan Jackman | 37086bf | 2021-02-02 13:50:02 +0000 | [diff] [blame] | 4642 | if (insn->imm & BPF_FETCH) { |
| 4643 | if (insn->imm == BPF_CMPXCHG) |
| 4644 | load_reg = BPF_REG_0; |
| 4645 | else |
| 4646 | load_reg = insn->src_reg; |
| 4647 | |
| 4648 | /* check and record load of old value */ |
| 4649 | err = check_reg_arg(env, load_reg, DST_OP); |
| 4650 | if (err) |
| 4651 | return err; |
| 4652 | } else { |
| 4653 | /* This instruction accesses a memory location but doesn't |
| 4654 | * actually load it into a register. |
| 4655 | */ |
| 4656 | load_reg = -1; |
| 4657 | } |
| 4658 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4659 | /* check whether we can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4660 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Brendan Jackman | 37086bf | 2021-02-02 13:50:02 +0000 | [diff] [blame] | 4661 | BPF_SIZE(insn->code), BPF_READ, load_reg, true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4662 | if (err) |
| 4663 | return err; |
| 4664 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 4665 | /* check whether we can write into the same memory */ |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4666 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 4667 | BPF_SIZE(insn->code), BPF_WRITE, -1, true); |
| 4668 | if (err) |
| 4669 | return err; |
| 4670 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 4671 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4672 | } |
| 4673 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4674 | /* When register 'regno' is used to read the stack (either directly or through |
| 4675 | * a helper function) make sure that it's within stack boundary and, depending |
| 4676 | * on the access type, that all elements of the stack are initialized. |
| 4677 | * |
| 4678 | * 'off' includes 'regno->off', but not its dynamic part (if any). |
| 4679 | * |
| 4680 | * All registers that have been spilled on the stack in the slots within the |
| 4681 | * read offsets are marked as read. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4682 | */ |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4683 | static int check_stack_range_initialized( |
| 4684 | struct bpf_verifier_env *env, int regno, int off, |
| 4685 | int access_size, bool zero_size_allowed, |
| 4686 | enum stack_access_src type, struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4687 | { |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 4688 | struct bpf_reg_state *reg = reg_state(env, regno); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4689 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 4690 | int err, min_off, max_off, i, j, slot, spi; |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4691 | char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; |
| 4692 | enum bpf_access_type bounds_check_type; |
| 4693 | /* Some accesses can write anything into the stack, others are |
| 4694 | * read-only. |
| 4695 | */ |
| 4696 | bool clobber = false; |
| 4697 | |
| 4698 | if (access_size == 0 && !zero_size_allowed) { |
| 4699 | verbose(env, "invalid zero-sized read\n"); |
| 4700 | return -EACCES; |
| 4701 | } |
| 4702 | |
| 4703 | if (type == ACCESS_HELPER) { |
| 4704 | /* The bounds checks for writes are more permissive than for |
| 4705 | * reads. However, if raw_mode is not set, we'll do extra |
| 4706 | * checks below. |
| 4707 | */ |
| 4708 | bounds_check_type = BPF_WRITE; |
| 4709 | clobber = true; |
| 4710 | } else { |
| 4711 | bounds_check_type = BPF_READ; |
| 4712 | } |
| 4713 | err = check_stack_access_within_bounds(env, regno, off, access_size, |
| 4714 | type, bounds_check_type); |
| 4715 | if (err) |
| 4716 | return err; |
| 4717 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4718 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4719 | if (tnum_is_const(reg->var_off)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4720 | min_off = max_off = reg->var_off.value + off; |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4721 | } else { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4722 | /* Variable offset is prohibited for unprivileged mode for |
| 4723 | * simplicity since it requires corresponding support in |
| 4724 | * Spectre masking for stack ALU. |
| 4725 | * See also retrieve_ptr_limit(). |
| 4726 | */ |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 4727 | if (!env->bypass_spec_v1) { |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4728 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4729 | |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4730 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4731 | verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
| 4732 | regno, err_extra, tn_buf); |
Andrey Ignatov | 088ec26 | 2019-04-03 23:22:39 -0700 | [diff] [blame] | 4733 | return -EACCES; |
| 4734 | } |
Andrey Ignatov | f2bcd05 | 2019-04-03 23:22:37 -0700 | [diff] [blame] | 4735 | /* Only initialized buffer on stack is allowed to be accessed |
| 4736 | * with variable offset. With uninitialized buffer it's hard to |
| 4737 | * guarantee that whole memory is marked as initialized on |
| 4738 | * helper return since specific bounds are unknown what may |
| 4739 | * cause uninitialized stack leaking. |
| 4740 | */ |
| 4741 | if (meta && meta->raw_mode) |
| 4742 | meta = NULL; |
| 4743 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4744 | min_off = reg->smin_value + off; |
| 4745 | max_off = reg->smax_value + off; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4746 | } |
| 4747 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 4748 | if (meta && meta->raw_mode) { |
| 4749 | meta->access_size = access_size; |
| 4750 | meta->regno = regno; |
| 4751 | return 0; |
| 4752 | } |
| 4753 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4754 | for (i = min_off; i < max_off + access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4755 | u8 *stype; |
| 4756 | |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4757 | slot = -i - 1; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4758 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4759 | if (state->allocated_stack <= slot) |
| 4760 | goto err; |
| 4761 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 4762 | if (*stype == STACK_MISC) |
| 4763 | goto mark; |
| 4764 | if (*stype == STACK_ZERO) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4765 | if (clobber) { |
| 4766 | /* helper can write anything into the stack */ |
| 4767 | *stype = STACK_MISC; |
| 4768 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4769 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4770 | } |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 4771 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 4772 | if (is_spilled_reg(&state->stack[spi]) && |
Yonghong Song | 1d68f22 | 2020-05-09 10:59:15 -0700 | [diff] [blame] | 4773 | state->stack[spi].spilled_ptr.type == PTR_TO_BTF_ID) |
| 4774 | goto mark; |
| 4775 | |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 4776 | if (is_spilled_reg(&state->stack[spi]) && |
Yonghong Song | cd17d38 | 2020-12-09 17:33:49 -0800 | [diff] [blame] | 4777 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
| 4778 | env->allow_ptr_leaks)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4779 | if (clobber) { |
| 4780 | __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
| 4781 | for (j = 0; j < BPF_REG_SIZE; j++) |
Martin KaFai Lau | 354e8f1 | 2021-09-21 17:49:41 -0700 | [diff] [blame] | 4782 | scrub_spilled_slot(&state->stack[spi].slot_type[j]); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4783 | } |
Alexei Starovoitov | f7cf25b | 2019-06-15 12:12:17 -0700 | [diff] [blame] | 4784 | goto mark; |
| 4785 | } |
| 4786 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4787 | err: |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4788 | if (tnum_is_const(reg->var_off)) { |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4789 | verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
| 4790 | err_extra, regno, min_off, i - min_off, access_size); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4791 | } else { |
| 4792 | char tn_buf[48]; |
| 4793 | |
| 4794 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4795 | verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
| 4796 | err_extra, regno, tn_buf, i - min_off, access_size); |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4797 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4798 | return -EACCES; |
| 4799 | mark: |
| 4800 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 4801 | * the whole slot to be marked as 'read' |
| 4802 | */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 4803 | mark_reg_read(env, &state->stack[spi].spilled_ptr, |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 4804 | state->stack[spi].spilled_ptr.parent, |
| 4805 | REG_LIVE_READ64); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4806 | } |
Andrey Ignatov | 2011fcc | 2019-03-28 18:01:57 -0700 | [diff] [blame] | 4807 | return update_stack_depth(env, state, min_off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4808 | } |
| 4809 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4810 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 4811 | int access_size, bool zero_size_allowed, |
| 4812 | struct bpf_call_arg_meta *meta) |
| 4813 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4814 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4815 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4816 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4817 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4818 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4819 | return check_packet_access(env, regno, reg->off, access_size, |
| 4820 | zero_size_allowed); |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 4821 | case PTR_TO_MAP_KEY: |
| 4822 | return check_mem_region_access(env, regno, reg->off, access_size, |
| 4823 | reg->map_ptr->key_size, false); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4824 | case PTR_TO_MAP_VALUE: |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 4825 | if (check_map_access_type(env, regno, reg->off, access_size, |
| 4826 | meta && meta->raw_mode ? BPF_WRITE : |
| 4827 | BPF_READ)) |
| 4828 | return -EACCES; |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 4829 | return check_map_access(env, regno, reg->off, access_size, |
| 4830 | zero_size_allowed); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 4831 | case PTR_TO_MEM: |
| 4832 | return check_mem_region_access(env, regno, reg->off, |
| 4833 | access_size, reg->mem_size, |
| 4834 | zero_size_allowed); |
Yonghong Song | afbf21d | 2020-07-23 11:41:11 -0700 | [diff] [blame] | 4835 | case PTR_TO_RDONLY_BUF: |
| 4836 | if (meta && meta->raw_mode) |
| 4837 | return -EACCES; |
| 4838 | return check_buffer_access(env, reg, regno, reg->off, |
| 4839 | access_size, zero_size_allowed, |
| 4840 | "rdonly", |
| 4841 | &env->prog->aux->max_rdonly_access); |
| 4842 | case PTR_TO_RDWR_BUF: |
| 4843 | return check_buffer_access(env, reg, regno, reg->off, |
| 4844 | access_size, zero_size_allowed, |
| 4845 | "rdwr", |
| 4846 | &env->prog->aux->max_rdwr_access); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 4847 | case PTR_TO_STACK: |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 4848 | return check_stack_range_initialized( |
| 4849 | env, |
| 4850 | regno, reg->off, access_size, |
| 4851 | zero_size_allowed, ACCESS_HELPER, meta); |
Lorenz Bauer | 0d004c02 | 2020-09-21 13:12:18 +0100 | [diff] [blame] | 4852 | default: /* scalar_value or invalid ptr */ |
| 4853 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 4854 | if (zero_size_allowed && access_size == 0 && |
| 4855 | register_is_null(reg)) |
| 4856 | return 0; |
| 4857 | |
| 4858 | verbose(env, "R%d type=%s expected=%s\n", regno, |
| 4859 | reg_type_str[reg->type], |
| 4860 | reg_type_str[PTR_TO_STACK]); |
| 4861 | return -EACCES; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | |
Dmitrii Banshchikov | e5069b9c | 2021-02-13 00:56:41 +0400 | [diff] [blame] | 4865 | int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
| 4866 | u32 regno, u32 mem_size) |
| 4867 | { |
| 4868 | if (register_is_null(reg)) |
| 4869 | return 0; |
| 4870 | |
| 4871 | if (reg_type_may_be_null(reg->type)) { |
| 4872 | /* Assuming that the register contains a value check if the memory |
| 4873 | * access is safe. Temporarily save and restore the register's state as |
| 4874 | * the conversion shouldn't be visible to a caller. |
| 4875 | */ |
| 4876 | const struct bpf_reg_state saved_reg = *reg; |
| 4877 | int rv; |
| 4878 | |
| 4879 | mark_ptr_not_null_reg(reg); |
| 4880 | rv = check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4881 | *reg = saved_reg; |
| 4882 | return rv; |
| 4883 | } |
| 4884 | |
| 4885 | return check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4886 | } |
| 4887 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 4888 | /* Implementation details: |
| 4889 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 4890 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| 4891 | * For traditional PTR_TO_MAP_VALUE the verifier clears reg->id after |
| 4892 | * value_or_null->value transition, since the verifier only cares about |
| 4893 | * the range of access to valid map value pointer and doesn't care about actual |
| 4894 | * address of the map element. |
| 4895 | * For maps with 'struct bpf_spin_lock' inside map value the verifier keeps |
| 4896 | * reg->id > 0 after value_or_null->value transition. By doing so |
| 4897 | * two bpf_map_lookups will be considered two different pointers that |
| 4898 | * point to different bpf_spin_locks. |
| 4899 | * The verifier allows taking only one bpf_spin_lock at a time to avoid |
| 4900 | * dead-locks. |
| 4901 | * Since only one bpf_spin_lock is allowed the checks are simpler than |
| 4902 | * reg_is_refcounted() logic. The verifier needs to remember only |
| 4903 | * one spin_lock instead of array of acquired_refs. |
| 4904 | * cur_state->active_spin_lock remembers which map value element got locked |
| 4905 | * and clears it after bpf_spin_unlock. |
| 4906 | */ |
| 4907 | static int process_spin_lock(struct bpf_verifier_env *env, int regno, |
| 4908 | bool is_lock) |
| 4909 | { |
| 4910 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4911 | struct bpf_verifier_state *cur = env->cur_state; |
| 4912 | bool is_const = tnum_is_const(reg->var_off); |
| 4913 | struct bpf_map *map = reg->map_ptr; |
| 4914 | u64 val = reg->var_off.value; |
| 4915 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 4916 | if (!is_const) { |
| 4917 | verbose(env, |
| 4918 | "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n", |
| 4919 | regno); |
| 4920 | return -EINVAL; |
| 4921 | } |
| 4922 | if (!map->btf) { |
| 4923 | verbose(env, |
| 4924 | "map '%s' has to have BTF in order to use bpf_spin_lock\n", |
| 4925 | map->name); |
| 4926 | return -EINVAL; |
| 4927 | } |
| 4928 | if (!map_value_has_spin_lock(map)) { |
| 4929 | if (map->spin_lock_off == -E2BIG) |
| 4930 | verbose(env, |
| 4931 | "map '%s' has more than one 'struct bpf_spin_lock'\n", |
| 4932 | map->name); |
| 4933 | else if (map->spin_lock_off == -ENOENT) |
| 4934 | verbose(env, |
| 4935 | "map '%s' doesn't have 'struct bpf_spin_lock'\n", |
| 4936 | map->name); |
| 4937 | else |
| 4938 | verbose(env, |
| 4939 | "map '%s' is not a struct type or bpf_spin_lock is mangled\n", |
| 4940 | map->name); |
| 4941 | return -EINVAL; |
| 4942 | } |
| 4943 | if (map->spin_lock_off != val + reg->off) { |
| 4944 | verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n", |
| 4945 | val + reg->off); |
| 4946 | return -EINVAL; |
| 4947 | } |
| 4948 | if (is_lock) { |
| 4949 | if (cur->active_spin_lock) { |
| 4950 | verbose(env, |
| 4951 | "Locking two bpf_spin_locks are not allowed\n"); |
| 4952 | return -EINVAL; |
| 4953 | } |
| 4954 | cur->active_spin_lock = reg->id; |
| 4955 | } else { |
| 4956 | if (!cur->active_spin_lock) { |
| 4957 | verbose(env, "bpf_spin_unlock without taking a lock\n"); |
| 4958 | return -EINVAL; |
| 4959 | } |
| 4960 | if (cur->active_spin_lock != reg->id) { |
| 4961 | verbose(env, "bpf_spin_unlock of different lock\n"); |
| 4962 | return -EINVAL; |
| 4963 | } |
| 4964 | cur->active_spin_lock = 0; |
| 4965 | } |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 4969 | static int process_timer_func(struct bpf_verifier_env *env, int regno, |
| 4970 | struct bpf_call_arg_meta *meta) |
| 4971 | { |
| 4972 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 4973 | bool is_const = tnum_is_const(reg->var_off); |
| 4974 | struct bpf_map *map = reg->map_ptr; |
| 4975 | u64 val = reg->var_off.value; |
| 4976 | |
| 4977 | if (!is_const) { |
| 4978 | verbose(env, |
| 4979 | "R%d doesn't have constant offset. bpf_timer has to be at the constant offset\n", |
| 4980 | regno); |
| 4981 | return -EINVAL; |
| 4982 | } |
| 4983 | if (!map->btf) { |
| 4984 | verbose(env, "map '%s' has to have BTF in order to use bpf_timer\n", |
| 4985 | map->name); |
| 4986 | return -EINVAL; |
| 4987 | } |
Alexei Starovoitov | 6813466 | 2021-07-14 17:54:10 -0700 | [diff] [blame] | 4988 | if (!map_value_has_timer(map)) { |
| 4989 | if (map->timer_off == -E2BIG) |
| 4990 | verbose(env, |
| 4991 | "map '%s' has more than one 'struct bpf_timer'\n", |
| 4992 | map->name); |
| 4993 | else if (map->timer_off == -ENOENT) |
| 4994 | verbose(env, |
| 4995 | "map '%s' doesn't have 'struct bpf_timer'\n", |
| 4996 | map->name); |
| 4997 | else |
| 4998 | verbose(env, |
| 4999 | "map '%s' is not a struct type or bpf_timer is mangled\n", |
| 5000 | map->name); |
| 5001 | return -EINVAL; |
| 5002 | } |
| 5003 | if (map->timer_off != val + reg->off) { |
| 5004 | verbose(env, "off %lld doesn't point to 'struct bpf_timer' that is at %d\n", |
| 5005 | val + reg->off, map->timer_off); |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5006 | return -EINVAL; |
| 5007 | } |
| 5008 | if (meta->map_ptr) { |
| 5009 | verbose(env, "verifier bug. Two map pointers in a timer helper\n"); |
| 5010 | return -EFAULT; |
| 5011 | } |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5012 | meta->map_uid = reg->map_uid; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5013 | meta->map_ptr = map; |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5017 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 5018 | { |
| 5019 | return type == ARG_PTR_TO_MEM || |
| 5020 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 5021 | type == ARG_PTR_TO_UNINIT_MEM; |
| 5022 | } |
| 5023 | |
| 5024 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 5025 | { |
| 5026 | return type == ARG_CONST_SIZE || |
| 5027 | type == ARG_CONST_SIZE_OR_ZERO; |
| 5028 | } |
| 5029 | |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5030 | static bool arg_type_is_alloc_size(enum bpf_arg_type type) |
| 5031 | { |
| 5032 | return type == ARG_CONST_ALLOC_SIZE_OR_ZERO; |
| 5033 | } |
| 5034 | |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 5035 | static bool arg_type_is_int_ptr(enum bpf_arg_type type) |
| 5036 | { |
| 5037 | return type == ARG_PTR_TO_INT || |
| 5038 | type == ARG_PTR_TO_LONG; |
| 5039 | } |
| 5040 | |
| 5041 | static int int_ptr_type_to_size(enum bpf_arg_type type) |
| 5042 | { |
| 5043 | if (type == ARG_PTR_TO_INT) |
| 5044 | return sizeof(u32); |
| 5045 | else if (type == ARG_PTR_TO_LONG) |
| 5046 | return sizeof(u64); |
| 5047 | |
| 5048 | return -EINVAL; |
| 5049 | } |
| 5050 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5051 | static int resolve_map_arg_type(struct bpf_verifier_env *env, |
| 5052 | const struct bpf_call_arg_meta *meta, |
| 5053 | enum bpf_arg_type *arg_type) |
| 5054 | { |
| 5055 | if (!meta->map_ptr) { |
| 5056 | /* kernel subsystem misconfigured verifier */ |
| 5057 | verbose(env, "invalid map_ptr to access map->type\n"); |
| 5058 | return -EACCES; |
| 5059 | } |
| 5060 | |
| 5061 | switch (meta->map_ptr->map_type) { |
| 5062 | case BPF_MAP_TYPE_SOCKMAP: |
| 5063 | case BPF_MAP_TYPE_SOCKHASH: |
| 5064 | if (*arg_type == ARG_PTR_TO_MAP_VALUE) { |
Lorenz Bauer | 6550f2d | 2020-09-28 10:08:02 +0100 | [diff] [blame] | 5065 | *arg_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5066 | } else { |
| 5067 | verbose(env, "invalid arg_type for sockmap/sockhash\n"); |
| 5068 | return -EINVAL; |
| 5069 | } |
| 5070 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5071 | case BPF_MAP_TYPE_BLOOM_FILTER: |
| 5072 | if (meta->func_id == BPF_FUNC_map_peek_elem) |
| 5073 | *arg_type = ARG_PTR_TO_MAP_VALUE; |
| 5074 | break; |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5075 | default: |
| 5076 | break; |
| 5077 | } |
| 5078 | return 0; |
| 5079 | } |
| 5080 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5081 | struct bpf_reg_types { |
| 5082 | const enum bpf_reg_type types[10]; |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5083 | u32 *btf_id; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5084 | }; |
| 5085 | |
| 5086 | static const struct bpf_reg_types map_key_value_types = { |
| 5087 | .types = { |
| 5088 | PTR_TO_STACK, |
| 5089 | PTR_TO_PACKET, |
| 5090 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5091 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5092 | PTR_TO_MAP_VALUE, |
| 5093 | }, |
| 5094 | }; |
| 5095 | |
| 5096 | static const struct bpf_reg_types sock_types = { |
| 5097 | .types = { |
| 5098 | PTR_TO_SOCK_COMMON, |
| 5099 | PTR_TO_SOCKET, |
| 5100 | PTR_TO_TCP_SOCK, |
| 5101 | PTR_TO_XDP_SOCK, |
| 5102 | }, |
| 5103 | }; |
| 5104 | |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5105 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5106 | static const struct bpf_reg_types btf_id_sock_common_types = { |
| 5107 | .types = { |
| 5108 | PTR_TO_SOCK_COMMON, |
| 5109 | PTR_TO_SOCKET, |
| 5110 | PTR_TO_TCP_SOCK, |
| 5111 | PTR_TO_XDP_SOCK, |
| 5112 | PTR_TO_BTF_ID, |
| 5113 | }, |
| 5114 | .btf_id = &btf_sock_ids[BTF_SOCK_TYPE_SOCK_COMMON], |
| 5115 | }; |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5116 | #endif |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5117 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5118 | static const struct bpf_reg_types mem_types = { |
| 5119 | .types = { |
| 5120 | PTR_TO_STACK, |
| 5121 | PTR_TO_PACKET, |
| 5122 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5123 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5124 | PTR_TO_MAP_VALUE, |
| 5125 | PTR_TO_MEM, |
| 5126 | PTR_TO_RDONLY_BUF, |
| 5127 | PTR_TO_RDWR_BUF, |
| 5128 | }, |
| 5129 | }; |
| 5130 | |
| 5131 | static const struct bpf_reg_types int_ptr_types = { |
| 5132 | .types = { |
| 5133 | PTR_TO_STACK, |
| 5134 | PTR_TO_PACKET, |
| 5135 | PTR_TO_PACKET_META, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5136 | PTR_TO_MAP_KEY, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5137 | PTR_TO_MAP_VALUE, |
| 5138 | }, |
| 5139 | }; |
| 5140 | |
| 5141 | static const struct bpf_reg_types fullsock_types = { .types = { PTR_TO_SOCKET } }; |
| 5142 | static const struct bpf_reg_types scalar_types = { .types = { SCALAR_VALUE } }; |
| 5143 | static const struct bpf_reg_types context_types = { .types = { PTR_TO_CTX } }; |
| 5144 | static const struct bpf_reg_types alloc_mem_types = { .types = { PTR_TO_MEM } }; |
| 5145 | static const struct bpf_reg_types const_map_ptr_types = { .types = { CONST_PTR_TO_MAP } }; |
| 5146 | static const struct bpf_reg_types btf_ptr_types = { .types = { PTR_TO_BTF_ID } }; |
| 5147 | static const struct bpf_reg_types spin_lock_types = { .types = { PTR_TO_MAP_VALUE } }; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5148 | static const struct bpf_reg_types percpu_btf_ptr_types = { .types = { PTR_TO_PERCPU_BTF_ID } }; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5149 | static const struct bpf_reg_types func_ptr_types = { .types = { PTR_TO_FUNC } }; |
| 5150 | static const struct bpf_reg_types stack_ptr_types = { .types = { PTR_TO_STACK } }; |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5151 | static const struct bpf_reg_types const_str_ptr_types = { .types = { PTR_TO_MAP_VALUE } }; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5152 | static const struct bpf_reg_types timer_types = { .types = { PTR_TO_MAP_VALUE } }; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5153 | |
Lorenz Bauer | 0789e13b | 2020-09-23 17:01:55 +0100 | [diff] [blame] | 5154 | static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5155 | [ARG_PTR_TO_MAP_KEY] = &map_key_value_types, |
| 5156 | [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types, |
| 5157 | [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types, |
| 5158 | [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types, |
| 5159 | [ARG_CONST_SIZE] = &scalar_types, |
| 5160 | [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, |
| 5161 | [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, |
| 5162 | [ARG_CONST_MAP_PTR] = &const_map_ptr_types, |
| 5163 | [ARG_PTR_TO_CTX] = &context_types, |
| 5164 | [ARG_PTR_TO_CTX_OR_NULL] = &context_types, |
| 5165 | [ARG_PTR_TO_SOCK_COMMON] = &sock_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5166 | #ifdef CONFIG_NET |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5167 | [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types, |
Randy Dunlap | 49a2a4d | 2020-10-06 19:16:13 -0700 | [diff] [blame] | 5168 | #endif |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5169 | [ARG_PTR_TO_SOCKET] = &fullsock_types, |
| 5170 | [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, |
| 5171 | [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, |
| 5172 | [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, |
| 5173 | [ARG_PTR_TO_MEM] = &mem_types, |
| 5174 | [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, |
| 5175 | [ARG_PTR_TO_UNINIT_MEM] = &mem_types, |
| 5176 | [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types, |
| 5177 | [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types, |
| 5178 | [ARG_PTR_TO_INT] = &int_ptr_types, |
| 5179 | [ARG_PTR_TO_LONG] = &int_ptr_types, |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5180 | [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types, |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5181 | [ARG_PTR_TO_FUNC] = &func_ptr_types, |
| 5182 | [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types, |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5183 | [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types, |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5184 | [ARG_PTR_TO_TIMER] = &timer_types, |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5185 | }; |
| 5186 | |
| 5187 | static int check_reg_type(struct bpf_verifier_env *env, u32 regno, |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5188 | enum bpf_arg_type arg_type, |
| 5189 | const u32 *arg_btf_id) |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5190 | { |
| 5191 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
| 5192 | enum bpf_reg_type expected, type = reg->type; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5193 | const struct bpf_reg_types *compatible; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5194 | int i, j; |
| 5195 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5196 | compatible = compatible_reg_types[arg_type]; |
| 5197 | if (!compatible) { |
| 5198 | verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type); |
| 5199 | return -EFAULT; |
| 5200 | } |
| 5201 | |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5202 | for (i = 0; i < ARRAY_SIZE(compatible->types); i++) { |
| 5203 | expected = compatible->types[i]; |
| 5204 | if (expected == NOT_INIT) |
| 5205 | break; |
| 5206 | |
| 5207 | if (type == expected) |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5208 | goto found; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]); |
| 5212 | for (j = 0; j + 1 < i; j++) |
| 5213 | verbose(env, "%s, ", reg_type_str[compatible->types[j]]); |
| 5214 | verbose(env, "%s\n", reg_type_str[compatible->types[j]]); |
| 5215 | return -EACCES; |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5216 | |
| 5217 | found: |
| 5218 | if (type == PTR_TO_BTF_ID) { |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5219 | if (!arg_btf_id) { |
| 5220 | if (!compatible->btf_id) { |
| 5221 | verbose(env, "verifier internal error: missing arg compatible BTF ID\n"); |
| 5222 | return -EFAULT; |
| 5223 | } |
| 5224 | arg_btf_id = compatible->btf_id; |
| 5225 | } |
| 5226 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5227 | if (!btf_struct_ids_match(&env->log, reg->btf, reg->btf_id, reg->off, |
| 5228 | btf_vmlinux, *arg_btf_id)) { |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5229 | verbose(env, "R%d is of type %s but %s is expected\n", |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5230 | regno, kernel_type_name(reg->btf, reg->btf_id), |
| 5231 | kernel_type_name(btf_vmlinux, *arg_btf_id)); |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5232 | return -EACCES; |
| 5233 | } |
| 5234 | |
| 5235 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
| 5236 | verbose(env, "R%d is a pointer to in-kernel struct with non-zero offset\n", |
| 5237 | regno); |
| 5238 | return -EACCES; |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | return 0; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5243 | } |
| 5244 | |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5245 | static int check_func_arg(struct bpf_verifier_env *env, u32 arg, |
| 5246 | struct bpf_call_arg_meta *meta, |
| 5247 | const struct bpf_func_proto *fn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5248 | { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5249 | u32 regno = BPF_REG_1 + arg; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5250 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 5251 | enum bpf_arg_type arg_type = fn->arg_type[arg]; |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5252 | enum bpf_reg_type type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5253 | int err = 0; |
| 5254 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5255 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5256 | return 0; |
| 5257 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 5258 | err = check_reg_arg(env, regno, SRC_OP); |
| 5259 | if (err) |
| 5260 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5261 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5262 | if (arg_type == ARG_ANYTHING) { |
| 5263 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5264 | verbose(env, "R%d leaks addr into helper function\n", |
| 5265 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5266 | return -EACCES; |
| 5267 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5268 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 5269 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 5270 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5271 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 5272 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5273 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 5274 | return -EACCES; |
| 5275 | } |
| 5276 | |
Lorenz Bauer | 912f442 | 2020-08-21 11:29:46 +0100 | [diff] [blame] | 5277 | if (arg_type == ARG_PTR_TO_MAP_VALUE || |
| 5278 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE || |
| 5279 | arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) { |
| 5280 | err = resolve_map_arg_type(env, meta, &arg_type); |
| 5281 | if (err) |
| 5282 | return err; |
| 5283 | } |
| 5284 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 5285 | if (register_is_null(reg) && arg_type_may_be_null(arg_type)) |
| 5286 | /* A NULL register has a SCALAR_VALUE type, so skip |
| 5287 | * type checking. |
| 5288 | */ |
| 5289 | goto skip_type_check; |
Jiri Olsa | faaf4a7 | 2020-08-25 21:21:18 +0200 | [diff] [blame] | 5290 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5291 | err = check_reg_type(env, regno, arg_type, fn->arg_btf_id[arg]); |
Lorenz Bauer | f79e7ea | 2020-09-21 13:12:27 +0100 | [diff] [blame] | 5292 | if (err) |
| 5293 | return err; |
| 5294 | |
Martin KaFai Lau | a968d5e | 2020-09-24 17:03:44 -0700 | [diff] [blame] | 5295 | if (type == PTR_TO_CTX) { |
Lorenz Bauer | feec704 | 2020-09-21 13:12:23 +0100 | [diff] [blame] | 5296 | err = check_ctx_reg(env, reg, regno); |
| 5297 | if (err < 0) |
| 5298 | return err; |
Lorenz Bauer | d7b9454 | 2020-09-21 13:12:21 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
Lorenz Bauer | fd1b0d6 | 2020-09-21 13:12:26 +0100 | [diff] [blame] | 5301 | skip_type_check: |
Lorenz Bauer | 02f7c95 | 2020-09-21 13:12:22 +0100 | [diff] [blame] | 5302 | if (reg->ref_obj_id) { |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5303 | if (meta->ref_obj_id) { |
| 5304 | verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n", |
| 5305 | regno, reg->ref_obj_id, |
| 5306 | meta->ref_obj_id); |
| 5307 | return -EFAULT; |
| 5308 | } |
| 5309 | meta->ref_obj_id = reg->ref_obj_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5310 | } |
| 5311 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5312 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 5313 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5314 | if (meta->map_ptr) { |
| 5315 | /* Use map_uid (which is unique id of inner map) to reject: |
| 5316 | * inner_map1 = bpf_map_lookup_elem(outer_map, key1) |
| 5317 | * inner_map2 = bpf_map_lookup_elem(outer_map, key2) |
| 5318 | * if (inner_map1 && inner_map2) { |
| 5319 | * timer = bpf_map_lookup_elem(inner_map1); |
| 5320 | * if (timer) |
| 5321 | * // mismatch would have been allowed |
| 5322 | * bpf_timer_init(timer, inner_map2); |
| 5323 | * } |
| 5324 | * |
| 5325 | * Comparing map_ptr is enough to distinguish normal and outer maps. |
| 5326 | */ |
| 5327 | if (meta->map_ptr != reg->map_ptr || |
| 5328 | meta->map_uid != reg->map_uid) { |
| 5329 | verbose(env, |
| 5330 | "timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n", |
| 5331 | meta->map_uid, reg->map_uid); |
| 5332 | return -EINVAL; |
| 5333 | } |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5334 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5335 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 5336 | meta->map_uid = reg->map_uid; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5337 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 5338 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 5339 | * check that [key, key + map->key_size) are within |
| 5340 | * stack limits and initialized |
| 5341 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5342 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5343 | /* in function declaration map_ptr must come before |
| 5344 | * map_key, so that it's verified and known before |
| 5345 | * we have to check map_key here. Otherwise it means |
| 5346 | * that kernel subsystem misconfigured verifier |
| 5347 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5348 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5349 | return -EACCES; |
| 5350 | } |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 5351 | err = check_helper_mem_access(env, regno, |
| 5352 | meta->map_ptr->key_size, false, |
| 5353 | NULL); |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5354 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE || |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 5355 | (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL && |
| 5356 | !register_is_null(reg)) || |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5357 | arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5358 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 5359 | * check [value, value + map->value_size) validity |
| 5360 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 5361 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5362 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5363 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5364 | return -EACCES; |
| 5365 | } |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5366 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE); |
Paul Chaignon | d71962f | 2018-04-24 15:07:54 +0200 | [diff] [blame] | 5367 | err = check_helper_mem_access(env, regno, |
| 5368 | meta->map_ptr->value_size, false, |
Mauricio Vasquez B | 2ea864c | 2018-10-18 15:16:20 +0200 | [diff] [blame] | 5369 | meta); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5370 | } else if (arg_type == ARG_PTR_TO_PERCPU_BTF_ID) { |
| 5371 | if (!reg->btf_id) { |
| 5372 | verbose(env, "Helper has invalid btf_id in R%d\n", regno); |
| 5373 | return -EACCES; |
| 5374 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 5375 | meta->ret_btf = reg->btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 5376 | meta->ret_btf_id = reg->btf_id; |
Lorenz Bauer | c18f0b6 | 2020-09-21 13:12:25 +0100 | [diff] [blame] | 5377 | } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) { |
| 5378 | if (meta->func_id == BPF_FUNC_spin_lock) { |
| 5379 | if (process_spin_lock(env, regno, true)) |
| 5380 | return -EACCES; |
| 5381 | } else if (meta->func_id == BPF_FUNC_spin_unlock) { |
| 5382 | if (process_spin_lock(env, regno, false)) |
| 5383 | return -EACCES; |
| 5384 | } else { |
| 5385 | verbose(env, "verifier internal error\n"); |
| 5386 | return -EFAULT; |
| 5387 | } |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 5388 | } else if (arg_type == ARG_PTR_TO_TIMER) { |
| 5389 | if (process_timer_func(env, regno, meta)) |
| 5390 | return -EACCES; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 5391 | } else if (arg_type == ARG_PTR_TO_FUNC) { |
| 5392 | meta->subprogno = reg->subprogno; |
Lorenz Bauer | a2bbe7c | 2020-09-21 13:12:24 +0100 | [diff] [blame] | 5393 | } else if (arg_type_is_mem_ptr(arg_type)) { |
| 5394 | /* The access to this pointer is only checked when we hit the |
| 5395 | * next is_mem_size argument below. |
| 5396 | */ |
| 5397 | meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MEM); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5398 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5399 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5400 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 5401 | /* This is used to refine r0 return value bounds for helpers |
| 5402 | * that enforce this value as an upper bound on return values. |
| 5403 | * See do_refine_retval_range() for helpers that can refine |
| 5404 | * the return value. C type of helper is u32 so we pull register |
| 5405 | * bound from umax_value however, if negative verifier errors |
| 5406 | * out. Only upper bounds can be learned because retval is an |
| 5407 | * int type and negative retvals are allowed. |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5408 | */ |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 5409 | meta->msize_max_value = reg->umax_value; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 5410 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5411 | /* The register is SCALAR_VALUE; the access check |
| 5412 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5413 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5414 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5415 | /* For unprivileged variable accesses, disable raw |
| 5416 | * mode so that the program is required to |
| 5417 | * initialize all the memory that the helper could |
| 5418 | * just partially fill up. |
| 5419 | */ |
| 5420 | meta = NULL; |
| 5421 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5422 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5423 | verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5424 | regno); |
| 5425 | return -EACCES; |
| 5426 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5427 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5428 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5429 | err = check_helper_mem_access(env, regno - 1, 0, |
| 5430 | zero_size_allowed, |
| 5431 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5432 | if (err) |
| 5433 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 5434 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5435 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5436 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5437 | verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5438 | regno); |
| 5439 | return -EACCES; |
| 5440 | } |
| 5441 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 5442 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5443 | zero_size_allowed, meta); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 5444 | if (!err) |
| 5445 | err = mark_chain_precision(env, regno); |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5446 | } else if (arg_type_is_alloc_size(arg_type)) { |
| 5447 | if (!tnum_is_const(reg->var_off)) { |
Brendan Jackman | 28a8add | 2021-01-12 12:39:13 +0000 | [diff] [blame] | 5448 | verbose(env, "R%d is not a known constant'\n", |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5449 | regno); |
| 5450 | return -EACCES; |
| 5451 | } |
| 5452 | meta->mem_size = reg->var_off.value; |
Andrey Ignatov | 57c3bb7 | 2019-03-18 16:57:10 -0700 | [diff] [blame] | 5453 | } else if (arg_type_is_int_ptr(arg_type)) { |
| 5454 | int size = int_ptr_type_to_size(arg_type); |
| 5455 | |
| 5456 | err = check_helper_mem_access(env, regno, size, false, meta); |
| 5457 | if (err) |
| 5458 | return err; |
| 5459 | err = check_ptr_alignment(env, reg, 0, size, true); |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5460 | } else if (arg_type == ARG_PTR_TO_CONST_STR) { |
| 5461 | struct bpf_map *map = reg->map_ptr; |
| 5462 | int map_off; |
| 5463 | u64 map_addr; |
| 5464 | char *str_ptr; |
| 5465 | |
Florent Revest | a8fad73 | 2021-04-23 01:55:43 +0200 | [diff] [blame] | 5466 | if (!bpf_map_is_rdonly(map)) { |
Florent Revest | fff13c4 | 2021-04-19 17:52:39 +0200 | [diff] [blame] | 5467 | verbose(env, "R%d does not point to a readonly map'\n", regno); |
| 5468 | return -EACCES; |
| 5469 | } |
| 5470 | |
| 5471 | if (!tnum_is_const(reg->var_off)) { |
| 5472 | verbose(env, "R%d is not a constant address'\n", regno); |
| 5473 | return -EACCES; |
| 5474 | } |
| 5475 | |
| 5476 | if (!map->ops->map_direct_value_addr) { |
| 5477 | verbose(env, "no direct value access support for this map type\n"); |
| 5478 | return -EACCES; |
| 5479 | } |
| 5480 | |
| 5481 | err = check_map_access(env, regno, reg->off, |
| 5482 | map->value_size - reg->off, false); |
| 5483 | if (err) |
| 5484 | return err; |
| 5485 | |
| 5486 | map_off = reg->off + reg->var_off.value; |
| 5487 | err = map->ops->map_direct_value_addr(map, &map_addr, map_off); |
| 5488 | if (err) { |
| 5489 | verbose(env, "direct value access on string failed\n"); |
| 5490 | return err; |
| 5491 | } |
| 5492 | |
| 5493 | str_ptr = (char *)(long)(map_addr); |
| 5494 | if (!strnchr(str_ptr + map_off, map->value_size - map_off, 0)) { |
| 5495 | verbose(env, "string is not zero-terminated\n"); |
| 5496 | return -EINVAL; |
| 5497 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | return err; |
| 5501 | } |
| 5502 | |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5503 | static bool may_update_sockmap(struct bpf_verifier_env *env, int func_id) |
| 5504 | { |
| 5505 | enum bpf_attach_type eatype = env->prog->expected_attach_type; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 5506 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5507 | |
| 5508 | if (func_id != BPF_FUNC_map_update_elem) |
| 5509 | return false; |
| 5510 | |
| 5511 | /* It's not possible to get access to a locked struct sock in these |
| 5512 | * contexts, so updating is safe. |
| 5513 | */ |
| 5514 | switch (type) { |
| 5515 | case BPF_PROG_TYPE_TRACING: |
| 5516 | if (eatype == BPF_TRACE_ITER) |
| 5517 | return true; |
| 5518 | break; |
| 5519 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 5520 | case BPF_PROG_TYPE_SCHED_CLS: |
| 5521 | case BPF_PROG_TYPE_SCHED_ACT: |
| 5522 | case BPF_PROG_TYPE_XDP: |
| 5523 | case BPF_PROG_TYPE_SK_REUSEPORT: |
| 5524 | case BPF_PROG_TYPE_FLOW_DISSECTOR: |
| 5525 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 5526 | return true; |
| 5527 | default: |
| 5528 | break; |
| 5529 | } |
| 5530 | |
| 5531 | verbose(env, "cannot update sockmap in this context\n"); |
| 5532 | return false; |
| 5533 | } |
| 5534 | |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 5535 | static bool allow_tail_call_in_subprogs(struct bpf_verifier_env *env) |
| 5536 | { |
| 5537 | return env->prog->jit_requested && IS_ENABLED(CONFIG_X86_64); |
| 5538 | } |
| 5539 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5540 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 5541 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5542 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5543 | if (!map) |
| 5544 | return 0; |
| 5545 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5546 | /* We need a two way check, first is from map perspective ... */ |
| 5547 | switch (map->map_type) { |
| 5548 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 5549 | if (func_id != BPF_FUNC_tail_call) |
| 5550 | goto error; |
| 5551 | break; |
| 5552 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 5553 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 5554 | func_id != BPF_FUNC_perf_event_output && |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5555 | func_id != BPF_FUNC_skb_output && |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 5556 | func_id != BPF_FUNC_perf_event_read_value && |
| 5557 | func_id != BPF_FUNC_xdp_output) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5558 | goto error; |
| 5559 | break; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5560 | case BPF_MAP_TYPE_RINGBUF: |
| 5561 | if (func_id != BPF_FUNC_ringbuf_output && |
| 5562 | func_id != BPF_FUNC_ringbuf_reserve && |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 5563 | func_id != BPF_FUNC_ringbuf_query) |
| 5564 | goto error; |
| 5565 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5566 | case BPF_MAP_TYPE_STACK_TRACE: |
| 5567 | if (func_id != BPF_FUNC_get_stackid) |
| 5568 | goto error; |
| 5569 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 5570 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 5571 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 5572 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 5573 | goto error; |
| 5574 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5575 | case BPF_MAP_TYPE_CGROUP_STORAGE: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5576 | case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE: |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5577 | if (func_id != BPF_FUNC_get_local_storage) |
| 5578 | goto error; |
| 5579 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 5580 | case BPF_MAP_TYPE_DEVMAP: |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 5581 | case BPF_MAP_TYPE_DEVMAP_HASH: |
Toke Høiland-Jørgensen | 0cdbb4b | 2019-06-28 11:12:35 +0200 | [diff] [blame] | 5582 | if (func_id != BPF_FUNC_redirect_map && |
| 5583 | func_id != BPF_FUNC_map_lookup_elem) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 5584 | goto error; |
| 5585 | break; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 5586 | /* Restrict bpf side of cpumap and xskmap, open when use-cases |
| 5587 | * appear. |
| 5588 | */ |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 5589 | case BPF_MAP_TYPE_CPUMAP: |
| 5590 | if (func_id != BPF_FUNC_redirect_map) |
| 5591 | goto error; |
| 5592 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 5593 | case BPF_MAP_TYPE_XSKMAP: |
| 5594 | if (func_id != BPF_FUNC_redirect_map && |
| 5595 | func_id != BPF_FUNC_map_lookup_elem) |
| 5596 | goto error; |
| 5597 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5598 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 5599 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 5600 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 5601 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 5602 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5603 | case BPF_MAP_TYPE_SOCKMAP: |
| 5604 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 5605 | func_id != BPF_FUNC_sock_map_update && |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 5606 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5607 | func_id != BPF_FUNC_msg_redirect_map && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5608 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5609 | func_id != BPF_FUNC_map_lookup_elem && |
| 5610 | !may_update_sockmap(env, func_id)) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5611 | goto error; |
| 5612 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5613 | case BPF_MAP_TYPE_SOCKHASH: |
| 5614 | if (func_id != BPF_FUNC_sk_redirect_hash && |
| 5615 | func_id != BPF_FUNC_sock_hash_update && |
| 5616 | func_id != BPF_FUNC_map_delete_elem && |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5617 | func_id != BPF_FUNC_msg_redirect_hash && |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5618 | func_id != BPF_FUNC_sk_select_reuseport && |
Lorenz Bauer | 0126240 | 2020-08-21 11:29:47 +0100 | [diff] [blame] | 5619 | func_id != BPF_FUNC_map_lookup_elem && |
| 5620 | !may_update_sockmap(env, func_id)) |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5621 | goto error; |
| 5622 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5623 | case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY: |
| 5624 | if (func_id != BPF_FUNC_sk_select_reuseport) |
| 5625 | goto error; |
| 5626 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5627 | case BPF_MAP_TYPE_QUEUE: |
| 5628 | case BPF_MAP_TYPE_STACK: |
| 5629 | if (func_id != BPF_FUNC_map_peek_elem && |
| 5630 | func_id != BPF_FUNC_map_pop_elem && |
| 5631 | func_id != BPF_FUNC_map_push_elem) |
| 5632 | goto error; |
| 5633 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 5634 | case BPF_MAP_TYPE_SK_STORAGE: |
| 5635 | if (func_id != BPF_FUNC_sk_storage_get && |
| 5636 | func_id != BPF_FUNC_sk_storage_delete) |
| 5637 | goto error; |
| 5638 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 5639 | case BPF_MAP_TYPE_INODE_STORAGE: |
| 5640 | if (func_id != BPF_FUNC_inode_storage_get && |
| 5641 | func_id != BPF_FUNC_inode_storage_delete) |
| 5642 | goto error; |
| 5643 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 5644 | case BPF_MAP_TYPE_TASK_STORAGE: |
| 5645 | if (func_id != BPF_FUNC_task_storage_get && |
| 5646 | func_id != BPF_FUNC_task_storage_delete) |
| 5647 | goto error; |
| 5648 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5649 | case BPF_MAP_TYPE_BLOOM_FILTER: |
| 5650 | if (func_id != BPF_FUNC_map_peek_elem && |
| 5651 | func_id != BPF_FUNC_map_push_elem) |
| 5652 | goto error; |
| 5653 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5654 | default: |
| 5655 | break; |
| 5656 | } |
| 5657 | |
| 5658 | /* ... and second from the function itself. */ |
| 5659 | switch (func_id) { |
| 5660 | case BPF_FUNC_tail_call: |
| 5661 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 5662 | goto error; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 5663 | if (env->subprog_cnt > 1 && !allow_tail_call_in_subprogs(env)) { |
| 5664 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5665 | return -EINVAL; |
| 5666 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5667 | break; |
| 5668 | case BPF_FUNC_perf_event_read: |
| 5669 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 5670 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 5671 | case BPF_FUNC_skb_output: |
Eelco Chaudron | d831ee8 | 2020-03-06 08:59:23 +0000 | [diff] [blame] | 5672 | case BPF_FUNC_xdp_output: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5673 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 5674 | goto error; |
| 5675 | break; |
Daniel Borkmann | 5b029a3 | 2021-08-23 21:02:09 +0200 | [diff] [blame] | 5676 | case BPF_FUNC_ringbuf_output: |
| 5677 | case BPF_FUNC_ringbuf_reserve: |
| 5678 | case BPF_FUNC_ringbuf_query: |
| 5679 | if (map->map_type != BPF_MAP_TYPE_RINGBUF) |
| 5680 | goto error; |
| 5681 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5682 | case BPF_FUNC_get_stackid: |
| 5683 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 5684 | goto error; |
| 5685 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 5686 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 5687 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 5688 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 5689 | goto error; |
| 5690 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 5691 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 5692 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
Toke Høiland-Jørgensen | 6f9d451 | 2019-07-26 18:06:55 +0200 | [diff] [blame] | 5693 | map->map_type != BPF_MAP_TYPE_DEVMAP_HASH && |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 5694 | map->map_type != BPF_MAP_TYPE_CPUMAP && |
| 5695 | map->map_type != BPF_MAP_TYPE_XSKMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 5696 | goto error; |
| 5697 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5698 | case BPF_FUNC_sk_redirect_map: |
John Fastabend | 4f738ad | 2018-03-18 12:57:10 -0700 | [diff] [blame] | 5699 | case BPF_FUNC_msg_redirect_map: |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5700 | case BPF_FUNC_sock_map_update: |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5701 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 5702 | goto error; |
| 5703 | break; |
John Fastabend | 8111038 | 2018-05-14 10:00:17 -0700 | [diff] [blame] | 5704 | case BPF_FUNC_sk_redirect_hash: |
| 5705 | case BPF_FUNC_msg_redirect_hash: |
| 5706 | case BPF_FUNC_sock_hash_update: |
| 5707 | if (map->map_type != BPF_MAP_TYPE_SOCKHASH) |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 5708 | goto error; |
| 5709 | break; |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5710 | case BPF_FUNC_get_local_storage: |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 5711 | if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE && |
| 5712 | map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 5713 | goto error; |
| 5714 | break; |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5715 | case BPF_FUNC_sk_select_reuseport: |
Jakub Sitnicki | 9fed900 | 2020-02-18 17:10:20 +0000 | [diff] [blame] | 5716 | if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY && |
| 5717 | map->map_type != BPF_MAP_TYPE_SOCKMAP && |
| 5718 | map->map_type != BPF_MAP_TYPE_SOCKHASH) |
Martin KaFai Lau | 2dbb9b9 | 2018-08-08 01:01:25 -0700 | [diff] [blame] | 5719 | goto error; |
| 5720 | break; |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5721 | case BPF_FUNC_map_pop_elem: |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 5722 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 5723 | map->map_type != BPF_MAP_TYPE_STACK) |
| 5724 | goto error; |
| 5725 | break; |
Joanne Koong | 9330986 | 2021-10-27 16:45:00 -0700 | [diff] [blame] | 5726 | case BPF_FUNC_map_peek_elem: |
| 5727 | case BPF_FUNC_map_push_elem: |
| 5728 | if (map->map_type != BPF_MAP_TYPE_QUEUE && |
| 5729 | map->map_type != BPF_MAP_TYPE_STACK && |
| 5730 | map->map_type != BPF_MAP_TYPE_BLOOM_FILTER) |
| 5731 | goto error; |
| 5732 | break; |
Martin KaFai Lau | 6ac99e8 | 2019-04-26 16:39:39 -0700 | [diff] [blame] | 5733 | case BPF_FUNC_sk_storage_get: |
| 5734 | case BPF_FUNC_sk_storage_delete: |
| 5735 | if (map->map_type != BPF_MAP_TYPE_SK_STORAGE) |
| 5736 | goto error; |
| 5737 | break; |
KP Singh | 8ea6368 | 2020-08-25 20:29:17 +0200 | [diff] [blame] | 5738 | case BPF_FUNC_inode_storage_get: |
| 5739 | case BPF_FUNC_inode_storage_delete: |
| 5740 | if (map->map_type != BPF_MAP_TYPE_INODE_STORAGE) |
| 5741 | goto error; |
| 5742 | break; |
KP Singh | 4cf1bc1 | 2020-11-06 10:37:40 +0000 | [diff] [blame] | 5743 | case BPF_FUNC_task_storage_get: |
| 5744 | case BPF_FUNC_task_storage_delete: |
| 5745 | if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) |
| 5746 | goto error; |
| 5747 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5748 | default: |
| 5749 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5750 | } |
| 5751 | |
| 5752 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5753 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5754 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 5755 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 5756 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5759 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5760 | { |
| 5761 | int count = 0; |
| 5762 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5763 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5764 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5765 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5766 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5767 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5768 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5769 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5770 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 5771 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5772 | count++; |
| 5773 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5774 | /* We only support one arg being in raw mode at the moment, |
| 5775 | * which is sufficient for the helper functions we have |
| 5776 | * right now. |
| 5777 | */ |
| 5778 | return count <= 1; |
| 5779 | } |
| 5780 | |
| 5781 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 5782 | enum bpf_arg_type arg_next) |
| 5783 | { |
| 5784 | return (arg_type_is_mem_ptr(arg_curr) && |
| 5785 | !arg_type_is_mem_size(arg_next)) || |
| 5786 | (!arg_type_is_mem_ptr(arg_curr) && |
| 5787 | arg_type_is_mem_size(arg_next)); |
| 5788 | } |
| 5789 | |
| 5790 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 5791 | { |
| 5792 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 5793 | * bytes from memory 'buf'. Both arg types need |
| 5794 | * to be paired, so make sure there's no buggy |
| 5795 | * helper function specification. |
| 5796 | */ |
| 5797 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 5798 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 5799 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 5800 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 5801 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 5802 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 5803 | return false; |
| 5804 | |
| 5805 | return true; |
| 5806 | } |
| 5807 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5808 | static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5809 | { |
| 5810 | int count = 0; |
| 5811 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5812 | if (arg_type_may_be_refcounted(fn->arg1_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5813 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5814 | if (arg_type_may_be_refcounted(fn->arg2_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5815 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5816 | if (arg_type_may_be_refcounted(fn->arg3_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5817 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5818 | if (arg_type_may_be_refcounted(fn->arg4_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5819 | count++; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5820 | if (arg_type_may_be_refcounted(fn->arg5_type)) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5821 | count++; |
| 5822 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5823 | /* A reference acquiring function cannot acquire |
| 5824 | * another refcounted ptr. |
| 5825 | */ |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 5826 | if (may_be_acquire_function(func_id) && count) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5827 | return false; |
| 5828 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5829 | /* We only support one arg being unreferenced at the moment, |
| 5830 | * which is sufficient for the helper functions we have right now. |
| 5831 | */ |
| 5832 | return count <= 1; |
| 5833 | } |
| 5834 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5835 | static bool check_btf_id_ok(const struct bpf_func_proto *fn) |
| 5836 | { |
| 5837 | int i; |
| 5838 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5839 | for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5840 | if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) |
| 5841 | return false; |
| 5842 | |
Martin KaFai Lau | 1df8f55 | 2020-09-24 17:03:50 -0700 | [diff] [blame] | 5843 | if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) |
| 5844 | return false; |
| 5845 | } |
| 5846 | |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5847 | return true; |
| 5848 | } |
| 5849 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5850 | static int check_func_proto(const struct bpf_func_proto *fn, int func_id) |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 5851 | { |
| 5852 | return check_raw_mode_ok(fn) && |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5853 | check_arg_pair_ok(fn) && |
Lorenz Bauer | 9436ef6 | 2020-09-21 13:12:20 +0100 | [diff] [blame] | 5854 | check_btf_id_ok(fn) && |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5855 | check_refcount_ok(fn, func_id) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 5856 | } |
| 5857 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5858 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 5859 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 5860 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5861 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 5862 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5863 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5864 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5865 | int i; |
| 5866 | |
| 5867 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5868 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5869 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5870 | |
Joe Stringer | f3709f6 | 2018-10-02 13:35:29 -0700 | [diff] [blame] | 5871 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5872 | if (!reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5873 | continue; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 5874 | if (reg_is_pkt_pointer_any(reg)) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5875 | __mark_reg_unknown(env, reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 5876 | } |
| 5877 | } |
| 5878 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5879 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 5880 | { |
| 5881 | struct bpf_verifier_state *vstate = env->cur_state; |
| 5882 | int i; |
| 5883 | |
| 5884 | for (i = 0; i <= vstate->curframe; i++) |
| 5885 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 5886 | } |
| 5887 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 5888 | enum { |
| 5889 | AT_PKT_END = -1, |
| 5890 | BEYOND_PKT_END = -2, |
| 5891 | }; |
| 5892 | |
| 5893 | static void mark_pkt_end(struct bpf_verifier_state *vstate, int regn, bool range_open) |
| 5894 | { |
| 5895 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 5896 | struct bpf_reg_state *reg = &state->regs[regn]; |
| 5897 | |
| 5898 | if (reg->type != PTR_TO_PACKET) |
| 5899 | /* PTR_TO_PACKET_META is not supported yet */ |
| 5900 | return; |
| 5901 | |
| 5902 | /* The 'reg' is pkt > pkt_end or pkt >= pkt_end. |
| 5903 | * How far beyond pkt_end it goes is unknown. |
| 5904 | * if (!range_open) it's the case of pkt >= pkt_end |
| 5905 | * if (range_open) it's the case of pkt > pkt_end |
| 5906 | * hence this pointer is at least 1 byte bigger than pkt_end |
| 5907 | */ |
| 5908 | if (range_open) |
| 5909 | reg->range = BEYOND_PKT_END; |
| 5910 | else |
| 5911 | reg->range = AT_PKT_END; |
| 5912 | } |
| 5913 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5914 | static void release_reg_references(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5915 | struct bpf_func_state *state, |
| 5916 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5917 | { |
| 5918 | struct bpf_reg_state *regs = state->regs, *reg; |
| 5919 | int i; |
| 5920 | |
| 5921 | for (i = 0; i < MAX_BPF_REG; i++) |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5922 | if (regs[i].ref_obj_id == ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5923 | mark_reg_unknown(env, regs, i); |
| 5924 | |
| 5925 | bpf_for_each_spilled_reg(i, state, reg) { |
| 5926 | if (!reg) |
| 5927 | continue; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5928 | if (reg->ref_obj_id == ref_obj_id) |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 5929 | __mark_reg_unknown(env, reg); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5930 | } |
| 5931 | } |
| 5932 | |
| 5933 | /* The pointer with the specified id has released its reference to kernel |
| 5934 | * resources. Identify all copies of the same pointer and clear the reference. |
| 5935 | */ |
| 5936 | static int release_reference(struct bpf_verifier_env *env, |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5937 | int ref_obj_id) |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5938 | { |
| 5939 | struct bpf_verifier_state *vstate = env->cur_state; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5940 | int err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5941 | int i; |
| 5942 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5943 | err = release_reference_state(cur_func(env), ref_obj_id); |
| 5944 | if (err) |
| 5945 | return err; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5946 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 5947 | for (i = 0; i <= vstate->curframe; i++) |
| 5948 | release_reg_references(env, vstate->frame[i], ref_obj_id); |
| 5949 | |
| 5950 | return 0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 5951 | } |
| 5952 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5953 | static void clear_caller_saved_regs(struct bpf_verifier_env *env, |
| 5954 | struct bpf_reg_state *regs) |
| 5955 | { |
| 5956 | int i; |
| 5957 | |
| 5958 | /* after the call registers r0 - r5 were scratched */ |
| 5959 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 5960 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 5961 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 5962 | } |
| 5963 | } |
| 5964 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 5965 | typedef int (*set_callee_state_fn)(struct bpf_verifier_env *env, |
| 5966 | struct bpf_func_state *caller, |
| 5967 | struct bpf_func_state *callee, |
| 5968 | int insn_idx); |
| 5969 | |
| 5970 | static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 5971 | int *insn_idx, int subprog, |
| 5972 | set_callee_state_fn set_callee_state_cb) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5973 | { |
| 5974 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5975 | struct bpf_func_info_aux *func_info_aux; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5976 | struct bpf_func_state *caller, *callee; |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 5977 | int err; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5978 | bool is_global = false; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5979 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 5980 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5981 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 5982 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5983 | return -E2BIG; |
| 5984 | } |
| 5985 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 5986 | caller = state->frame[state->curframe]; |
| 5987 | if (state->frame[state->curframe + 1]) { |
| 5988 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 5989 | state->curframe + 1); |
| 5990 | return -EFAULT; |
| 5991 | } |
| 5992 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5993 | func_info_aux = env->prog->aux->func_info_aux; |
| 5994 | if (func_info_aux) |
| 5995 | is_global = func_info_aux[subprog].linkage == BTF_FUNC_GLOBAL; |
Martin KaFai Lau | 34747c4 | 2021-03-24 18:51:36 -0700 | [diff] [blame] | 5996 | err = btf_check_subprog_arg_match(env, subprog, caller->regs); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 5997 | if (err == -EFAULT) |
| 5998 | return err; |
| 5999 | if (is_global) { |
| 6000 | if (err) { |
| 6001 | verbose(env, "Caller passes invalid args into func#%d\n", |
| 6002 | subprog); |
| 6003 | return err; |
| 6004 | } else { |
| 6005 | if (env->log.level & BPF_LOG_LEVEL) |
| 6006 | verbose(env, |
| 6007 | "Func#%d is global and valid. Skipping.\n", |
| 6008 | subprog); |
| 6009 | clear_caller_saved_regs(env, caller->regs); |
| 6010 | |
Ilya Leoshkevich | 45159b2 | 2021-02-12 05:04:08 +0100 | [diff] [blame] | 6011 | /* All global functions return a 64-bit SCALAR_VALUE */ |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6012 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
Ilya Leoshkevich | 45159b2 | 2021-02-12 05:04:08 +0100 | [diff] [blame] | 6013 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6014 | |
| 6015 | /* continue with next insn after call */ |
| 6016 | return 0; |
| 6017 | } |
| 6018 | } |
| 6019 | |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6020 | if (insn->code == (BPF_JMP | BPF_CALL) && |
| 6021 | insn->imm == BPF_FUNC_timer_set_callback) { |
| 6022 | struct bpf_verifier_state *async_cb; |
| 6023 | |
| 6024 | /* there is no real recursion here. timer callbacks are async */ |
Alexei Starovoitov | 7ddc80a | 2021-07-14 17:54:15 -0700 | [diff] [blame] | 6025 | env->subprog_info[subprog].is_async_cb = true; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6026 | async_cb = push_async_cb(env, env->subprog_info[subprog].start, |
| 6027 | *insn_idx, subprog); |
| 6028 | if (!async_cb) |
| 6029 | return -EFAULT; |
| 6030 | callee = async_cb->frame[0]; |
| 6031 | callee->async_entry_cnt = caller->async_entry_cnt + 1; |
| 6032 | |
| 6033 | /* Convert bpf_timer_set_callback() args into timer callback args */ |
| 6034 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
| 6035 | if (err) |
| 6036 | return err; |
| 6037 | |
| 6038 | clear_caller_saved_regs(env, caller->regs); |
| 6039 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 6040 | caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 6041 | /* continue with next insn after call */ |
| 6042 | return 0; |
| 6043 | } |
| 6044 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6045 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 6046 | if (!callee) |
| 6047 | return -ENOMEM; |
| 6048 | state->frame[state->curframe + 1] = callee; |
| 6049 | |
| 6050 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 6051 | * into its own stack before reading from it. |
| 6052 | * callee can read/write into caller's stack |
| 6053 | */ |
| 6054 | init_func_state(env, callee, |
| 6055 | /* remember the callsite, it will be used by bpf_exit */ |
| 6056 | *insn_idx /* callsite */, |
| 6057 | state->curframe + 1 /* frameno within this callchain */, |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 6058 | subprog /* subprog number within this prog */); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6059 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6060 | /* Transfer references to the callee */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 6061 | err = copy_reference_state(callee, caller); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6062 | if (err) |
| 6063 | return err; |
| 6064 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6065 | err = set_callee_state_cb(env, caller, callee, *insn_idx); |
| 6066 | if (err) |
| 6067 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6068 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 6069 | clear_caller_saved_regs(env, caller->regs); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6070 | |
| 6071 | /* only increment it after check_reg_arg() finished */ |
| 6072 | state->curframe++; |
| 6073 | |
| 6074 | /* and go analyze first insn of the callee */ |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6075 | *insn_idx = env->subprog_info[subprog].start - 1; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6076 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6077 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6078 | verbose(env, "caller:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6079 | print_verifier_state(env, caller, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6080 | verbose(env, "callee:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6081 | print_verifier_state(env, callee, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6082 | } |
| 6083 | return 0; |
| 6084 | } |
| 6085 | |
Yonghong Song | 314ee05 | 2021-02-26 12:49:27 -0800 | [diff] [blame] | 6086 | int map_set_for_each_callback_args(struct bpf_verifier_env *env, |
| 6087 | struct bpf_func_state *caller, |
| 6088 | struct bpf_func_state *callee) |
| 6089 | { |
| 6090 | /* bpf_for_each_map_elem(struct bpf_map *map, void *callback_fn, |
| 6091 | * void *callback_ctx, u64 flags); |
| 6092 | * callback_fn(struct bpf_map *map, void *key, void *value, |
| 6093 | * void *callback_ctx); |
| 6094 | */ |
| 6095 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; |
| 6096 | |
| 6097 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; |
| 6098 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6099 | callee->regs[BPF_REG_2].map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6100 | |
| 6101 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; |
| 6102 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); |
| 6103 | callee->regs[BPF_REG_3].map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6104 | |
| 6105 | /* pointer to stack or null */ |
| 6106 | callee->regs[BPF_REG_4] = caller->regs[BPF_REG_3]; |
| 6107 | |
| 6108 | /* unused */ |
| 6109 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6110 | return 0; |
| 6111 | } |
| 6112 | |
Yonghong Song | 1435137 | 2021-02-26 12:49:23 -0800 | [diff] [blame] | 6113 | static int set_callee_state(struct bpf_verifier_env *env, |
| 6114 | struct bpf_func_state *caller, |
| 6115 | struct bpf_func_state *callee, int insn_idx) |
| 6116 | { |
| 6117 | int i; |
| 6118 | |
| 6119 | /* copy r1 - r5 args that callee can access. The copy includes parent |
| 6120 | * pointers, which connects us up to the liveness chain |
| 6121 | */ |
| 6122 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 6123 | callee->regs[i] = caller->regs[i]; |
| 6124 | return 0; |
| 6125 | } |
| 6126 | |
| 6127 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 6128 | int *insn_idx) |
| 6129 | { |
| 6130 | int subprog, target_insn; |
| 6131 | |
| 6132 | target_insn = *insn_idx + insn->imm + 1; |
| 6133 | subprog = find_subprog(env, target_insn); |
| 6134 | if (subprog < 0) { |
| 6135 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 6136 | target_insn); |
| 6137 | return -EFAULT; |
| 6138 | } |
| 6139 | |
| 6140 | return __check_func_call(env, insn, insn_idx, subprog, set_callee_state); |
| 6141 | } |
| 6142 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6143 | static int set_map_elem_callback_state(struct bpf_verifier_env *env, |
| 6144 | struct bpf_func_state *caller, |
| 6145 | struct bpf_func_state *callee, |
| 6146 | int insn_idx) |
| 6147 | { |
| 6148 | struct bpf_insn_aux_data *insn_aux = &env->insn_aux_data[insn_idx]; |
| 6149 | struct bpf_map *map; |
| 6150 | int err; |
| 6151 | |
| 6152 | if (bpf_map_ptr_poisoned(insn_aux)) { |
| 6153 | verbose(env, "tail_call abusing map_ptr\n"); |
| 6154 | return -EINVAL; |
| 6155 | } |
| 6156 | |
| 6157 | map = BPF_MAP_PTR(insn_aux->map_ptr_state); |
| 6158 | if (!map->ops->map_set_for_each_callback_args || |
| 6159 | !map->ops->map_for_each_callback) { |
| 6160 | verbose(env, "callback function not allowed for map\n"); |
| 6161 | return -ENOTSUPP; |
| 6162 | } |
| 6163 | |
| 6164 | err = map->ops->map_set_for_each_callback_args(env, caller, callee); |
| 6165 | if (err) |
| 6166 | return err; |
| 6167 | |
| 6168 | callee->in_callback_fn = true; |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6172 | static int set_loop_callback_state(struct bpf_verifier_env *env, |
| 6173 | struct bpf_func_state *caller, |
| 6174 | struct bpf_func_state *callee, |
| 6175 | int insn_idx) |
| 6176 | { |
| 6177 | /* bpf_loop(u32 nr_loops, void *callback_fn, void *callback_ctx, |
| 6178 | * u64 flags); |
| 6179 | * callback_fn(u32 index, void *callback_ctx); |
| 6180 | */ |
| 6181 | callee->regs[BPF_REG_1].type = SCALAR_VALUE; |
| 6182 | callee->regs[BPF_REG_2] = caller->regs[BPF_REG_3]; |
| 6183 | |
| 6184 | /* unused */ |
| 6185 | __mark_reg_not_init(env, &callee->regs[BPF_REG_3]); |
| 6186 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6187 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6188 | |
| 6189 | callee->in_callback_fn = true; |
| 6190 | return 0; |
| 6191 | } |
| 6192 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6193 | static int set_timer_callback_state(struct bpf_verifier_env *env, |
| 6194 | struct bpf_func_state *caller, |
| 6195 | struct bpf_func_state *callee, |
| 6196 | int insn_idx) |
| 6197 | { |
| 6198 | struct bpf_map *map_ptr = caller->regs[BPF_REG_1].map_ptr; |
| 6199 | |
| 6200 | /* bpf_timer_set_callback(struct bpf_timer *timer, void *callback_fn); |
| 6201 | * callback_fn(struct bpf_map *map, void *key, void *value); |
| 6202 | */ |
| 6203 | callee->regs[BPF_REG_1].type = CONST_PTR_TO_MAP; |
| 6204 | __mark_reg_known_zero(&callee->regs[BPF_REG_1]); |
| 6205 | callee->regs[BPF_REG_1].map_ptr = map_ptr; |
| 6206 | |
| 6207 | callee->regs[BPF_REG_2].type = PTR_TO_MAP_KEY; |
| 6208 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6209 | callee->regs[BPF_REG_2].map_ptr = map_ptr; |
| 6210 | |
| 6211 | callee->regs[BPF_REG_3].type = PTR_TO_MAP_VALUE; |
| 6212 | __mark_reg_known_zero(&callee->regs[BPF_REG_3]); |
| 6213 | callee->regs[BPF_REG_3].map_ptr = map_ptr; |
| 6214 | |
| 6215 | /* unused */ |
| 6216 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6217 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 6218 | callee->in_async_callback_fn = true; |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6219 | return 0; |
| 6220 | } |
| 6221 | |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6222 | static int set_find_vma_callback_state(struct bpf_verifier_env *env, |
| 6223 | struct bpf_func_state *caller, |
| 6224 | struct bpf_func_state *callee, |
| 6225 | int insn_idx) |
| 6226 | { |
| 6227 | /* bpf_find_vma(struct task_struct *task, u64 addr, |
| 6228 | * void *callback_fn, void *callback_ctx, u64 flags) |
| 6229 | * (callback_fn)(struct task_struct *task, |
| 6230 | * struct vm_area_struct *vma, void *callback_ctx); |
| 6231 | */ |
| 6232 | callee->regs[BPF_REG_1] = caller->regs[BPF_REG_1]; |
| 6233 | |
| 6234 | callee->regs[BPF_REG_2].type = PTR_TO_BTF_ID; |
| 6235 | __mark_reg_known_zero(&callee->regs[BPF_REG_2]); |
| 6236 | callee->regs[BPF_REG_2].btf = btf_vmlinux; |
Song Liu | d19ddb4 | 2021-11-12 07:02:43 -0800 | [diff] [blame] | 6237 | callee->regs[BPF_REG_2].btf_id = btf_tracing_ids[BTF_TRACING_TYPE_VMA], |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6238 | |
| 6239 | /* pointer to stack or null */ |
| 6240 | callee->regs[BPF_REG_3] = caller->regs[BPF_REG_4]; |
| 6241 | |
| 6242 | /* unused */ |
| 6243 | __mark_reg_not_init(env, &callee->regs[BPF_REG_4]); |
| 6244 | __mark_reg_not_init(env, &callee->regs[BPF_REG_5]); |
| 6245 | callee->in_callback_fn = true; |
| 6246 | return 0; |
| 6247 | } |
| 6248 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6249 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 6250 | { |
| 6251 | struct bpf_verifier_state *state = env->cur_state; |
| 6252 | struct bpf_func_state *caller, *callee; |
| 6253 | struct bpf_reg_state *r0; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6254 | int err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6255 | |
| 6256 | callee = state->frame[state->curframe]; |
| 6257 | r0 = &callee->regs[BPF_REG_0]; |
| 6258 | if (r0->type == PTR_TO_STACK) { |
| 6259 | /* technically it's ok to return caller's stack pointer |
| 6260 | * (or caller's caller's pointer) back to the caller, |
| 6261 | * since these pointers are valid. Only current stack |
| 6262 | * pointer will be invalid as soon as function exits, |
| 6263 | * but let's be conservative |
| 6264 | */ |
| 6265 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 6266 | return -EINVAL; |
| 6267 | } |
| 6268 | |
| 6269 | state->curframe--; |
| 6270 | caller = state->frame[state->curframe]; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6271 | if (callee->in_callback_fn) { |
| 6272 | /* enforce R0 return value range [0, 1]. */ |
| 6273 | struct tnum range = tnum_range(0, 1); |
| 6274 | |
| 6275 | if (r0->type != SCALAR_VALUE) { |
| 6276 | verbose(env, "R0 not a scalar value\n"); |
| 6277 | return -EACCES; |
| 6278 | } |
| 6279 | if (!tnum_in(range, r0->var_off)) { |
| 6280 | verbose_invalid_scalar(env, r0, &range, "callback return", "R0"); |
| 6281 | return -EINVAL; |
| 6282 | } |
| 6283 | } else { |
| 6284 | /* return to the caller whatever r0 had in the callee */ |
| 6285 | caller->regs[BPF_REG_0] = *r0; |
| 6286 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6287 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6288 | /* Transfer references to the caller */ |
Lorenz Bauer | c69431a | 2021-04-29 14:46:54 +0100 | [diff] [blame] | 6289 | err = copy_reference_state(caller, callee); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6290 | if (err) |
| 6291 | return err; |
| 6292 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6293 | *insn_idx = callee->callsite + 1; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 6294 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6295 | verbose(env, "returning from callee:\n"); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6296 | print_verifier_state(env, callee, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6297 | verbose(env, "to caller at %d:\n", *insn_idx); |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 6298 | print_verifier_state(env, caller, true); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 6299 | } |
| 6300 | /* clear everything in the callee */ |
| 6301 | free_func_state(callee); |
| 6302 | state->frame[state->curframe + 1] = NULL; |
| 6303 | return 0; |
| 6304 | } |
| 6305 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6306 | static void do_refine_retval_range(struct bpf_reg_state *regs, int ret_type, |
| 6307 | int func_id, |
| 6308 | struct bpf_call_arg_meta *meta) |
| 6309 | { |
| 6310 | struct bpf_reg_state *ret_reg = ®s[BPF_REG_0]; |
| 6311 | |
| 6312 | if (ret_type != RET_INTEGER || |
| 6313 | (func_id != BPF_FUNC_get_stack && |
Dave Marchevsky | fd0b88f | 2021-04-16 13:47:02 -0700 | [diff] [blame] | 6314 | func_id != BPF_FUNC_get_task_stack && |
Daniel Borkmann | 47cc0ed | 2020-05-15 12:11:17 +0200 | [diff] [blame] | 6315 | func_id != BPF_FUNC_probe_read_str && |
| 6316 | func_id != BPF_FUNC_probe_read_kernel_str && |
| 6317 | func_id != BPF_FUNC_probe_read_user_str)) |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6318 | return; |
| 6319 | |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 6320 | ret_reg->smax_value = meta->msize_max_value; |
John Fastabend | fa123ac | 2020-03-30 14:36:59 -0700 | [diff] [blame] | 6321 | ret_reg->s32_max_value = meta->msize_max_value; |
Alexei Starovoitov | b0270958 | 2020-12-08 19:01:51 +0100 | [diff] [blame] | 6322 | ret_reg->smin_value = -MAX_ERRNO; |
| 6323 | ret_reg->s32_min_value = -MAX_ERRNO; |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6324 | __reg_deduce_bounds(ret_reg); |
| 6325 | __reg_bound_offset(ret_reg); |
John Fastabend | 1006050 | 2020-03-30 14:36:19 -0700 | [diff] [blame] | 6326 | __update_reg_bounds(ret_reg); |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6327 | } |
| 6328 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6329 | static int |
| 6330 | record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 6331 | int func_id, int insn_idx) |
| 6332 | { |
| 6333 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6334 | struct bpf_map *map = meta->map_ptr; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6335 | |
| 6336 | if (func_id != BPF_FUNC_tail_call && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6337 | func_id != BPF_FUNC_map_lookup_elem && |
| 6338 | func_id != BPF_FUNC_map_update_elem && |
Mauricio Vasquez B | f1a2e44 | 2018-10-18 15:16:25 +0200 | [diff] [blame] | 6339 | func_id != BPF_FUNC_map_delete_elem && |
| 6340 | func_id != BPF_FUNC_map_push_elem && |
| 6341 | func_id != BPF_FUNC_map_pop_elem && |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6342 | func_id != BPF_FUNC_map_peek_elem && |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 6343 | func_id != BPF_FUNC_for_each_map_elem && |
| 6344 | func_id != BPF_FUNC_redirect_map) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6345 | return 0; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 6346 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6347 | if (map == NULL) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6348 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 6349 | return -EINVAL; |
| 6350 | } |
| 6351 | |
Daniel Borkmann | 591fe98 | 2019-04-09 23:20:05 +0200 | [diff] [blame] | 6352 | /* In case of read-only, some additional restrictions |
| 6353 | * need to be applied in order to prevent altering the |
| 6354 | * state of the map from program side. |
| 6355 | */ |
| 6356 | if ((map->map_flags & BPF_F_RDONLY_PROG) && |
| 6357 | (func_id == BPF_FUNC_map_delete_elem || |
| 6358 | func_id == BPF_FUNC_map_update_elem || |
| 6359 | func_id == BPF_FUNC_map_push_elem || |
| 6360 | func_id == BPF_FUNC_map_pop_elem)) { |
| 6361 | verbose(env, "write into map forbidden\n"); |
| 6362 | return -EACCES; |
| 6363 | } |
| 6364 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6365 | if (!BPF_MAP_PTR(aux->map_ptr_state)) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6366 | bpf_map_ptr_store(aux, meta->map_ptr, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 6367 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6368 | else if (BPF_MAP_PTR(aux->map_ptr_state) != meta->map_ptr) |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6369 | bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON, |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 6370 | !meta->map_ptr->bypass_spec_v1); |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6371 | return 0; |
| 6372 | } |
| 6373 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6374 | static int |
| 6375 | record_func_key(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta, |
| 6376 | int func_id, int insn_idx) |
| 6377 | { |
| 6378 | struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx]; |
| 6379 | struct bpf_reg_state *regs = cur_regs(env), *reg; |
| 6380 | struct bpf_map *map = meta->map_ptr; |
| 6381 | struct tnum range; |
| 6382 | u64 val; |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 6383 | int err; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6384 | |
| 6385 | if (func_id != BPF_FUNC_tail_call) |
| 6386 | return 0; |
| 6387 | if (!map || map->map_type != BPF_MAP_TYPE_PROG_ARRAY) { |
| 6388 | verbose(env, "kernel subsystem misconfigured verifier\n"); |
| 6389 | return -EINVAL; |
| 6390 | } |
| 6391 | |
| 6392 | range = tnum_range(0, map->max_entries - 1); |
| 6393 | reg = ®s[BPF_REG_3]; |
| 6394 | |
| 6395 | if (!register_is_const(reg) || !tnum_in(range, reg->var_off)) { |
| 6396 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 6397 | return 0; |
| 6398 | } |
| 6399 | |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 6400 | err = mark_chain_precision(env, BPF_REG_3); |
| 6401 | if (err) |
| 6402 | return err; |
| 6403 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6404 | val = reg->var_off.value; |
| 6405 | if (bpf_map_key_unseen(aux)) |
| 6406 | bpf_map_key_store(aux, val); |
| 6407 | else if (!bpf_map_key_poisoned(aux) && |
| 6408 | bpf_map_key_immediate(aux) != val) |
| 6409 | bpf_map_key_store(aux, BPF_MAP_KEY_POISON); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6413 | static int check_reference_leak(struct bpf_verifier_env *env) |
| 6414 | { |
| 6415 | struct bpf_func_state *state = cur_func(env); |
| 6416 | int i; |
| 6417 | |
| 6418 | for (i = 0; i < state->acquired_refs; i++) { |
| 6419 | verbose(env, "Unreleased reference id=%d alloc_insn=%d\n", |
| 6420 | state->refs[i].id, state->refs[i].insn_idx); |
| 6421 | } |
| 6422 | return state->acquired_refs ? -EINVAL : 0; |
| 6423 | } |
| 6424 | |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6425 | static int check_bpf_snprintf_call(struct bpf_verifier_env *env, |
| 6426 | struct bpf_reg_state *regs) |
| 6427 | { |
| 6428 | struct bpf_reg_state *fmt_reg = ®s[BPF_REG_3]; |
| 6429 | struct bpf_reg_state *data_len_reg = ®s[BPF_REG_5]; |
| 6430 | struct bpf_map *fmt_map = fmt_reg->map_ptr; |
| 6431 | int err, fmt_map_off, num_args; |
| 6432 | u64 fmt_addr; |
| 6433 | char *fmt; |
| 6434 | |
| 6435 | /* data must be an array of u64 */ |
| 6436 | if (data_len_reg->var_off.value % 8) |
| 6437 | return -EINVAL; |
| 6438 | num_args = data_len_reg->var_off.value / 8; |
| 6439 | |
| 6440 | /* fmt being ARG_PTR_TO_CONST_STR guarantees that var_off is const |
| 6441 | * and map_direct_value_addr is set. |
| 6442 | */ |
| 6443 | fmt_map_off = fmt_reg->off + fmt_reg->var_off.value; |
| 6444 | err = fmt_map->ops->map_direct_value_addr(fmt_map, &fmt_addr, |
| 6445 | fmt_map_off); |
Florent Revest | 8e8ee10 | 2021-04-23 01:55:42 +0200 | [diff] [blame] | 6446 | if (err) { |
| 6447 | verbose(env, "verifier bug\n"); |
| 6448 | return -EFAULT; |
| 6449 | } |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6450 | fmt = (char *)(long)fmt_addr + fmt_map_off; |
| 6451 | |
| 6452 | /* We are also guaranteed that fmt+fmt_map_off is NULL terminated, we |
| 6453 | * can focus on validating the format specifiers. |
| 6454 | */ |
Florent Revest | 48cac3f | 2021-04-27 19:43:13 +0200 | [diff] [blame] | 6455 | err = bpf_bprintf_prepare(fmt, UINT_MAX, NULL, NULL, num_args); |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6456 | if (err < 0) |
| 6457 | verbose(env, "Invalid format string\n"); |
| 6458 | |
| 6459 | return err; |
| 6460 | } |
| 6461 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6462 | static int check_get_func_ip(struct bpf_verifier_env *env) |
| 6463 | { |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6464 | enum bpf_prog_type type = resolve_prog_type(env->prog); |
| 6465 | int func_id = BPF_FUNC_get_func_ip; |
| 6466 | |
| 6467 | if (type == BPF_PROG_TYPE_TRACING) { |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 6468 | if (!bpf_prog_has_trampoline(env->prog)) { |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6469 | verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n", |
| 6470 | func_id_name(func_id), func_id); |
| 6471 | return -ENOTSUPP; |
| 6472 | } |
| 6473 | return 0; |
Jiri Olsa | 9ffd9f3 | 2021-07-14 11:43:56 +0200 | [diff] [blame] | 6474 | } else if (type == BPF_PROG_TYPE_KPROBE) { |
| 6475 | return 0; |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6476 | } |
| 6477 | |
| 6478 | verbose(env, "func %s#%d not supported for program type %d\n", |
| 6479 | func_id_name(func_id), func_id, type); |
| 6480 | return -ENOTSUPP; |
| 6481 | } |
| 6482 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6483 | static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 6484 | int *insn_idx_p) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6485 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6486 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6487 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6488 | struct bpf_call_arg_meta meta; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6489 | int insn_idx = *insn_idx_p; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6490 | bool changes_data; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6491 | int i, err, func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6492 | |
| 6493 | /* find function prototype */ |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6494 | func_id = insn->imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6495 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6496 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 6497 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6498 | return -EINVAL; |
| 6499 | } |
| 6500 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 6501 | if (env->ops->get_func_proto) |
Andrey Ignatov | 5e43f89 | 2018-03-30 15:08:00 -0700 | [diff] [blame] | 6502 | fn = env->ops->get_func_proto(func_id, env->prog); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6503 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6504 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 6505 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6506 | return -EINVAL; |
| 6507 | } |
| 6508 | |
| 6509 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 6510 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Daniel Borkmann | 3fe2867 | 2018-06-02 23:06:33 +0200 | [diff] [blame] | 6511 | verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6512 | return -EINVAL; |
| 6513 | } |
| 6514 | |
Jiri Olsa | eae2e83 | 2020-08-25 21:21:19 +0200 | [diff] [blame] | 6515 | if (fn->allowed && !fn->allowed(env->prog)) { |
| 6516 | verbose(env, "helper call is not allowed in probe\n"); |
| 6517 | return -EINVAL; |
| 6518 | } |
| 6519 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 6520 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 6521 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 6522 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 6523 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 6524 | func_id_name(func_id), func_id); |
| 6525 | return -EINVAL; |
| 6526 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6527 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6528 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 6529 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6530 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6531 | err = check_func_proto(fn, func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6532 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6533 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 6534 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6535 | return err; |
| 6536 | } |
| 6537 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 6538 | meta.func_id = func_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6539 | /* check args */ |
Dmitrii Banshchikov | 523a4cf | 2021-02-26 00:26:29 +0400 | [diff] [blame] | 6540 | for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6541 | err = check_func_arg(env, i, &meta, fn); |
Alexei Starovoitov | a7658e1 | 2019-10-15 20:25:04 -0700 | [diff] [blame] | 6542 | if (err) |
| 6543 | return err; |
| 6544 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6545 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 6546 | err = record_func_map(env, &meta, func_id, insn_idx); |
| 6547 | if (err) |
| 6548 | return err; |
| 6549 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 6550 | err = record_func_key(env, &meta, func_id, insn_idx); |
| 6551 | if (err) |
| 6552 | return err; |
| 6553 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6554 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 6555 | * is inferred from register state. |
| 6556 | */ |
| 6557 | for (i = 0; i < meta.access_size; i++) { |
Daniel Borkmann | ca36960 | 2018-02-23 22:29:05 +0100 | [diff] [blame] | 6558 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, |
| 6559 | BPF_WRITE, -1, false); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 6560 | if (err) |
| 6561 | return err; |
| 6562 | } |
| 6563 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6564 | if (is_release_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6565 | err = release_reference(env, meta.ref_obj_id); |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6566 | if (err) { |
| 6567 | verbose(env, "func %s#%d reference has not been acquired before\n", |
| 6568 | func_id_name(func_id), func_id); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6569 | return err; |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 6570 | } |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 6571 | } |
| 6572 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 6573 | regs = cur_regs(env); |
Roman Gushchin | cd33943 | 2018-08-02 14:27:24 -0700 | [diff] [blame] | 6574 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6575 | switch (func_id) { |
| 6576 | case BPF_FUNC_tail_call: |
| 6577 | err = check_reference_leak(env); |
| 6578 | if (err) { |
| 6579 | verbose(env, "tail_call would lead to reference leak\n"); |
| 6580 | return err; |
| 6581 | } |
| 6582 | break; |
| 6583 | case BPF_FUNC_get_local_storage: |
| 6584 | /* check that flags argument in get_local_storage(map, flags) is 0, |
| 6585 | * this is required because get_local_storage() can't return an error. |
| 6586 | */ |
| 6587 | if (!register_is_null(®s[BPF_REG_2])) { |
| 6588 | verbose(env, "get_local_storage() doesn't support non-zero flags\n"); |
| 6589 | return -EINVAL; |
| 6590 | } |
| 6591 | break; |
| 6592 | case BPF_FUNC_for_each_map_elem: |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 6593 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6594 | set_map_elem_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6595 | break; |
| 6596 | case BPF_FUNC_timer_set_callback: |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 6597 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6598 | set_timer_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6599 | break; |
| 6600 | case BPF_FUNC_find_vma: |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6601 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6602 | set_find_vma_callback_state); |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6603 | break; |
| 6604 | case BPF_FUNC_snprintf: |
| 6605 | err = check_bpf_snprintf_call(env, regs); |
| 6606 | break; |
| 6607 | case BPF_FUNC_loop: |
| 6608 | err = __check_func_call(env, insn, insn_idx_p, meta.subprogno, |
| 6609 | set_loop_callback_state); |
| 6610 | break; |
Song Liu | 7c7e3d3 | 2021-11-05 16:23:29 -0700 | [diff] [blame] | 6611 | } |
| 6612 | |
Joanne Koong | e6f2dd0 | 2021-11-29 19:06:19 -0800 | [diff] [blame] | 6613 | if (err) |
| 6614 | return err; |
Florent Revest | 7b15523 | 2021-04-19 17:52:40 +0200 | [diff] [blame] | 6615 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6616 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6617 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6618 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6619 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 6620 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6621 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 6622 | /* helper call returns 64-bit value. */ |
| 6623 | regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 6624 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 6625 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6626 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6627 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6628 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6629 | } else if (fn->ret_type == RET_VOID) { |
| 6630 | regs[BPF_REG_0].type = NOT_INIT; |
Roman Gushchin | 3e6a4b3 | 2018-08-02 14:27:22 -0700 | [diff] [blame] | 6631 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL || |
| 6632 | fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 6633 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6634 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6635 | /* remember map_ptr, so that check_map_access() |
| 6636 | * can check 'value_size' boundary of memory access |
| 6637 | * to map element returned from bpf_map_lookup_elem() |
| 6638 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6639 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6640 | verbose(env, |
| 6641 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6642 | return -EINVAL; |
| 6643 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 6644 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Alexei Starovoitov | 3e8ce29 | 2021-07-14 17:54:11 -0700 | [diff] [blame] | 6645 | regs[BPF_REG_0].map_uid = meta.map_uid; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 6646 | if (fn->ret_type == RET_PTR_TO_MAP_VALUE) { |
| 6647 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE; |
Alexei Starovoitov | e16d2f1 | 2019-01-31 15:40:05 -0800 | [diff] [blame] | 6648 | if (map_value_has_spin_lock(meta.map_ptr)) |
| 6649 | regs[BPF_REG_0].id = ++env->id_gen; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 6650 | } else { |
| 6651 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Daniel Borkmann | 4d31f30 | 2018-11-01 00:05:53 +0100 | [diff] [blame] | 6652 | } |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 6653 | } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) { |
| 6654 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 6655 | regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL; |
Lorenz Bauer | 85a51f8 | 2019-03-22 09:54:00 +0800 | [diff] [blame] | 6656 | } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) { |
| 6657 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 6658 | regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 6659 | } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) { |
| 6660 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 6661 | regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 6662 | } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) { |
| 6663 | mark_reg_known_zero(env, regs, BPF_REG_0); |
| 6664 | regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL; |
Andrii Nakryiko | 457f443 | 2020-05-29 00:54:20 -0700 | [diff] [blame] | 6665 | regs[BPF_REG_0].mem_size = meta.mem_size; |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 6666 | } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL || |
| 6667 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6668 | const struct btf_type *t; |
| 6669 | |
| 6670 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6671 | t = btf_type_skip_modifiers(meta.ret_btf, meta.ret_btf_id, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6672 | if (!btf_type_is_struct(t)) { |
| 6673 | u32 tsize; |
| 6674 | const struct btf_type *ret; |
| 6675 | const char *tname; |
| 6676 | |
| 6677 | /* resolve the type size of ksym. */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6678 | ret = btf_resolve_size(meta.ret_btf, t, &tsize); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6679 | if (IS_ERR(ret)) { |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6680 | tname = btf_name_by_offset(meta.ret_btf, t->name_off); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6681 | verbose(env, "unable to resolve the size of type '%s': %ld\n", |
| 6682 | tname, PTR_ERR(ret)); |
| 6683 | return -EINVAL; |
| 6684 | } |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 6685 | regs[BPF_REG_0].type = |
| 6686 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 6687 | PTR_TO_MEM : PTR_TO_MEM_OR_NULL; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6688 | regs[BPF_REG_0].mem_size = tsize; |
| 6689 | } else { |
Hao Luo | 63d9b80 | 2020-09-29 16:50:48 -0700 | [diff] [blame] | 6690 | regs[BPF_REG_0].type = |
| 6691 | fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ? |
| 6692 | PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL; |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6693 | regs[BPF_REG_0].btf = meta.ret_btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 6694 | regs[BPF_REG_0].btf_id = meta.ret_btf_id; |
| 6695 | } |
KP Singh | 3ca1032 | 2020-11-06 10:37:43 +0000 | [diff] [blame] | 6696 | } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL || |
| 6697 | fn->ret_type == RET_PTR_TO_BTF_ID) { |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6698 | int ret_btf_id; |
| 6699 | |
| 6700 | mark_reg_known_zero(env, regs, BPF_REG_0); |
KP Singh | 3ca1032 | 2020-11-06 10:37:43 +0000 | [diff] [blame] | 6701 | regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ? |
| 6702 | PTR_TO_BTF_ID : |
| 6703 | PTR_TO_BTF_ID_OR_NULL; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6704 | ret_btf_id = *fn->ret_btf_id; |
| 6705 | if (ret_btf_id == 0) { |
| 6706 | verbose(env, "invalid return type %d of func %s#%d\n", |
| 6707 | fn->ret_type, func_id_name(func_id), func_id); |
| 6708 | return -EINVAL; |
| 6709 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 6710 | /* current BPF helper definitions are only coming from |
| 6711 | * built-in code with type IDs from vmlinux BTF |
| 6712 | */ |
| 6713 | regs[BPF_REG_0].btf = btf_vmlinux; |
Yonghong Song | af7ec13 | 2020-06-23 16:08:09 -0700 | [diff] [blame] | 6714 | regs[BPF_REG_0].btf_id = ret_btf_id; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6715 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6716 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 6717 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 6718 | return -EINVAL; |
| 6719 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 6720 | |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 6721 | if (reg_type_may_be_null(regs[BPF_REG_0].type)) |
| 6722 | regs[BPF_REG_0].id = ++env->id_gen; |
| 6723 | |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 6724 | if (is_ptr_cast_function(func_id)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6725 | /* For release_reference() */ |
| 6726 | regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id; |
Jakub Sitnicki | 64d8529 | 2020-04-29 20:11:52 +0200 | [diff] [blame] | 6727 | } else if (is_acquire_function(func_id, meta.map_ptr)) { |
Lorenz Bauer | 0f3adc2 | 2019-03-22 09:53:59 +0800 | [diff] [blame] | 6728 | int id = acquire_reference_state(env, insn_idx); |
| 6729 | |
| 6730 | if (id < 0) |
| 6731 | return id; |
| 6732 | /* For mark_ptr_or_null_reg() */ |
| 6733 | regs[BPF_REG_0].id = id; |
| 6734 | /* For release_reference() */ |
| 6735 | regs[BPF_REG_0].ref_obj_id = id; |
| 6736 | } |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 6737 | |
Yonghong Song | 849fa50 | 2018-04-28 22:28:09 -0700 | [diff] [blame] | 6738 | do_refine_retval_range(regs, fn->ret_type, func_id, &meta); |
| 6739 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 6740 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 6741 | if (err) |
| 6742 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 6743 | |
Song Liu | fa28dcb | 2020-06-29 23:28:44 -0700 | [diff] [blame] | 6744 | if ((func_id == BPF_FUNC_get_stack || |
| 6745 | func_id == BPF_FUNC_get_task_stack) && |
| 6746 | !env->prog->has_callchain_buf) { |
Yonghong Song | c195651e | 2018-04-28 22:28:08 -0700 | [diff] [blame] | 6747 | const char *err_str; |
| 6748 | |
| 6749 | #ifdef CONFIG_PERF_EVENTS |
| 6750 | err = get_callchain_buffers(sysctl_perf_event_max_stack); |
| 6751 | err_str = "cannot get callchain buffer for func %s#%d\n"; |
| 6752 | #else |
| 6753 | err = -ENOTSUPP; |
| 6754 | err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n"; |
| 6755 | #endif |
| 6756 | if (err) { |
| 6757 | verbose(env, err_str, func_id_name(func_id), func_id); |
| 6758 | return err; |
| 6759 | } |
| 6760 | |
| 6761 | env->prog->has_callchain_buf = true; |
| 6762 | } |
| 6763 | |
Song Liu | 5d99cb2c | 2020-07-23 11:06:45 -0700 | [diff] [blame] | 6764 | if (func_id == BPF_FUNC_get_stackid || func_id == BPF_FUNC_get_stack) |
| 6765 | env->prog->call_get_stack = true; |
| 6766 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 6767 | if (func_id == BPF_FUNC_get_func_ip) { |
| 6768 | if (check_get_func_ip(env)) |
| 6769 | return -ENOTSUPP; |
| 6770 | env->prog->call_get_func_ip = true; |
| 6771 | } |
| 6772 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 6773 | if (changes_data) |
| 6774 | clear_all_pkt_pointers(env); |
| 6775 | return 0; |
| 6776 | } |
| 6777 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6778 | /* mark_btf_func_reg_size() is used when the reg size is determined by |
| 6779 | * the BTF func_proto's return value size and argument. |
| 6780 | */ |
| 6781 | static void mark_btf_func_reg_size(struct bpf_verifier_env *env, u32 regno, |
| 6782 | size_t reg_size) |
| 6783 | { |
| 6784 | struct bpf_reg_state *reg = &cur_regs(env)[regno]; |
| 6785 | |
| 6786 | if (regno == BPF_REG_0) { |
| 6787 | /* Function return value */ |
| 6788 | reg->live |= REG_LIVE_WRITTEN; |
| 6789 | reg->subreg_def = reg_size == sizeof(u64) ? |
| 6790 | DEF_NOT_SUBREG : env->insn_idx + 1; |
| 6791 | } else { |
| 6792 | /* Function argument */ |
| 6793 | if (reg_size == sizeof(u64)) { |
| 6794 | mark_insn_zext(env, reg); |
| 6795 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 6796 | } else { |
| 6797 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ32); |
| 6798 | } |
| 6799 | } |
| 6800 | } |
| 6801 | |
| 6802 | static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn) |
| 6803 | { |
| 6804 | const struct btf_type *t, *func, *func_proto, *ptr_type; |
| 6805 | struct bpf_reg_state *regs = cur_regs(env); |
| 6806 | const char *func_name, *ptr_type_name; |
| 6807 | u32 i, nargs, func_id, ptr_type_id; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6808 | struct module *btf_mod = NULL; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6809 | const struct btf_param *args; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6810 | struct btf *desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6811 | int err; |
| 6812 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 6813 | /* skip for now, but return error when we find this in fixup_kfunc_call */ |
| 6814 | if (!insn->imm) |
| 6815 | return 0; |
| 6816 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6817 | desc_btf = find_kfunc_desc_btf(env, insn->imm, insn->off, &btf_mod); |
| 6818 | if (IS_ERR(desc_btf)) |
| 6819 | return PTR_ERR(desc_btf); |
| 6820 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6821 | func_id = insn->imm; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6822 | func = btf_type_by_id(desc_btf, func_id); |
| 6823 | func_name = btf_name_by_offset(desc_btf, func->name_off); |
| 6824 | func_proto = btf_type_by_id(desc_btf, func->type); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6825 | |
| 6826 | if (!env->ops->check_kfunc_call || |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6827 | !env->ops->check_kfunc_call(func_id, btf_mod)) { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6828 | verbose(env, "calling kernel function %s is not allowed\n", |
| 6829 | func_name); |
| 6830 | return -EACCES; |
| 6831 | } |
| 6832 | |
| 6833 | /* Check the arguments */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6834 | err = btf_check_kfunc_arg_match(env, desc_btf, func_id, regs); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6835 | if (err) |
| 6836 | return err; |
| 6837 | |
| 6838 | for (i = 0; i < CALLER_SAVED_REGS; i++) |
| 6839 | mark_reg_not_init(env, regs, caller_saved[i]); |
| 6840 | |
| 6841 | /* Check return type */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6842 | t = btf_type_skip_modifiers(desc_btf, func_proto->type, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6843 | if (btf_type_is_scalar(t)) { |
| 6844 | mark_reg_unknown(env, regs, BPF_REG_0); |
| 6845 | mark_btf_func_reg_size(env, BPF_REG_0, t->size); |
| 6846 | } else if (btf_type_is_ptr(t)) { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6847 | ptr_type = btf_type_skip_modifiers(desc_btf, t->type, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6848 | &ptr_type_id); |
| 6849 | if (!btf_type_is_struct(ptr_type)) { |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6850 | ptr_type_name = btf_name_by_offset(desc_btf, |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6851 | ptr_type->name_off); |
| 6852 | verbose(env, "kernel function %s returns pointer type %s %s is not supported\n", |
| 6853 | func_name, btf_type_str(ptr_type), |
| 6854 | ptr_type_name); |
| 6855 | return -EINVAL; |
| 6856 | } |
| 6857 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6858 | regs[BPF_REG_0].btf = desc_btf; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6859 | regs[BPF_REG_0].type = PTR_TO_BTF_ID; |
| 6860 | regs[BPF_REG_0].btf_id = ptr_type_id; |
| 6861 | mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); |
| 6862 | } /* else { add_kfunc_call() ensures it is btf_type_is_void(t) } */ |
| 6863 | |
| 6864 | nargs = btf_type_vlen(func_proto); |
| 6865 | args = (const struct btf_param *)(func_proto + 1); |
| 6866 | for (i = 0; i < nargs; i++) { |
| 6867 | u32 regno = i + 1; |
| 6868 | |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 6869 | t = btf_type_skip_modifiers(desc_btf, args[i].type, NULL); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 6870 | if (btf_type_is_ptr(t)) |
| 6871 | mark_btf_func_reg_size(env, regno, sizeof(void *)); |
| 6872 | else |
| 6873 | /* scalar. ensured by btf_check_kfunc_arg_match() */ |
| 6874 | mark_btf_func_reg_size(env, regno, t->size); |
| 6875 | } |
| 6876 | |
| 6877 | return 0; |
| 6878 | } |
| 6879 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6880 | static bool signed_add_overflows(s64 a, s64 b) |
| 6881 | { |
| 6882 | /* Do the add in u64, where overflow is well-defined */ |
| 6883 | s64 res = (s64)((u64)a + (u64)b); |
| 6884 | |
| 6885 | if (b < 0) |
| 6886 | return res > a; |
| 6887 | return res < a; |
| 6888 | } |
| 6889 | |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6890 | static bool signed_add32_overflows(s32 a, s32 b) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6891 | { |
| 6892 | /* Do the add in u32, where overflow is well-defined */ |
| 6893 | s32 res = (s32)((u32)a + (u32)b); |
| 6894 | |
| 6895 | if (b < 0) |
| 6896 | return res > a; |
| 6897 | return res < a; |
| 6898 | } |
| 6899 | |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6900 | static bool signed_sub_overflows(s64 a, s64 b) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 6901 | { |
| 6902 | /* Do the sub in u64, where overflow is well-defined */ |
| 6903 | s64 res = (s64)((u64)a - (u64)b); |
| 6904 | |
| 6905 | if (b < 0) |
| 6906 | return res < a; |
| 6907 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 6908 | } |
| 6909 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6910 | static bool signed_sub32_overflows(s32 a, s32 b) |
| 6911 | { |
Daniel Borkmann | bc895e8 | 2021-01-20 00:24:24 +0100 | [diff] [blame] | 6912 | /* Do the sub in u32, where overflow is well-defined */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 6913 | s32 res = (s32)((u32)a - (u32)b); |
| 6914 | |
| 6915 | if (b < 0) |
| 6916 | return res < a; |
| 6917 | return res > a; |
| 6918 | } |
| 6919 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 6920 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 6921 | const struct bpf_reg_state *reg, |
| 6922 | enum bpf_reg_type type) |
| 6923 | { |
| 6924 | bool known = tnum_is_const(reg->var_off); |
| 6925 | s64 val = reg->var_off.value; |
| 6926 | s64 smin = reg->smin_value; |
| 6927 | |
| 6928 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 6929 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 6930 | reg_type_str[type], val); |
| 6931 | return false; |
| 6932 | } |
| 6933 | |
| 6934 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 6935 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 6936 | reg_type_str[type], reg->off); |
| 6937 | return false; |
| 6938 | } |
| 6939 | |
| 6940 | if (smin == S64_MIN) { |
| 6941 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 6942 | reg_type_str[type]); |
| 6943 | return false; |
| 6944 | } |
| 6945 | |
| 6946 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 6947 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 6948 | smin, reg_type_str[type]); |
| 6949 | return false; |
| 6950 | } |
| 6951 | |
| 6952 | return true; |
| 6953 | } |
| 6954 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6955 | static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) |
| 6956 | { |
| 6957 | return &env->insn_aux_data[env->insn_idx]; |
| 6958 | } |
| 6959 | |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 6960 | enum { |
| 6961 | REASON_BOUNDS = -1, |
| 6962 | REASON_TYPE = -2, |
| 6963 | REASON_PATHS = -3, |
| 6964 | REASON_LIMIT = -4, |
| 6965 | REASON_STACK = -5, |
| 6966 | }; |
| 6967 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6968 | static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 6969 | u32 *alu_limit, bool mask_to_left) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6970 | { |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6971 | u32 max = 0, ptr_limit = 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6972 | |
| 6973 | switch (ptr_reg->type) { |
| 6974 | case PTR_TO_STACK: |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6975 | /* Offset 0 is out-of-bounds, but acceptable start for the |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6976 | * left direction, see BPF_REG_FP. Also, unknown scalar |
| 6977 | * offset where we would need to deal with min/max bounds is |
| 6978 | * currently prohibited for unprivileged. |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6979 | */ |
| 6980 | max = MAX_BPF_STACK + mask_to_left; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6981 | ptr_limit = -(ptr_reg->var_off.value + ptr_reg->off); |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 6982 | break; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6983 | case PTR_TO_MAP_VALUE: |
Piotr Krysiuk | 1b1597e | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 6984 | max = ptr_reg->map_ptr->value_size; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 6985 | ptr_limit = (mask_to_left ? |
| 6986 | ptr_reg->smin_value : |
| 6987 | ptr_reg->umax_value) + ptr_reg->off; |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 6988 | break; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6989 | default: |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 6990 | return REASON_TYPE; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6991 | } |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 6992 | |
| 6993 | if (ptr_limit >= max) |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 6994 | return REASON_LIMIT; |
Daniel Borkmann | b658bbb | 2021-03-23 09:04:10 +0100 | [diff] [blame] | 6995 | *alu_limit = ptr_limit; |
| 6996 | return 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 6997 | } |
| 6998 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 6999 | static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, |
| 7000 | const struct bpf_insn *insn) |
| 7001 | { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 7002 | return env->bypass_spec_v1 || BPF_SRC(insn->code) == BPF_K; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux, |
| 7006 | u32 alu_state, u32 alu_limit) |
| 7007 | { |
| 7008 | /* If we arrived here from different branches with different |
| 7009 | * state or limits to sanitize, then this won't work. |
| 7010 | */ |
| 7011 | if (aux->alu_state && |
| 7012 | (aux->alu_state != alu_state || |
| 7013 | aux->alu_limit != alu_limit)) |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7014 | return REASON_PATHS; |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7015 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 7016 | /* Corresponding fixup done in do_misc_fixups(). */ |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7017 | aux->alu_state = alu_state; |
| 7018 | aux->alu_limit = alu_limit; |
| 7019 | return 0; |
| 7020 | } |
| 7021 | |
| 7022 | static int sanitize_val_alu(struct bpf_verifier_env *env, |
| 7023 | struct bpf_insn *insn) |
| 7024 | { |
| 7025 | struct bpf_insn_aux_data *aux = cur_aux(env); |
| 7026 | |
| 7027 | if (can_skip_alu_sanitation(env, insn)) |
| 7028 | return 0; |
| 7029 | |
| 7030 | return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0); |
| 7031 | } |
| 7032 | |
Daniel Borkmann | f528819 | 2021-03-24 11:25:39 +0100 | [diff] [blame] | 7033 | static bool sanitize_needed(u8 opcode) |
| 7034 | { |
| 7035 | return opcode == BPF_ADD || opcode == BPF_SUB; |
| 7036 | } |
| 7037 | |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7038 | struct bpf_sanitize_info { |
| 7039 | struct bpf_insn_aux_data aux; |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 7040 | bool mask_to_left; |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7041 | }; |
| 7042 | |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 7043 | static struct bpf_verifier_state * |
| 7044 | sanitize_speculative_path(struct bpf_verifier_env *env, |
| 7045 | const struct bpf_insn *insn, |
| 7046 | u32 next_idx, u32 curr_idx) |
| 7047 | { |
| 7048 | struct bpf_verifier_state *branch; |
| 7049 | struct bpf_reg_state *regs; |
| 7050 | |
| 7051 | branch = push_stack(env, next_idx, curr_idx, true); |
| 7052 | if (branch && insn) { |
| 7053 | regs = branch->frame[branch->curframe]->regs; |
| 7054 | if (BPF_SRC(insn->code) == BPF_K) { |
| 7055 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 7056 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 7057 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 7058 | mark_reg_unknown(env, regs, insn->src_reg); |
| 7059 | } |
| 7060 | } |
| 7061 | return branch; |
| 7062 | } |
| 7063 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7064 | static int sanitize_ptr_alu(struct bpf_verifier_env *env, |
| 7065 | struct bpf_insn *insn, |
| 7066 | const struct bpf_reg_state *ptr_reg, |
Daniel Borkmann | 6f55b2f | 2021-03-22 15:45:52 +0100 | [diff] [blame] | 7067 | const struct bpf_reg_state *off_reg, |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7068 | struct bpf_reg_state *dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7069 | struct bpf_sanitize_info *info, |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7070 | const bool commit_window) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7071 | { |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7072 | struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7073 | struct bpf_verifier_state *vstate = env->cur_state; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 7074 | bool off_is_imm = tnum_is_const(off_reg->var_off); |
Daniel Borkmann | 6f55b2f | 2021-03-22 15:45:52 +0100 | [diff] [blame] | 7075 | bool off_is_neg = off_reg->smin_value < 0; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7076 | bool ptr_is_dst_reg = ptr_reg == dst_reg; |
| 7077 | u8 opcode = BPF_OP(insn->code); |
| 7078 | u32 alu_state, alu_limit; |
| 7079 | struct bpf_reg_state tmp; |
| 7080 | bool ret; |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7081 | int err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7082 | |
Daniel Borkmann | d3bd741 | 2019-01-06 00:54:37 +0100 | [diff] [blame] | 7083 | if (can_skip_alu_sanitation(env, insn)) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7084 | return 0; |
| 7085 | |
| 7086 | /* We already marked aux for masking from non-speculative |
| 7087 | * paths, thus we got here in the first place. We only care |
| 7088 | * to explore bad access from here. |
| 7089 | */ |
| 7090 | if (vstate->speculative) |
| 7091 | goto do_sim; |
| 7092 | |
Daniel Borkmann | bb01a1b | 2021-05-21 10:19:22 +0000 | [diff] [blame] | 7093 | if (!commit_window) { |
| 7094 | if (!tnum_is_const(off_reg->var_off) && |
| 7095 | (off_reg->smin_value < 0) != (off_reg->smax_value < 0)) |
| 7096 | return REASON_BOUNDS; |
| 7097 | |
| 7098 | info->mask_to_left = (opcode == BPF_ADD && off_is_neg) || |
| 7099 | (opcode == BPF_SUB && !off_is_neg); |
| 7100 | } |
| 7101 | |
| 7102 | err = retrieve_ptr_limit(ptr_reg, &alu_limit, info->mask_to_left); |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7103 | if (err < 0) |
| 7104 | return err; |
| 7105 | |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7106 | if (commit_window) { |
| 7107 | /* In commit phase we narrow the masking window based on |
| 7108 | * the observed pointer move after the simulated operation. |
| 7109 | */ |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7110 | alu_state = info->aux.alu_state; |
| 7111 | alu_limit = abs(info->aux.alu_limit - alu_limit); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7112 | } else { |
| 7113 | alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 7114 | alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7115 | alu_state |= ptr_is_dst_reg ? |
| 7116 | BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST; |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 7117 | |
| 7118 | /* Limit pruning on unknown scalars to enable deep search for |
| 7119 | * potential masking differences from other program paths. |
| 7120 | */ |
| 7121 | if (!off_is_imm) |
| 7122 | env->explore_alu_limits = true; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7123 | } |
| 7124 | |
Piotr Krysiuk | f232326 | 2021-03-16 09:47:02 +0100 | [diff] [blame] | 7125 | err = update_alu_sanitation_state(aux, alu_state, alu_limit); |
| 7126 | if (err < 0) |
| 7127 | return err; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7128 | do_sim: |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7129 | /* If we're in commit phase, we're done here given we already |
| 7130 | * pushed the truncated dst_reg into the speculative verification |
| 7131 | * stack. |
Daniel Borkmann | a703619 | 2021-05-04 08:58:25 +0000 | [diff] [blame] | 7132 | * |
| 7133 | * Also, when register is a known constant, we rewrite register-based |
| 7134 | * operation to immediate-based, and thus do not need masking (and as |
| 7135 | * a consequence, do not need to simulate the zero-truncation either). |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7136 | */ |
Daniel Borkmann | a703619 | 2021-05-04 08:58:25 +0000 | [diff] [blame] | 7137 | if (commit_window || off_is_imm) |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7138 | return 0; |
| 7139 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7140 | /* Simulate and find potential out-of-bounds access under |
| 7141 | * speculative execution from truncation as a result of |
| 7142 | * masking when off was not within expected range. If off |
| 7143 | * sits in dst, then we temporarily need to move ptr there |
| 7144 | * to simulate dst (== 0) +/-= ptr. Needed, for example, |
| 7145 | * for cases where we use K-based arithmetic in one direction |
| 7146 | * and truncated reg-based in the other in order to explore |
| 7147 | * bad access. |
| 7148 | */ |
| 7149 | if (!ptr_is_dst_reg) { |
| 7150 | tmp = *dst_reg; |
| 7151 | *dst_reg = *ptr_reg; |
| 7152 | } |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 7153 | ret = sanitize_speculative_path(env, NULL, env->insn_idx + 1, |
| 7154 | env->insn_idx); |
Xu Yu | 0803278 | 2019-03-21 18:00:35 +0800 | [diff] [blame] | 7155 | if (!ptr_is_dst_reg && ret) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7156 | *dst_reg = tmp; |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7157 | return !ret ? REASON_STACK : 0; |
| 7158 | } |
| 7159 | |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 7160 | static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) |
| 7161 | { |
| 7162 | struct bpf_verifier_state *vstate = env->cur_state; |
| 7163 | |
| 7164 | /* If we simulate paths under speculation, we don't update the |
| 7165 | * insn as 'seen' such that when we verify unreachable paths in |
| 7166 | * the non-speculative domain, sanitize_dead_code() can still |
| 7167 | * rewrite/sanitize them. |
| 7168 | */ |
| 7169 | if (!vstate->speculative) |
| 7170 | env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; |
| 7171 | } |
| 7172 | |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7173 | static int sanitize_err(struct bpf_verifier_env *env, |
| 7174 | const struct bpf_insn *insn, int reason, |
| 7175 | const struct bpf_reg_state *off_reg, |
| 7176 | const struct bpf_reg_state *dst_reg) |
| 7177 | { |
| 7178 | static const char *err = "pointer arithmetic with it prohibited for !root"; |
| 7179 | const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub"; |
| 7180 | u32 dst = insn->dst_reg, src = insn->src_reg; |
| 7181 | |
| 7182 | switch (reason) { |
| 7183 | case REASON_BOUNDS: |
| 7184 | verbose(env, "R%d has unknown scalar with mixed signed bounds, %s\n", |
| 7185 | off_reg == dst_reg ? dst : src, err); |
| 7186 | break; |
| 7187 | case REASON_TYPE: |
| 7188 | verbose(env, "R%d has pointer with unsupported alu operation, %s\n", |
| 7189 | off_reg == dst_reg ? src : dst, err); |
| 7190 | break; |
| 7191 | case REASON_PATHS: |
| 7192 | verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n", |
| 7193 | dst, op, err); |
| 7194 | break; |
| 7195 | case REASON_LIMIT: |
| 7196 | verbose(env, "R%d tried to %s beyond pointer bounds, %s\n", |
| 7197 | dst, op, err); |
| 7198 | break; |
| 7199 | case REASON_STACK: |
| 7200 | verbose(env, "R%d could not be pushed for speculative verification, %s\n", |
| 7201 | dst, err); |
| 7202 | break; |
| 7203 | default: |
| 7204 | verbose(env, "verifier internal error: unknown reason (%d)\n", |
| 7205 | reason); |
| 7206 | break; |
| 7207 | } |
| 7208 | |
| 7209 | return -EACCES; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7210 | } |
| 7211 | |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 7212 | /* check that stack access falls within stack limits and that 'reg' doesn't |
| 7213 | * have a variable offset. |
| 7214 | * |
| 7215 | * Variable offset is prohibited for unprivileged mode for simplicity since it |
| 7216 | * requires corresponding support in Spectre masking for stack ALU. See also |
| 7217 | * retrieve_ptr_limit(). |
| 7218 | * |
| 7219 | * |
| 7220 | * 'off' includes 'reg->off'. |
| 7221 | */ |
| 7222 | static int check_stack_access_for_ptr_arithmetic( |
| 7223 | struct bpf_verifier_env *env, |
| 7224 | int regno, |
| 7225 | const struct bpf_reg_state *reg, |
| 7226 | int off) |
| 7227 | { |
| 7228 | if (!tnum_is_const(reg->var_off)) { |
| 7229 | char tn_buf[48]; |
| 7230 | |
| 7231 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 7232 | verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", |
| 7233 | regno, tn_buf, off); |
| 7234 | return -EACCES; |
| 7235 | } |
| 7236 | |
| 7237 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 7238 | verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 7239 | "prohibited for !root; off=%d\n", regno, off); |
| 7240 | return -EACCES; |
| 7241 | } |
| 7242 | |
| 7243 | return 0; |
| 7244 | } |
| 7245 | |
Daniel Borkmann | 073815b | 2021-03-23 15:05:48 +0100 | [diff] [blame] | 7246 | static int sanitize_check_bounds(struct bpf_verifier_env *env, |
| 7247 | const struct bpf_insn *insn, |
| 7248 | const struct bpf_reg_state *dst_reg) |
| 7249 | { |
| 7250 | u32 dst = insn->dst_reg; |
| 7251 | |
| 7252 | /* For unprivileged we require that resulting offset must be in bounds |
| 7253 | * in order to be able to sanitize access later on. |
| 7254 | */ |
| 7255 | if (env->bypass_spec_v1) |
| 7256 | return 0; |
| 7257 | |
| 7258 | switch (dst_reg->type) { |
| 7259 | case PTR_TO_STACK: |
| 7260 | if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg, |
| 7261 | dst_reg->off + dst_reg->var_off.value)) |
| 7262 | return -EACCES; |
| 7263 | break; |
| 7264 | case PTR_TO_MAP_VALUE: |
| 7265 | if (check_map_access(env, dst, dst_reg->off, 1, false)) { |
| 7266 | verbose(env, "R%d pointer arithmetic of map value goes out of range, " |
| 7267 | "prohibited for !root\n", dst); |
| 7268 | return -EACCES; |
| 7269 | } |
| 7270 | break; |
| 7271 | default: |
| 7272 | break; |
| 7273 | } |
| 7274 | |
| 7275 | return 0; |
| 7276 | } |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 7277 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7278 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7279 | * Caller should also handle BPF_MOV case separately. |
| 7280 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 7281 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 7282 | */ |
| 7283 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 7284 | struct bpf_insn *insn, |
| 7285 | const struct bpf_reg_state *ptr_reg, |
| 7286 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7287 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 7288 | struct bpf_verifier_state *vstate = env->cur_state; |
| 7289 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 7290 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7291 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7292 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 7293 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 7294 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 7295 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7296 | struct bpf_sanitize_info info = {}; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7297 | u8 opcode = BPF_OP(insn->code); |
Daniel Borkmann | 24c109b | 2021-03-23 08:51:02 +0100 | [diff] [blame] | 7298 | u32 dst = insn->dst_reg; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 7299 | int ret; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7300 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7301 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7302 | |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 7303 | if ((known && (smin_val != smax_val || umin_val != umax_val)) || |
| 7304 | smin_val > smax_val || umin_val > umax_val) { |
| 7305 | /* Taint dst register if offset had invalid bounds derived from |
| 7306 | * e.g. dead branches. |
| 7307 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 7308 | __mark_reg_unknown(env, dst_reg); |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 7309 | return 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7310 | } |
| 7311 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7312 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 7313 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Yonghong Song | 6c69354 | 2020-06-18 16:46:31 -0700 | [diff] [blame] | 7314 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
| 7315 | __mark_reg_unknown(env, dst_reg); |
| 7316 | return 0; |
| 7317 | } |
| 7318 | |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7319 | verbose(env, |
| 7320 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 7321 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7322 | return -EACCES; |
| 7323 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 7324 | |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7325 | switch (ptr_reg->type) { |
| 7326 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 7327 | verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n", |
| 7328 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7329 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7330 | case CONST_PTR_TO_MAP: |
Yonghong Song | 7c69673 | 2020-09-08 10:57:02 -0700 | [diff] [blame] | 7331 | /* smin_val represents the known value */ |
| 7332 | if (known && smin_val == 0 && opcode == BPF_ADD) |
| 7333 | break; |
Gustavo A. R. Silva | 8731745 | 2020-10-02 18:42:17 -0500 | [diff] [blame] | 7334 | fallthrough; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7335 | case PTR_TO_PACKET_END: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 7336 | case PTR_TO_SOCKET: |
| 7337 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 7338 | case PTR_TO_SOCK_COMMON: |
| 7339 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 7340 | case PTR_TO_TCP_SOCK: |
| 7341 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 7342 | case PTR_TO_XDP_SOCK: |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7343 | verbose(env, "R%d pointer arithmetic on %s prohibited\n", |
| 7344 | dst, reg_type_str[ptr_reg->type]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7345 | return -EACCES; |
Joe Stringer | aad2eea | 2018-10-02 13:35:30 -0700 | [diff] [blame] | 7346 | default: |
| 7347 | break; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 7351 | * The id may be overwritten later if we create a new variable offset. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7352 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7353 | dst_reg->type = ptr_reg->type; |
| 7354 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 7355 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 7356 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 7357 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 7358 | return -EINVAL; |
| 7359 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7360 | /* pointer types do not carry 32-bit bounds at the moment. */ |
| 7361 | __mark_reg32_unbounded(dst_reg); |
| 7362 | |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7363 | if (sanitize_needed(opcode)) { |
| 7364 | ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7365 | &info, false); |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7366 | if (ret < 0) |
| 7367 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7368 | } |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 7369 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7370 | switch (opcode) { |
| 7371 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7372 | /* We can take a fixed offset as long as it doesn't overflow |
| 7373 | * the s32 'off' field |
| 7374 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7375 | if (known && (ptr_reg->off + smin_val == |
| 7376 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7377 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7378 | dst_reg->smin_value = smin_ptr; |
| 7379 | dst_reg->smax_value = smax_ptr; |
| 7380 | dst_reg->umin_value = umin_ptr; |
| 7381 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7382 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7383 | dst_reg->off = ptr_reg->off + smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7384 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7385 | break; |
| 7386 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7387 | /* A new variable offset is created. Note that off_reg->off |
| 7388 | * == 0, since it's a scalar. |
| 7389 | * dst_reg gets the pointer type and since some positive |
| 7390 | * integer value was added to the pointer, give it a new 'id' |
| 7391 | * if it's a PTR_TO_PACKET. |
| 7392 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 7393 | * added into the variable offset, and we copy the fixed offset |
| 7394 | * from ptr_reg. |
| 7395 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7396 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 7397 | signed_add_overflows(smax_ptr, smax_val)) { |
| 7398 | dst_reg->smin_value = S64_MIN; |
| 7399 | dst_reg->smax_value = S64_MAX; |
| 7400 | } else { |
| 7401 | dst_reg->smin_value = smin_ptr + smin_val; |
| 7402 | dst_reg->smax_value = smax_ptr + smax_val; |
| 7403 | } |
| 7404 | if (umin_ptr + umin_val < umin_ptr || |
| 7405 | umax_ptr + umax_val < umax_ptr) { |
| 7406 | dst_reg->umin_value = 0; |
| 7407 | dst_reg->umax_value = U64_MAX; |
| 7408 | } else { |
| 7409 | dst_reg->umin_value = umin_ptr + umin_val; |
| 7410 | dst_reg->umax_value = umax_ptr + umax_val; |
| 7411 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7412 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 7413 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7414 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7415 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7416 | dst_reg->id = ++env->id_gen; |
| 7417 | /* something was added to pkt_ptr, set range to zero */ |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 7418 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7419 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 7420 | break; |
| 7421 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7422 | if (dst_reg == off_reg) { |
| 7423 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7424 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 7425 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7426 | return -EACCES; |
| 7427 | } |
| 7428 | /* We don't allow subtraction from FP, because (according to |
| 7429 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 7430 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 7431 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7432 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7433 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 7434 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7435 | return -EACCES; |
| 7436 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7437 | if (known && (ptr_reg->off - smin_val == |
| 7438 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7439 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7440 | dst_reg->smin_value = smin_ptr; |
| 7441 | dst_reg->smax_value = smax_ptr; |
| 7442 | dst_reg->umin_value = umin_ptr; |
| 7443 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7444 | dst_reg->var_off = ptr_reg->var_off; |
| 7445 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7446 | dst_reg->off = ptr_reg->off - smin_val; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7447 | dst_reg->raw = ptr_reg->raw; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7448 | break; |
| 7449 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7450 | /* A new variable offset is created. If the subtrahend is known |
| 7451 | * nonnegative, then any reg->range we had before is still good. |
| 7452 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7453 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 7454 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 7455 | /* Overflow possible, we know nothing */ |
| 7456 | dst_reg->smin_value = S64_MIN; |
| 7457 | dst_reg->smax_value = S64_MAX; |
| 7458 | } else { |
| 7459 | dst_reg->smin_value = smin_ptr - smax_val; |
| 7460 | dst_reg->smax_value = smax_ptr - smin_val; |
| 7461 | } |
| 7462 | if (umin_ptr < umax_val) { |
| 7463 | /* Overflow possible, we know nothing */ |
| 7464 | dst_reg->umin_value = 0; |
| 7465 | dst_reg->umax_value = U64_MAX; |
| 7466 | } else { |
| 7467 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7468 | dst_reg->umin_value = umin_ptr - umax_val; |
| 7469 | dst_reg->umax_value = umax_ptr - umin_val; |
| 7470 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7471 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 7472 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | 0962590 | 2018-11-01 00:05:52 +0100 | [diff] [blame] | 7473 | dst_reg->raw = ptr_reg->raw; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 7474 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7475 | dst_reg->id = ++env->id_gen; |
| 7476 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7477 | if (smin_val < 0) |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 7478 | memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7479 | } |
| 7480 | break; |
| 7481 | case BPF_AND: |
| 7482 | case BPF_OR: |
| 7483 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7484 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 7485 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 7486 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7487 | return -EACCES; |
| 7488 | default: |
| 7489 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 7490 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 7491 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7492 | return -EACCES; |
| 7493 | } |
| 7494 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 7495 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 7496 | return -EINVAL; |
| 7497 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 7498 | __update_reg_bounds(dst_reg); |
| 7499 | __reg_deduce_bounds(dst_reg); |
| 7500 | __reg_bound_offset(dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 7501 | |
Daniel Borkmann | 073815b | 2021-03-23 15:05:48 +0100 | [diff] [blame] | 7502 | if (sanitize_check_bounds(env, insn, dst_reg) < 0) |
| 7503 | return -EACCES; |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7504 | if (sanitize_needed(opcode)) { |
| 7505 | ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg, |
Daniel Borkmann | 3d0220f | 2021-05-21 10:17:36 +0000 | [diff] [blame] | 7506 | &info, true); |
Daniel Borkmann | 7fedb63 | 2021-03-24 10:38:26 +0100 | [diff] [blame] | 7507 | if (ret < 0) |
| 7508 | return sanitize_err(env, insn, ret, off_reg, dst_reg); |
Daniel Borkmann | 0d6303d | 2019-01-03 00:58:30 +0100 | [diff] [blame] | 7509 | } |
| 7510 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 7511 | return 0; |
| 7512 | } |
| 7513 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7514 | static void scalar32_min_max_add(struct bpf_reg_state *dst_reg, |
| 7515 | struct bpf_reg_state *src_reg) |
| 7516 | { |
| 7517 | s32 smin_val = src_reg->s32_min_value; |
| 7518 | s32 smax_val = src_reg->s32_max_value; |
| 7519 | u32 umin_val = src_reg->u32_min_value; |
| 7520 | u32 umax_val = src_reg->u32_max_value; |
| 7521 | |
| 7522 | if (signed_add32_overflows(dst_reg->s32_min_value, smin_val) || |
| 7523 | signed_add32_overflows(dst_reg->s32_max_value, smax_val)) { |
| 7524 | dst_reg->s32_min_value = S32_MIN; |
| 7525 | dst_reg->s32_max_value = S32_MAX; |
| 7526 | } else { |
| 7527 | dst_reg->s32_min_value += smin_val; |
| 7528 | dst_reg->s32_max_value += smax_val; |
| 7529 | } |
| 7530 | if (dst_reg->u32_min_value + umin_val < umin_val || |
| 7531 | dst_reg->u32_max_value + umax_val < umax_val) { |
| 7532 | dst_reg->u32_min_value = 0; |
| 7533 | dst_reg->u32_max_value = U32_MAX; |
| 7534 | } else { |
| 7535 | dst_reg->u32_min_value += umin_val; |
| 7536 | dst_reg->u32_max_value += umax_val; |
| 7537 | } |
| 7538 | } |
| 7539 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7540 | static void scalar_min_max_add(struct bpf_reg_state *dst_reg, |
| 7541 | struct bpf_reg_state *src_reg) |
| 7542 | { |
| 7543 | s64 smin_val = src_reg->smin_value; |
| 7544 | s64 smax_val = src_reg->smax_value; |
| 7545 | u64 umin_val = src_reg->umin_value; |
| 7546 | u64 umax_val = src_reg->umax_value; |
| 7547 | |
| 7548 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 7549 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 7550 | dst_reg->smin_value = S64_MIN; |
| 7551 | dst_reg->smax_value = S64_MAX; |
| 7552 | } else { |
| 7553 | dst_reg->smin_value += smin_val; |
| 7554 | dst_reg->smax_value += smax_val; |
| 7555 | } |
| 7556 | if (dst_reg->umin_value + umin_val < umin_val || |
| 7557 | dst_reg->umax_value + umax_val < umax_val) { |
| 7558 | dst_reg->umin_value = 0; |
| 7559 | dst_reg->umax_value = U64_MAX; |
| 7560 | } else { |
| 7561 | dst_reg->umin_value += umin_val; |
| 7562 | dst_reg->umax_value += umax_val; |
| 7563 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7564 | } |
| 7565 | |
| 7566 | static void scalar32_min_max_sub(struct bpf_reg_state *dst_reg, |
| 7567 | struct bpf_reg_state *src_reg) |
| 7568 | { |
| 7569 | s32 smin_val = src_reg->s32_min_value; |
| 7570 | s32 smax_val = src_reg->s32_max_value; |
| 7571 | u32 umin_val = src_reg->u32_min_value; |
| 7572 | u32 umax_val = src_reg->u32_max_value; |
| 7573 | |
| 7574 | if (signed_sub32_overflows(dst_reg->s32_min_value, smax_val) || |
| 7575 | signed_sub32_overflows(dst_reg->s32_max_value, smin_val)) { |
| 7576 | /* Overflow possible, we know nothing */ |
| 7577 | dst_reg->s32_min_value = S32_MIN; |
| 7578 | dst_reg->s32_max_value = S32_MAX; |
| 7579 | } else { |
| 7580 | dst_reg->s32_min_value -= smax_val; |
| 7581 | dst_reg->s32_max_value -= smin_val; |
| 7582 | } |
| 7583 | if (dst_reg->u32_min_value < umax_val) { |
| 7584 | /* Overflow possible, we know nothing */ |
| 7585 | dst_reg->u32_min_value = 0; |
| 7586 | dst_reg->u32_max_value = U32_MAX; |
| 7587 | } else { |
| 7588 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7589 | dst_reg->u32_min_value -= umax_val; |
| 7590 | dst_reg->u32_max_value -= umin_val; |
| 7591 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | static void scalar_min_max_sub(struct bpf_reg_state *dst_reg, |
| 7595 | struct bpf_reg_state *src_reg) |
| 7596 | { |
| 7597 | s64 smin_val = src_reg->smin_value; |
| 7598 | s64 smax_val = src_reg->smax_value; |
| 7599 | u64 umin_val = src_reg->umin_value; |
| 7600 | u64 umax_val = src_reg->umax_value; |
| 7601 | |
| 7602 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 7603 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 7604 | /* Overflow possible, we know nothing */ |
| 7605 | dst_reg->smin_value = S64_MIN; |
| 7606 | dst_reg->smax_value = S64_MAX; |
| 7607 | } else { |
| 7608 | dst_reg->smin_value -= smax_val; |
| 7609 | dst_reg->smax_value -= smin_val; |
| 7610 | } |
| 7611 | if (dst_reg->umin_value < umax_val) { |
| 7612 | /* Overflow possible, we know nothing */ |
| 7613 | dst_reg->umin_value = 0; |
| 7614 | dst_reg->umax_value = U64_MAX; |
| 7615 | } else { |
| 7616 | /* Cannot overflow (as long as bounds are consistent) */ |
| 7617 | dst_reg->umin_value -= umax_val; |
| 7618 | dst_reg->umax_value -= umin_val; |
| 7619 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7620 | } |
| 7621 | |
| 7622 | static void scalar32_min_max_mul(struct bpf_reg_state *dst_reg, |
| 7623 | struct bpf_reg_state *src_reg) |
| 7624 | { |
| 7625 | s32 smin_val = src_reg->s32_min_value; |
| 7626 | u32 umin_val = src_reg->u32_min_value; |
| 7627 | u32 umax_val = src_reg->u32_max_value; |
| 7628 | |
| 7629 | if (smin_val < 0 || dst_reg->s32_min_value < 0) { |
| 7630 | /* Ain't nobody got time to multiply that sign */ |
| 7631 | __mark_reg32_unbounded(dst_reg); |
| 7632 | return; |
| 7633 | } |
| 7634 | /* Both values are positive, so we can work with unsigned and |
| 7635 | * copy the result to signed (unless it exceeds S32_MAX). |
| 7636 | */ |
| 7637 | if (umax_val > U16_MAX || dst_reg->u32_max_value > U16_MAX) { |
| 7638 | /* Potential overflow, we know nothing */ |
| 7639 | __mark_reg32_unbounded(dst_reg); |
| 7640 | return; |
| 7641 | } |
| 7642 | dst_reg->u32_min_value *= umin_val; |
| 7643 | dst_reg->u32_max_value *= umax_val; |
| 7644 | if (dst_reg->u32_max_value > S32_MAX) { |
| 7645 | /* Overflow possible, we know nothing */ |
| 7646 | dst_reg->s32_min_value = S32_MIN; |
| 7647 | dst_reg->s32_max_value = S32_MAX; |
| 7648 | } else { |
| 7649 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7650 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7651 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7652 | } |
| 7653 | |
| 7654 | static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, |
| 7655 | struct bpf_reg_state *src_reg) |
| 7656 | { |
| 7657 | s64 smin_val = src_reg->smin_value; |
| 7658 | u64 umin_val = src_reg->umin_value; |
| 7659 | u64 umax_val = src_reg->umax_value; |
| 7660 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7661 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
| 7662 | /* Ain't nobody got time to multiply that sign */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7663 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7664 | return; |
| 7665 | } |
| 7666 | /* Both values are positive, so we can work with unsigned and |
| 7667 | * copy the result to signed (unless it exceeds S64_MAX). |
| 7668 | */ |
| 7669 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 7670 | /* Potential overflow, we know nothing */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7671 | __mark_reg64_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7672 | return; |
| 7673 | } |
| 7674 | dst_reg->umin_value *= umin_val; |
| 7675 | dst_reg->umax_value *= umax_val; |
| 7676 | if (dst_reg->umax_value > S64_MAX) { |
| 7677 | /* Overflow possible, we know nothing */ |
| 7678 | dst_reg->smin_value = S64_MIN; |
| 7679 | dst_reg->smax_value = S64_MAX; |
| 7680 | } else { |
| 7681 | dst_reg->smin_value = dst_reg->umin_value; |
| 7682 | dst_reg->smax_value = dst_reg->umax_value; |
| 7683 | } |
| 7684 | } |
| 7685 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7686 | static void scalar32_min_max_and(struct bpf_reg_state *dst_reg, |
| 7687 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7688 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7689 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7690 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7691 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 7692 | s32 smin_val = src_reg->s32_min_value; |
| 7693 | u32 umax_val = src_reg->u32_max_value; |
| 7694 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7695 | if (src_known && dst_known) { |
| 7696 | __mark_reg32_known(dst_reg, var32_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7697 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7698 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7699 | |
| 7700 | /* We get our minimum from the var_off, since that's inherently |
| 7701 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 7702 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7703 | dst_reg->u32_min_value = var32_off.value; |
| 7704 | dst_reg->u32_max_value = min(dst_reg->u32_max_value, umax_val); |
| 7705 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 7706 | /* Lose signed bounds when ANDing negative numbers, |
| 7707 | * ain't nobody got time for that. |
| 7708 | */ |
| 7709 | dst_reg->s32_min_value = S32_MIN; |
| 7710 | dst_reg->s32_max_value = S32_MAX; |
| 7711 | } else { |
| 7712 | /* ANDing two positives gives a positive, so safe to |
| 7713 | * cast result into s64. |
| 7714 | */ |
| 7715 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7716 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7717 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7718 | } |
| 7719 | |
| 7720 | static void scalar_min_max_and(struct bpf_reg_state *dst_reg, |
| 7721 | struct bpf_reg_state *src_reg) |
| 7722 | { |
| 7723 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7724 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7725 | s64 smin_val = src_reg->smin_value; |
| 7726 | u64 umax_val = src_reg->umax_value; |
| 7727 | |
| 7728 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 7729 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7730 | return; |
| 7731 | } |
| 7732 | |
| 7733 | /* We get our minimum from the var_off, since that's inherently |
| 7734 | * bitwise. Our maximum is the minimum of the operands' maxima. |
| 7735 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7736 | dst_reg->umin_value = dst_reg->var_off.value; |
| 7737 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 7738 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 7739 | /* Lose signed bounds when ANDing negative numbers, |
| 7740 | * ain't nobody got time for that. |
| 7741 | */ |
| 7742 | dst_reg->smin_value = S64_MIN; |
| 7743 | dst_reg->smax_value = S64_MAX; |
| 7744 | } else { |
| 7745 | /* ANDing two positives gives a positive, so safe to |
| 7746 | * cast result into s64. |
| 7747 | */ |
| 7748 | dst_reg->smin_value = dst_reg->umin_value; |
| 7749 | dst_reg->smax_value = dst_reg->umax_value; |
| 7750 | } |
| 7751 | /* We may learn something more from the var_off */ |
| 7752 | __update_reg_bounds(dst_reg); |
| 7753 | } |
| 7754 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7755 | static void scalar32_min_max_or(struct bpf_reg_state *dst_reg, |
| 7756 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7757 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7758 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7759 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7760 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 7761 | s32 smin_val = src_reg->s32_min_value; |
| 7762 | u32 umin_val = src_reg->u32_min_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7763 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7764 | if (src_known && dst_known) { |
| 7765 | __mark_reg32_known(dst_reg, var32_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7766 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7767 | } |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7768 | |
| 7769 | /* We get our maximum from the var_off, and our minimum is the |
| 7770 | * maximum of the operands' minima |
| 7771 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7772 | dst_reg->u32_min_value = max(dst_reg->u32_min_value, umin_val); |
| 7773 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 7774 | if (dst_reg->s32_min_value < 0 || smin_val < 0) { |
| 7775 | /* Lose signed bounds when ORing negative numbers, |
| 7776 | * ain't nobody got time for that. |
| 7777 | */ |
| 7778 | dst_reg->s32_min_value = S32_MIN; |
| 7779 | dst_reg->s32_max_value = S32_MAX; |
| 7780 | } else { |
| 7781 | /* ORing two positives gives a positive, so safe to |
| 7782 | * cast result into s64. |
| 7783 | */ |
Daniel Borkmann | 5b9fbeb | 2020-10-07 15:48:58 +0200 | [diff] [blame] | 7784 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7785 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7786 | } |
| 7787 | } |
| 7788 | |
| 7789 | static void scalar_min_max_or(struct bpf_reg_state *dst_reg, |
| 7790 | struct bpf_reg_state *src_reg) |
| 7791 | { |
| 7792 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7793 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7794 | s64 smin_val = src_reg->smin_value; |
| 7795 | u64 umin_val = src_reg->umin_value; |
| 7796 | |
| 7797 | if (src_known && dst_known) { |
John Fastabend | 4fbb38a | 2020-09-24 11:45:06 -0700 | [diff] [blame] | 7798 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7799 | return; |
| 7800 | } |
| 7801 | |
| 7802 | /* We get our maximum from the var_off, and our minimum is the |
| 7803 | * maximum of the operands' minima |
| 7804 | */ |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7805 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 7806 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 7807 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 7808 | /* Lose signed bounds when ORing negative numbers, |
| 7809 | * ain't nobody got time for that. |
| 7810 | */ |
| 7811 | dst_reg->smin_value = S64_MIN; |
| 7812 | dst_reg->smax_value = S64_MAX; |
| 7813 | } else { |
| 7814 | /* ORing two positives gives a positive, so safe to |
| 7815 | * cast result into s64. |
| 7816 | */ |
| 7817 | dst_reg->smin_value = dst_reg->umin_value; |
| 7818 | dst_reg->smax_value = dst_reg->umax_value; |
| 7819 | } |
| 7820 | /* We may learn something more from the var_off */ |
| 7821 | __update_reg_bounds(dst_reg); |
| 7822 | } |
| 7823 | |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7824 | static void scalar32_min_max_xor(struct bpf_reg_state *dst_reg, |
| 7825 | struct bpf_reg_state *src_reg) |
| 7826 | { |
| 7827 | bool src_known = tnum_subreg_is_const(src_reg->var_off); |
| 7828 | bool dst_known = tnum_subreg_is_const(dst_reg->var_off); |
| 7829 | struct tnum var32_off = tnum_subreg(dst_reg->var_off); |
| 7830 | s32 smin_val = src_reg->s32_min_value; |
| 7831 | |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7832 | if (src_known && dst_known) { |
| 7833 | __mark_reg32_known(dst_reg, var32_off.value); |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7834 | return; |
Daniel Borkmann | 049c4e1 | 2021-05-10 13:10:44 +0000 | [diff] [blame] | 7835 | } |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 7836 | |
| 7837 | /* We get both minimum and maximum from the var32_off. */ |
| 7838 | dst_reg->u32_min_value = var32_off.value; |
| 7839 | dst_reg->u32_max_value = var32_off.value | var32_off.mask; |
| 7840 | |
| 7841 | if (dst_reg->s32_min_value >= 0 && smin_val >= 0) { |
| 7842 | /* XORing two positive sign numbers gives a positive, |
| 7843 | * so safe to cast u32 result into s32. |
| 7844 | */ |
| 7845 | dst_reg->s32_min_value = dst_reg->u32_min_value; |
| 7846 | dst_reg->s32_max_value = dst_reg->u32_max_value; |
| 7847 | } else { |
| 7848 | dst_reg->s32_min_value = S32_MIN; |
| 7849 | dst_reg->s32_max_value = S32_MAX; |
| 7850 | } |
| 7851 | } |
| 7852 | |
| 7853 | static void scalar_min_max_xor(struct bpf_reg_state *dst_reg, |
| 7854 | struct bpf_reg_state *src_reg) |
| 7855 | { |
| 7856 | bool src_known = tnum_is_const(src_reg->var_off); |
| 7857 | bool dst_known = tnum_is_const(dst_reg->var_off); |
| 7858 | s64 smin_val = src_reg->smin_value; |
| 7859 | |
| 7860 | if (src_known && dst_known) { |
| 7861 | /* dst_reg->var_off.value has been updated earlier */ |
| 7862 | __mark_reg_known(dst_reg, dst_reg->var_off.value); |
| 7863 | return; |
| 7864 | } |
| 7865 | |
| 7866 | /* We get both minimum and maximum from the var_off. */ |
| 7867 | dst_reg->umin_value = dst_reg->var_off.value; |
| 7868 | dst_reg->umax_value = dst_reg->var_off.value | dst_reg->var_off.mask; |
| 7869 | |
| 7870 | if (dst_reg->smin_value >= 0 && smin_val >= 0) { |
| 7871 | /* XORing two positive sign numbers gives a positive, |
| 7872 | * so safe to cast u64 result into s64. |
| 7873 | */ |
| 7874 | dst_reg->smin_value = dst_reg->umin_value; |
| 7875 | dst_reg->smax_value = dst_reg->umax_value; |
| 7876 | } else { |
| 7877 | dst_reg->smin_value = S64_MIN; |
| 7878 | dst_reg->smax_value = S64_MAX; |
| 7879 | } |
| 7880 | |
| 7881 | __update_reg_bounds(dst_reg); |
| 7882 | } |
| 7883 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7884 | static void __scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7885 | u64 umin_val, u64 umax_val) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7886 | { |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7887 | /* We lose all sign bit information (except what we can pick |
| 7888 | * up from var_off) |
| 7889 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7890 | dst_reg->s32_min_value = S32_MIN; |
| 7891 | dst_reg->s32_max_value = S32_MAX; |
| 7892 | /* If we might shift our top bit out, then we know nothing */ |
| 7893 | if (umax_val > 31 || dst_reg->u32_max_value > 1ULL << (31 - umax_val)) { |
| 7894 | dst_reg->u32_min_value = 0; |
| 7895 | dst_reg->u32_max_value = U32_MAX; |
| 7896 | } else { |
| 7897 | dst_reg->u32_min_value <<= umin_val; |
| 7898 | dst_reg->u32_max_value <<= umax_val; |
| 7899 | } |
| 7900 | } |
| 7901 | |
| 7902 | static void scalar32_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7903 | struct bpf_reg_state *src_reg) |
| 7904 | { |
| 7905 | u32 umax_val = src_reg->u32_max_value; |
| 7906 | u32 umin_val = src_reg->u32_min_value; |
| 7907 | /* u32 alu operation will zext upper bits */ |
| 7908 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 7909 | |
| 7910 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7911 | dst_reg->var_off = tnum_subreg(tnum_lshift(subreg, umin_val)); |
| 7912 | /* Not required but being careful mark reg64 bounds as unknown so |
| 7913 | * that we are forced to pick them up from tnum and zext later and |
| 7914 | * if some path skips this step we are still safe. |
| 7915 | */ |
| 7916 | __mark_reg64_unbounded(dst_reg); |
| 7917 | __update_reg32_bounds(dst_reg); |
| 7918 | } |
| 7919 | |
| 7920 | static void __scalar64_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7921 | u64 umin_val, u64 umax_val) |
| 7922 | { |
| 7923 | /* Special case <<32 because it is a common compiler pattern to sign |
| 7924 | * extend subreg by doing <<32 s>>32. In this case if 32bit bounds are |
| 7925 | * positive we know this shift will also be positive so we can track |
| 7926 | * bounds correctly. Otherwise we lose all sign bit information except |
| 7927 | * what we can pick up from var_off. Perhaps we can generalize this |
| 7928 | * later to shifts of any length. |
| 7929 | */ |
| 7930 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_max_value >= 0) |
| 7931 | dst_reg->smax_value = (s64)dst_reg->s32_max_value << 32; |
| 7932 | else |
| 7933 | dst_reg->smax_value = S64_MAX; |
| 7934 | |
| 7935 | if (umin_val == 32 && umax_val == 32 && dst_reg->s32_min_value >= 0) |
| 7936 | dst_reg->smin_value = (s64)dst_reg->s32_min_value << 32; |
| 7937 | else |
| 7938 | dst_reg->smin_value = S64_MIN; |
| 7939 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7940 | /* If we might shift our top bit out, then we know nothing */ |
| 7941 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 7942 | dst_reg->umin_value = 0; |
| 7943 | dst_reg->umax_value = U64_MAX; |
| 7944 | } else { |
| 7945 | dst_reg->umin_value <<= umin_val; |
| 7946 | dst_reg->umax_value <<= umax_val; |
| 7947 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | static void scalar_min_max_lsh(struct bpf_reg_state *dst_reg, |
| 7951 | struct bpf_reg_state *src_reg) |
| 7952 | { |
| 7953 | u64 umax_val = src_reg->umax_value; |
| 7954 | u64 umin_val = src_reg->umin_value; |
| 7955 | |
| 7956 | /* scalar64 calc uses 32bit unshifted bounds so must be called first */ |
| 7957 | __scalar64_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7958 | __scalar32_min_max_lsh(dst_reg, umin_val, umax_val); |
| 7959 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7960 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 7961 | /* We may learn something more from the var_off */ |
| 7962 | __update_reg_bounds(dst_reg); |
| 7963 | } |
| 7964 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7965 | static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 7966 | struct bpf_reg_state *src_reg) |
| 7967 | { |
| 7968 | struct tnum subreg = tnum_subreg(dst_reg->var_off); |
| 7969 | u32 umax_val = src_reg->u32_max_value; |
| 7970 | u32 umin_val = src_reg->u32_min_value; |
| 7971 | |
| 7972 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 7973 | * be negative, then either: |
| 7974 | * 1) src_reg might be zero, so the sign bit of the result is |
| 7975 | * unknown, so we lose our signed bounds |
| 7976 | * 2) it's known negative, thus the unsigned bounds capture the |
| 7977 | * signed bounds |
| 7978 | * 3) the signed bounds cross zero, so they tell us nothing |
| 7979 | * about the result |
| 7980 | * If the value in dst_reg is known nonnegative, then again the |
Tobias Klauser | 18b24d7 | 2021-01-21 18:43:24 +0100 | [diff] [blame] | 7981 | * unsigned bounds capture the signed bounds. |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 7982 | * Thus, in all cases it suffices to blow away our signed bounds |
| 7983 | * and rely on inferring new ones from the unsigned bounds and |
| 7984 | * var_off of the result. |
| 7985 | */ |
| 7986 | dst_reg->s32_min_value = S32_MIN; |
| 7987 | dst_reg->s32_max_value = S32_MAX; |
| 7988 | |
| 7989 | dst_reg->var_off = tnum_rshift(subreg, umin_val); |
| 7990 | dst_reg->u32_min_value >>= umax_val; |
| 7991 | dst_reg->u32_max_value >>= umin_val; |
| 7992 | |
| 7993 | __mark_reg64_unbounded(dst_reg); |
| 7994 | __update_reg32_bounds(dst_reg); |
| 7995 | } |
| 7996 | |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 7997 | static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, |
| 7998 | struct bpf_reg_state *src_reg) |
| 7999 | { |
| 8000 | u64 umax_val = src_reg->umax_value; |
| 8001 | u64 umin_val = src_reg->umin_value; |
| 8002 | |
| 8003 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 8004 | * be negative, then either: |
| 8005 | * 1) src_reg might be zero, so the sign bit of the result is |
| 8006 | * unknown, so we lose our signed bounds |
| 8007 | * 2) it's known negative, thus the unsigned bounds capture the |
| 8008 | * signed bounds |
| 8009 | * 3) the signed bounds cross zero, so they tell us nothing |
| 8010 | * about the result |
| 8011 | * If the value in dst_reg is known nonnegative, then again the |
Tobias Klauser | 18b24d7 | 2021-01-21 18:43:24 +0100 | [diff] [blame] | 8012 | * unsigned bounds capture the signed bounds. |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8013 | * Thus, in all cases it suffices to blow away our signed bounds |
| 8014 | * and rely on inferring new ones from the unsigned bounds and |
| 8015 | * var_off of the result. |
| 8016 | */ |
| 8017 | dst_reg->smin_value = S64_MIN; |
| 8018 | dst_reg->smax_value = S64_MAX; |
| 8019 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val); |
| 8020 | dst_reg->umin_value >>= umax_val; |
| 8021 | dst_reg->umax_value >>= umin_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8022 | |
| 8023 | /* Its not easy to operate on alu32 bounds here because it depends |
| 8024 | * on bits being shifted in. Take easy way out and mark unbounded |
| 8025 | * so we can recalculate later from tnum. |
| 8026 | */ |
| 8027 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8028 | __update_reg_bounds(dst_reg); |
| 8029 | } |
| 8030 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8031 | static void scalar32_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 8032 | struct bpf_reg_state *src_reg) |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8033 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8034 | u64 umin_val = src_reg->u32_min_value; |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8035 | |
| 8036 | /* Upon reaching here, src_known is true and |
| 8037 | * umax_val is equal to umin_val. |
| 8038 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8039 | dst_reg->s32_min_value = (u32)(((s32)dst_reg->s32_min_value) >> umin_val); |
| 8040 | dst_reg->s32_max_value = (u32)(((s32)dst_reg->s32_max_value) >> umin_val); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8041 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8042 | dst_reg->var_off = tnum_arshift(tnum_subreg(dst_reg->var_off), umin_val, 32); |
| 8043 | |
| 8044 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 8045 | * dst_reg var_off to refine the result. |
| 8046 | */ |
| 8047 | dst_reg->u32_min_value = 0; |
| 8048 | dst_reg->u32_max_value = U32_MAX; |
| 8049 | |
| 8050 | __mark_reg64_unbounded(dst_reg); |
| 8051 | __update_reg32_bounds(dst_reg); |
| 8052 | } |
| 8053 | |
| 8054 | static void scalar_min_max_arsh(struct bpf_reg_state *dst_reg, |
| 8055 | struct bpf_reg_state *src_reg) |
| 8056 | { |
| 8057 | u64 umin_val = src_reg->umin_value; |
| 8058 | |
| 8059 | /* Upon reaching here, src_known is true and umax_val is equal |
| 8060 | * to umin_val. |
| 8061 | */ |
| 8062 | dst_reg->smin_value >>= umin_val; |
| 8063 | dst_reg->smax_value >>= umin_val; |
| 8064 | |
| 8065 | dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val, 64); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8066 | |
| 8067 | /* blow away the dst_reg umin_value/umax_value and rely on |
| 8068 | * dst_reg var_off to refine the result. |
| 8069 | */ |
| 8070 | dst_reg->umin_value = 0; |
| 8071 | dst_reg->umax_value = U64_MAX; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8072 | |
| 8073 | /* Its not easy to operate on alu32 bounds here because it depends |
| 8074 | * on bits being shifted in from upper 32-bits. Take easy way out |
| 8075 | * and mark unbounded so we can recalculate later from tnum. |
| 8076 | */ |
| 8077 | __mark_reg32_unbounded(dst_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8078 | __update_reg_bounds(dst_reg); |
| 8079 | } |
| 8080 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8081 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 8082 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 8083 | * need extra checks in the 32-bit case. |
| 8084 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8085 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 8086 | struct bpf_insn *insn, |
| 8087 | struct bpf_reg_state *dst_reg, |
| 8088 | struct bpf_reg_state src_reg) |
| 8089 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8090 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8091 | u8 opcode = BPF_OP(insn->code); |
Mao Wenan | b0b3fb6 | 2020-04-18 09:37:35 +0800 | [diff] [blame] | 8092 | bool src_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8093 | s64 smin_val, smax_val; |
| 8094 | u64 umin_val, umax_val; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8095 | s32 s32_min_val, s32_max_val; |
| 8096 | u32 u32_min_val, u32_max_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8097 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8098 | bool alu32 = (BPF_CLASS(insn->code) != BPF_ALU64); |
Daniel Borkmann | a6aaece | 2021-03-23 09:30:01 +0100 | [diff] [blame] | 8099 | int ret; |
Jann Horn | b799207 | 2018-10-05 18:17:59 +0200 | [diff] [blame] | 8100 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8101 | smin_val = src_reg.smin_value; |
| 8102 | smax_val = src_reg.smax_value; |
| 8103 | umin_val = src_reg.umin_value; |
| 8104 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8105 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8106 | s32_min_val = src_reg.s32_min_value; |
| 8107 | s32_max_val = src_reg.s32_max_value; |
| 8108 | u32_min_val = src_reg.u32_min_value; |
| 8109 | u32_max_val = src_reg.u32_max_value; |
| 8110 | |
| 8111 | if (alu32) { |
| 8112 | src_known = tnum_subreg_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8113 | if ((src_known && |
| 8114 | (s32_min_val != s32_max_val || u32_min_val != u32_max_val)) || |
| 8115 | s32_min_val > s32_max_val || u32_min_val > u32_max_val) { |
| 8116 | /* Taint dst register if offset had invalid bounds |
| 8117 | * derived from e.g. dead branches. |
| 8118 | */ |
| 8119 | __mark_reg_unknown(env, dst_reg); |
| 8120 | return 0; |
| 8121 | } |
| 8122 | } else { |
| 8123 | src_known = tnum_is_const(src_reg.var_off); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8124 | if ((src_known && |
| 8125 | (smin_val != smax_val || umin_val != umax_val)) || |
| 8126 | smin_val > smax_val || umin_val > umax_val) { |
| 8127 | /* Taint dst register if offset had invalid bounds |
| 8128 | * derived from e.g. dead branches. |
| 8129 | */ |
| 8130 | __mark_reg_unknown(env, dst_reg); |
| 8131 | return 0; |
| 8132 | } |
Daniel Borkmann | 6f16101 | 2018-01-18 01:15:21 +0100 | [diff] [blame] | 8133 | } |
| 8134 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 8135 | if (!src_known && |
| 8136 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 8137 | __mark_reg_unknown(env, dst_reg); |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 8138 | return 0; |
| 8139 | } |
| 8140 | |
Daniel Borkmann | f528819 | 2021-03-24 11:25:39 +0100 | [diff] [blame] | 8141 | if (sanitize_needed(opcode)) { |
| 8142 | ret = sanitize_val_alu(env, insn); |
| 8143 | if (ret < 0) |
| 8144 | return sanitize_err(env, insn, ret, NULL, NULL); |
| 8145 | } |
| 8146 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8147 | /* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops. |
| 8148 | * There are two classes of instructions: The first class we track both |
| 8149 | * alu32 and alu64 sign/unsigned bounds independently this provides the |
| 8150 | * greatest amount of precision when alu operations are mixed with jmp32 |
| 8151 | * operations. These operations are BPF_ADD, BPF_SUB, BPF_MUL, BPF_ADD, |
| 8152 | * and BPF_OR. This is possible because these ops have fairly easy to |
| 8153 | * understand and calculate behavior in both 32-bit and 64-bit alu ops. |
| 8154 | * See alu32 verifier tests for examples. The second class of |
| 8155 | * operations, BPF_LSH, BPF_RSH, and BPF_ARSH, however are not so easy |
| 8156 | * with regards to tracking sign/unsigned bounds because the bits may |
| 8157 | * cross subreg boundaries in the alu64 case. When this happens we mark |
| 8158 | * the reg unbounded in the subreg bound space and use the resulting |
| 8159 | * tnum to calculate an approximation of the sign/unsigned bounds. |
| 8160 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8161 | switch (opcode) { |
| 8162 | case BPF_ADD: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8163 | scalar32_min_max_add(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8164 | scalar_min_max_add(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8165 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8166 | break; |
| 8167 | case BPF_SUB: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8168 | scalar32_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8169 | scalar_min_max_sub(dst_reg, &src_reg); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8170 | dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8171 | break; |
| 8172 | case BPF_MUL: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8173 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 8174 | scalar32_min_max_mul(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8175 | scalar_min_max_mul(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8176 | break; |
| 8177 | case BPF_AND: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8178 | dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off); |
| 8179 | scalar32_min_max_and(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8180 | scalar_min_max_and(dst_reg, &src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8181 | break; |
| 8182 | case BPF_OR: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8183 | dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off); |
| 8184 | scalar32_min_max_or(dst_reg, &src_reg); |
John Fastabend | 07cd263 | 2020-03-24 10:38:15 -0700 | [diff] [blame] | 8185 | scalar_min_max_or(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8186 | break; |
Yonghong Song | 2921c90 | 2020-08-24 23:46:08 -0700 | [diff] [blame] | 8187 | case BPF_XOR: |
| 8188 | dst_reg->var_off = tnum_xor(dst_reg->var_off, src_reg.var_off); |
| 8189 | scalar32_min_max_xor(dst_reg, &src_reg); |
| 8190 | scalar_min_max_xor(dst_reg, &src_reg); |
| 8191 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8192 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8193 | if (umax_val >= insn_bitness) { |
| 8194 | /* Shifts greater than 31 or 63 are undefined. |
| 8195 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8196 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8197 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8198 | break; |
| 8199 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8200 | if (alu32) |
| 8201 | scalar32_min_max_lsh(dst_reg, &src_reg); |
| 8202 | else |
| 8203 | scalar_min_max_lsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8204 | break; |
| 8205 | case BPF_RSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8206 | if (umax_val >= insn_bitness) { |
| 8207 | /* Shifts greater than 31 or 63 are undefined. |
| 8208 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8209 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8210 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8211 | break; |
| 8212 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8213 | if (alu32) |
| 8214 | scalar32_min_max_rsh(dst_reg, &src_reg); |
| 8215 | else |
| 8216 | scalar_min_max_rsh(dst_reg, &src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8217 | break; |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 8218 | case BPF_ARSH: |
| 8219 | if (umax_val >= insn_bitness) { |
| 8220 | /* Shifts greater than 31 or 63 are undefined. |
| 8221 | * This includes shifts by a negative number. |
| 8222 | */ |
| 8223 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 8224 | break; |
| 8225 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8226 | if (alu32) |
| 8227 | scalar32_min_max_arsh(dst_reg, &src_reg); |
| 8228 | else |
| 8229 | scalar_min_max_arsh(dst_reg, &src_reg); |
Yonghong Song | 9cbe1f5a | 2018-04-28 22:28:11 -0700 | [diff] [blame] | 8230 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8231 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8232 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8233 | break; |
| 8234 | } |
| 8235 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8236 | /* ALU32 ops are zero extended into 64bit register */ |
| 8237 | if (alu32) |
| 8238 | zext_32_to_64(dst_reg); |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 8239 | |
John Fastabend | 294f2fc | 2020-03-24 10:38:37 -0700 | [diff] [blame] | 8240 | __update_reg_bounds(dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8241 | __reg_deduce_bounds(dst_reg); |
| 8242 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8243 | return 0; |
| 8244 | } |
| 8245 | |
| 8246 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 8247 | * and var_off. |
| 8248 | */ |
| 8249 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 8250 | struct bpf_insn *insn) |
| 8251 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8252 | struct bpf_verifier_state *vstate = env->cur_state; |
| 8253 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 8254 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8255 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 8256 | u8 opcode = BPF_OP(insn->code); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8257 | int err; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8258 | |
| 8259 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8260 | src_reg = NULL; |
| 8261 | if (dst_reg->type != SCALAR_VALUE) |
| 8262 | ptr_reg = dst_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8263 | else |
| 8264 | /* Make sure ID is cleared otherwise dst_reg min/max could be |
| 8265 | * incorrectly propagated into other registers by find_equal_scalars() |
| 8266 | */ |
| 8267 | dst_reg->id = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8268 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8269 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8270 | if (src_reg->type != SCALAR_VALUE) { |
| 8271 | if (dst_reg->type != SCALAR_VALUE) { |
| 8272 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8273 | * an arbitrary scalar. Disallow all math except |
| 8274 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8275 | */ |
Alexei Starovoitov | dd06682 | 2018-09-12 14:06:10 -0700 | [diff] [blame] | 8276 | if (opcode == BPF_SUB && env->allow_ptr_leaks) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8277 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 8278 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8279 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8280 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 8281 | insn->dst_reg, |
| 8282 | bpf_alu_string[opcode >> 4]); |
| 8283 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8284 | } else { |
| 8285 | /* scalar += pointer |
| 8286 | * This is legal, but we have to reverse our |
| 8287 | * src/dest handling in computing the range |
| 8288 | */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8289 | err = mark_chain_precision(env, insn->dst_reg); |
| 8290 | if (err) |
| 8291 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8292 | return adjust_ptr_min_max_vals(env, insn, |
| 8293 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8294 | } |
| 8295 | } else if (ptr_reg) { |
| 8296 | /* pointer += scalar */ |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 8297 | err = mark_chain_precision(env, insn->src_reg); |
| 8298 | if (err) |
| 8299 | return err; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8300 | return adjust_ptr_min_max_vals(env, insn, |
| 8301 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8302 | } |
| 8303 | } else { |
| 8304 | /* Pretend the src is a reg with a known value, since we only |
| 8305 | * need to be able to read from this state. |
| 8306 | */ |
| 8307 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8308 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8309 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 8310 | if (ptr_reg) /* pointer += K */ |
| 8311 | return adjust_ptr_min_max_vals(env, insn, |
| 8312 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8313 | } |
| 8314 | |
| 8315 | /* Got here implies adding two SCALAR_VALUEs */ |
| 8316 | if (WARN_ON_ONCE(ptr_reg)) { |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 8317 | print_verifier_state(env, state, true); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8318 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8319 | return -EINVAL; |
| 8320 | } |
| 8321 | if (WARN_ON(!src_reg)) { |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 8322 | print_verifier_state(env, state, true); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8323 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8324 | return -EINVAL; |
| 8325 | } |
| 8326 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8327 | } |
| 8328 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8329 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 8330 | static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8331 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 8332 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8333 | u8 opcode = BPF_OP(insn->code); |
| 8334 | int err; |
| 8335 | |
| 8336 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 8337 | if (opcode == BPF_NEG) { |
| 8338 | if (BPF_SRC(insn->code) != 0 || |
| 8339 | insn->src_reg != BPF_REG_0 || |
| 8340 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8341 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8342 | return -EINVAL; |
| 8343 | } |
| 8344 | } else { |
| 8345 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 8346 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 8347 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8348 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8349 | return -EINVAL; |
| 8350 | } |
| 8351 | } |
| 8352 | |
| 8353 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8354 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8355 | if (err) |
| 8356 | return err; |
| 8357 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8358 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8359 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8360 | insn->dst_reg); |
| 8361 | return -EACCES; |
| 8362 | } |
| 8363 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8364 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8365 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8366 | if (err) |
| 8367 | return err; |
| 8368 | |
| 8369 | } else if (opcode == BPF_MOV) { |
| 8370 | |
| 8371 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8372 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8373 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8374 | return -EINVAL; |
| 8375 | } |
| 8376 | |
| 8377 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8378 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8379 | if (err) |
| 8380 | return err; |
| 8381 | } else { |
| 8382 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8383 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8384 | return -EINVAL; |
| 8385 | } |
| 8386 | } |
| 8387 | |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 8388 | /* check dest operand, mark as required later */ |
| 8389 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8390 | if (err) |
| 8391 | return err; |
| 8392 | |
| 8393 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8394 | struct bpf_reg_state *src_reg = regs + insn->src_reg; |
| 8395 | struct bpf_reg_state *dst_reg = regs + insn->dst_reg; |
| 8396 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8397 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 8398 | /* case: R1 = R2 |
| 8399 | * copy register state to dest reg |
| 8400 | */ |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8401 | if (src_reg->type == SCALAR_VALUE && !src_reg->id) |
| 8402 | /* Assign src and dst registers the same ID |
| 8403 | * that will be used by find_equal_scalars() |
| 8404 | * to propagate min/max range. |
| 8405 | */ |
| 8406 | src_reg->id = ++env->id_gen; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8407 | *dst_reg = *src_reg; |
| 8408 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8409 | dst_reg->subreg_def = DEF_NOT_SUBREG; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8410 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8411 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8412 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8413 | verbose(env, |
| 8414 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8415 | insn->src_reg); |
| 8416 | return -EACCES; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8417 | } else if (src_reg->type == SCALAR_VALUE) { |
| 8418 | *dst_reg = *src_reg; |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 8419 | /* Make sure ID is cleared otherwise |
| 8420 | * dst_reg min/max could be incorrectly |
| 8421 | * propagated into src_reg by find_equal_scalars() |
| 8422 | */ |
| 8423 | dst_reg->id = 0; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8424 | dst_reg->live |= REG_LIVE_WRITTEN; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 8425 | dst_reg->subreg_def = env->insn_idx + 1; |
Jiong Wang | e434b8c | 2018-12-07 12:16:18 -0500 | [diff] [blame] | 8426 | } else { |
| 8427 | mark_reg_unknown(env, regs, |
| 8428 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 8429 | } |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8430 | zext_32_to_64(dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8431 | } |
| 8432 | } else { |
| 8433 | /* case: R = imm |
| 8434 | * remember the value we stored into this reg |
| 8435 | */ |
Arthur Fabre | fbeb160 | 2018-07-31 18:17:22 +0100 | [diff] [blame] | 8436 | /* clear any state __mark_reg_known doesn't set */ |
| 8437 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8438 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 8439 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 8440 | __mark_reg_known(regs + insn->dst_reg, |
| 8441 | insn->imm); |
| 8442 | } else { |
| 8443 | __mark_reg_known(regs + insn->dst_reg, |
| 8444 | (u32)insn->imm); |
| 8445 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8446 | } |
| 8447 | |
| 8448 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8449 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8450 | return -EINVAL; |
| 8451 | |
| 8452 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 8453 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8454 | if (BPF_SRC(insn->code) == BPF_X) { |
| 8455 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8456 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8457 | return -EINVAL; |
| 8458 | } |
| 8459 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8460 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8461 | if (err) |
| 8462 | return err; |
| 8463 | } else { |
| 8464 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8465 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8466 | return -EINVAL; |
| 8467 | } |
| 8468 | } |
| 8469 | |
| 8470 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8471 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8472 | if (err) |
| 8473 | return err; |
| 8474 | |
| 8475 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 8476 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8477 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8478 | return -EINVAL; |
| 8479 | } |
| 8480 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 8481 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 8482 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 8483 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 8484 | |
| 8485 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 8486 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 8487 | return -EINVAL; |
| 8488 | } |
| 8489 | } |
| 8490 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8491 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 8492 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 8493 | if (err) |
| 8494 | return err; |
| 8495 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8496 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 8497 | } |
| 8498 | |
| 8499 | return 0; |
| 8500 | } |
| 8501 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8502 | static void __find_good_pkt_pointers(struct bpf_func_state *state, |
| 8503 | struct bpf_reg_state *dst_reg, |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8504 | enum bpf_reg_type type, int new_range) |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8505 | { |
| 8506 | struct bpf_reg_state *reg; |
| 8507 | int i; |
| 8508 | |
| 8509 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 8510 | reg = &state->regs[i]; |
| 8511 | if (reg->type == type && reg->id == dst_reg->id) |
| 8512 | /* keep the maximum range already checked */ |
| 8513 | reg->range = max(reg->range, new_range); |
| 8514 | } |
| 8515 | |
| 8516 | bpf_for_each_spilled_reg(i, state, reg) { |
| 8517 | if (!reg) |
| 8518 | continue; |
| 8519 | if (reg->type == type && reg->id == dst_reg->id) |
| 8520 | reg->range = max(reg->range, new_range); |
| 8521 | } |
| 8522 | } |
| 8523 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 8524 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 8525 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 8526 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8527 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8528 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8529 | int new_range, i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8530 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8531 | if (dst_reg->off < 0 || |
| 8532 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8533 | /* This doesn't give us any range */ |
| 8534 | return; |
| 8535 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8536 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 8537 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8538 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 8539 | * than pkt_end, but that's because it's also less than pkt. |
| 8540 | */ |
| 8541 | return; |
| 8542 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8543 | new_range = dst_reg->off; |
| 8544 | if (range_right_open) |
Maxim Mikityanskiy | 2fa7d94 | 2021-11-30 20:16:07 +0200 | [diff] [blame] | 8545 | new_range++; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8546 | |
| 8547 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8548 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8549 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8550 | * |
| 8551 | * r2 = r3; |
| 8552 | * r2 += 8; |
| 8553 | * if (r2 > pkt_end) goto <handle exception> |
| 8554 | * <access okay> |
| 8555 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8556 | * r2 = r3; |
| 8557 | * r2 += 8; |
| 8558 | * if (r2 < pkt_end) goto <access okay> |
| 8559 | * <handle exception> |
| 8560 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8561 | * Where: |
| 8562 | * r2 == dst_reg, pkt_end == src_reg |
| 8563 | * r2=pkt(id=n,off=8,r=0) |
| 8564 | * r3=pkt(id=n,off=0,r=0) |
| 8565 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8566 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8567 | * |
| 8568 | * r2 = r3; |
| 8569 | * r2 += 8; |
| 8570 | * if (pkt_end >= r2) goto <access okay> |
| 8571 | * <handle exception> |
| 8572 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8573 | * r2 = r3; |
| 8574 | * r2 += 8; |
| 8575 | * if (pkt_end <= r2) goto <handle exception> |
| 8576 | * <access okay> |
| 8577 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8578 | * Where: |
| 8579 | * pkt_end == dst_reg, r2 == src_reg |
| 8580 | * r2=pkt(id=n,off=8,r=0) |
| 8581 | * r3=pkt(id=n,off=0,r=0) |
| 8582 | * |
| 8583 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 8584 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 8585 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 8586 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8587 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 8588 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8589 | /* If our ids match, then we must have the same max_value. And we |
| 8590 | * don't care about the other reg's fixed offset, since if it's too big |
| 8591 | * the range won't allow anything. |
| 8592 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 8593 | */ |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 8594 | for (i = 0; i <= vstate->curframe; i++) |
| 8595 | __find_good_pkt_pointers(vstate->frame[i], dst_reg, type, |
| 8596 | new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 8597 | } |
| 8598 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8599 | static int is_branch32_taken(struct bpf_reg_state *reg, u32 val, u8 opcode) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8600 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8601 | struct tnum subreg = tnum_subreg(reg->var_off); |
| 8602 | s32 sval = (s32)val; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8603 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8604 | switch (opcode) { |
| 8605 | case BPF_JEQ: |
| 8606 | if (tnum_is_const(subreg)) |
| 8607 | return !!tnum_equals_const(subreg, val); |
| 8608 | break; |
| 8609 | case BPF_JNE: |
| 8610 | if (tnum_is_const(subreg)) |
| 8611 | return !tnum_equals_const(subreg, val); |
| 8612 | break; |
| 8613 | case BPF_JSET: |
| 8614 | if ((~subreg.mask & subreg.value) & val) |
| 8615 | return 1; |
| 8616 | if (!((subreg.mask | subreg.value) & val)) |
| 8617 | return 0; |
| 8618 | break; |
| 8619 | case BPF_JGT: |
| 8620 | if (reg->u32_min_value > val) |
| 8621 | return 1; |
| 8622 | else if (reg->u32_max_value <= val) |
| 8623 | return 0; |
| 8624 | break; |
| 8625 | case BPF_JSGT: |
| 8626 | if (reg->s32_min_value > sval) |
| 8627 | return 1; |
Daniel Borkmann | ee114dd | 2021-02-05 17:20:14 +0100 | [diff] [blame] | 8628 | else if (reg->s32_max_value <= sval) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8629 | return 0; |
| 8630 | break; |
| 8631 | case BPF_JLT: |
| 8632 | if (reg->u32_max_value < val) |
| 8633 | return 1; |
| 8634 | else if (reg->u32_min_value >= val) |
| 8635 | return 0; |
| 8636 | break; |
| 8637 | case BPF_JSLT: |
| 8638 | if (reg->s32_max_value < sval) |
| 8639 | return 1; |
| 8640 | else if (reg->s32_min_value >= sval) |
| 8641 | return 0; |
| 8642 | break; |
| 8643 | case BPF_JGE: |
| 8644 | if (reg->u32_min_value >= val) |
| 8645 | return 1; |
| 8646 | else if (reg->u32_max_value < val) |
| 8647 | return 0; |
| 8648 | break; |
| 8649 | case BPF_JSGE: |
| 8650 | if (reg->s32_min_value >= sval) |
| 8651 | return 1; |
| 8652 | else if (reg->s32_max_value < sval) |
| 8653 | return 0; |
| 8654 | break; |
| 8655 | case BPF_JLE: |
| 8656 | if (reg->u32_max_value <= val) |
| 8657 | return 1; |
| 8658 | else if (reg->u32_min_value > val) |
| 8659 | return 0; |
| 8660 | break; |
| 8661 | case BPF_JSLE: |
| 8662 | if (reg->s32_max_value <= sval) |
| 8663 | return 1; |
| 8664 | else if (reg->s32_min_value > sval) |
| 8665 | return 0; |
| 8666 | break; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8667 | } |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8668 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8669 | return -1; |
| 8670 | } |
| 8671 | |
| 8672 | |
| 8673 | static int is_branch64_taken(struct bpf_reg_state *reg, u64 val, u8 opcode) |
| 8674 | { |
| 8675 | s64 sval = (s64)val; |
| 8676 | |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8677 | switch (opcode) { |
| 8678 | case BPF_JEQ: |
| 8679 | if (tnum_is_const(reg->var_off)) |
| 8680 | return !!tnum_equals_const(reg->var_off, val); |
| 8681 | break; |
| 8682 | case BPF_JNE: |
| 8683 | if (tnum_is_const(reg->var_off)) |
| 8684 | return !tnum_equals_const(reg->var_off, val); |
| 8685 | break; |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8686 | case BPF_JSET: |
| 8687 | if ((~reg->var_off.mask & reg->var_off.value) & val) |
| 8688 | return 1; |
| 8689 | if (!((reg->var_off.mask | reg->var_off.value) & val)) |
| 8690 | return 0; |
| 8691 | break; |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8692 | case BPF_JGT: |
| 8693 | if (reg->umin_value > val) |
| 8694 | return 1; |
| 8695 | else if (reg->umax_value <= val) |
| 8696 | return 0; |
| 8697 | break; |
| 8698 | case BPF_JSGT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8699 | if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8700 | return 1; |
Daniel Borkmann | ee114dd | 2021-02-05 17:20:14 +0100 | [diff] [blame] | 8701 | else if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8702 | return 0; |
| 8703 | break; |
| 8704 | case BPF_JLT: |
| 8705 | if (reg->umax_value < val) |
| 8706 | return 1; |
| 8707 | else if (reg->umin_value >= val) |
| 8708 | return 0; |
| 8709 | break; |
| 8710 | case BPF_JSLT: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8711 | if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8712 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8713 | else if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8714 | return 0; |
| 8715 | break; |
| 8716 | case BPF_JGE: |
| 8717 | if (reg->umin_value >= val) |
| 8718 | return 1; |
| 8719 | else if (reg->umax_value < val) |
| 8720 | return 0; |
| 8721 | break; |
| 8722 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8723 | if (reg->smin_value >= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8724 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8725 | else if (reg->smax_value < sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8726 | return 0; |
| 8727 | break; |
| 8728 | case BPF_JLE: |
| 8729 | if (reg->umax_value <= val) |
| 8730 | return 1; |
| 8731 | else if (reg->umin_value > val) |
| 8732 | return 0; |
| 8733 | break; |
| 8734 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8735 | if (reg->smax_value <= sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8736 | return 1; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8737 | else if (reg->smin_value > sval) |
Alexei Starovoitov | 4f7b3e8 | 2018-12-03 22:46:05 -0800 | [diff] [blame] | 8738 | return 0; |
| 8739 | break; |
| 8740 | } |
| 8741 | |
| 8742 | return -1; |
| 8743 | } |
| 8744 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8745 | /* compute branch direction of the expression "if (reg opcode val) goto target;" |
| 8746 | * and return: |
| 8747 | * 1 - branch will be taken and "goto target" will be executed |
| 8748 | * 0 - branch will not be taken and fall-through to next insn |
| 8749 | * -1 - unknown. Example: "if (reg < 5)" is unknown when register value |
| 8750 | * range [0,10] |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8751 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8752 | static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode, |
| 8753 | bool is_jmp32) |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8754 | { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 8755 | if (__is_pointer_value(false, reg)) { |
| 8756 | if (!reg_type_not_null(reg->type)) |
| 8757 | return -1; |
| 8758 | |
| 8759 | /* If pointer is valid tests against zero will fail so we can |
| 8760 | * use this to direct branch taken. |
| 8761 | */ |
| 8762 | if (val != 0) |
| 8763 | return -1; |
| 8764 | |
| 8765 | switch (opcode) { |
| 8766 | case BPF_JEQ: |
| 8767 | return 0; |
| 8768 | case BPF_JNE: |
| 8769 | return 1; |
| 8770 | default: |
| 8771 | return -1; |
| 8772 | } |
| 8773 | } |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8774 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8775 | if (is_jmp32) |
| 8776 | return is_branch32_taken(reg, val, opcode); |
| 8777 | return is_branch64_taken(reg, val, opcode); |
Jann Horn | 604dca5 | 2020-03-30 18:03:23 +0200 | [diff] [blame] | 8778 | } |
| 8779 | |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 8780 | static int flip_opcode(u32 opcode) |
| 8781 | { |
| 8782 | /* How can we transform "a <op> b" into "b <op> a"? */ |
| 8783 | static const u8 opcode_flip[16] = { |
| 8784 | /* these stay the same */ |
| 8785 | [BPF_JEQ >> 4] = BPF_JEQ, |
| 8786 | [BPF_JNE >> 4] = BPF_JNE, |
| 8787 | [BPF_JSET >> 4] = BPF_JSET, |
| 8788 | /* these swap "lesser" and "greater" (L and G in the opcodes) */ |
| 8789 | [BPF_JGE >> 4] = BPF_JLE, |
| 8790 | [BPF_JGT >> 4] = BPF_JLT, |
| 8791 | [BPF_JLE >> 4] = BPF_JGE, |
| 8792 | [BPF_JLT >> 4] = BPF_JGT, |
| 8793 | [BPF_JSGE >> 4] = BPF_JSLE, |
| 8794 | [BPF_JSGT >> 4] = BPF_JSLT, |
| 8795 | [BPF_JSLE >> 4] = BPF_JSGE, |
| 8796 | [BPF_JSLT >> 4] = BPF_JSGT |
| 8797 | }; |
| 8798 | return opcode_flip[opcode >> 4]; |
| 8799 | } |
| 8800 | |
| 8801 | static int is_pkt_ptr_branch_taken(struct bpf_reg_state *dst_reg, |
| 8802 | struct bpf_reg_state *src_reg, |
| 8803 | u8 opcode) |
| 8804 | { |
| 8805 | struct bpf_reg_state *pkt; |
| 8806 | |
| 8807 | if (src_reg->type == PTR_TO_PACKET_END) { |
| 8808 | pkt = dst_reg; |
| 8809 | } else if (dst_reg->type == PTR_TO_PACKET_END) { |
| 8810 | pkt = src_reg; |
| 8811 | opcode = flip_opcode(opcode); |
| 8812 | } else { |
| 8813 | return -1; |
| 8814 | } |
| 8815 | |
| 8816 | if (pkt->range >= 0) |
| 8817 | return -1; |
| 8818 | |
| 8819 | switch (opcode) { |
| 8820 | case BPF_JLE: |
| 8821 | /* pkt <= pkt_end */ |
| 8822 | fallthrough; |
| 8823 | case BPF_JGT: |
| 8824 | /* pkt > pkt_end */ |
| 8825 | if (pkt->range == BEYOND_PKT_END) |
| 8826 | /* pkt has at last one extra byte beyond pkt_end */ |
| 8827 | return opcode == BPF_JGT; |
| 8828 | break; |
| 8829 | case BPF_JLT: |
| 8830 | /* pkt < pkt_end */ |
| 8831 | fallthrough; |
| 8832 | case BPF_JGE: |
| 8833 | /* pkt >= pkt_end */ |
| 8834 | if (pkt->range == BEYOND_PKT_END || pkt->range == AT_PKT_END) |
| 8835 | return opcode == BPF_JGE; |
| 8836 | break; |
| 8837 | } |
| 8838 | return -1; |
| 8839 | } |
| 8840 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8841 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 8842 | * variable register that we are working on, and src_reg is a constant or we're |
| 8843 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8844 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8845 | */ |
| 8846 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8847 | struct bpf_reg_state *false_reg, |
| 8848 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 8849 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8850 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8851 | struct tnum false_32off = tnum_subreg(false_reg->var_off); |
| 8852 | struct tnum false_64off = false_reg->var_off; |
| 8853 | struct tnum true_32off = tnum_subreg(true_reg->var_off); |
| 8854 | struct tnum true_64off = true_reg->var_off; |
| 8855 | s64 sval = (s64)val; |
| 8856 | s32 sval32 = (s32)val32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8857 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8858 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 8859 | * variable offset from the compare (unless src_reg were a pointer into |
| 8860 | * the same object, but we don't bother with that. |
| 8861 | * Since false_reg and true_reg have the same type by construction, we |
| 8862 | * only need to check one of them for pointerness. |
| 8863 | */ |
| 8864 | if (__is_pointer_value(false, false_reg)) |
| 8865 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 8866 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8867 | switch (opcode) { |
| 8868 | case BPF_JEQ: |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8869 | case BPF_JNE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8870 | { |
| 8871 | struct bpf_reg_state *reg = |
| 8872 | opcode == BPF_JEQ ? true_reg : false_reg; |
| 8873 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 8874 | /* JEQ/JNE comparison doesn't change the register equivalence. |
| 8875 | * r1 = r2; |
| 8876 | * if (r1 == 42) goto label; |
| 8877 | * ... |
| 8878 | * label: // here both r1 and r2 are known to be 42. |
| 8879 | * |
| 8880 | * Hence when marking register as known preserve it's ID. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8881 | */ |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8882 | if (is_jmp32) |
| 8883 | __mark_reg32_known(reg, val32); |
| 8884 | else |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 8885 | ___mark_reg_known(reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8886 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8887 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8888 | case BPF_JSET: |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8889 | if (is_jmp32) { |
| 8890 | false_32off = tnum_and(false_32off, tnum_const(~val32)); |
| 8891 | if (is_power_of_2(val32)) |
| 8892 | true_32off = tnum_or(true_32off, |
| 8893 | tnum_const(val32)); |
| 8894 | } else { |
| 8895 | false_64off = tnum_and(false_64off, tnum_const(~val)); |
| 8896 | if (is_power_of_2(val)) |
| 8897 | true_64off = tnum_or(true_64off, |
| 8898 | tnum_const(val)); |
| 8899 | } |
Jakub Kicinski | 960ea05 | 2018-12-19 22:13:04 -0800 | [diff] [blame] | 8900 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8901 | case BPF_JGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8902 | case BPF_JGT: |
| 8903 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8904 | if (is_jmp32) { |
| 8905 | u32 false_umax = opcode == BPF_JGT ? val32 : val32 - 1; |
| 8906 | u32 true_umin = opcode == BPF_JGT ? val32 + 1 : val32; |
| 8907 | |
| 8908 | false_reg->u32_max_value = min(false_reg->u32_max_value, |
| 8909 | false_umax); |
| 8910 | true_reg->u32_min_value = max(true_reg->u32_min_value, |
| 8911 | true_umin); |
| 8912 | } else { |
| 8913 | u64 false_umax = opcode == BPF_JGT ? val : val - 1; |
| 8914 | u64 true_umin = opcode == BPF_JGT ? val + 1 : val; |
| 8915 | |
| 8916 | false_reg->umax_value = min(false_reg->umax_value, false_umax); |
| 8917 | true_reg->umin_value = max(true_reg->umin_value, true_umin); |
| 8918 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 8919 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8920 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8921 | case BPF_JSGE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8922 | case BPF_JSGT: |
| 8923 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8924 | if (is_jmp32) { |
| 8925 | s32 false_smax = opcode == BPF_JSGT ? sval32 : sval32 - 1; |
| 8926 | s32 true_smin = opcode == BPF_JSGT ? sval32 + 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8927 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8928 | false_reg->s32_max_value = min(false_reg->s32_max_value, false_smax); |
| 8929 | true_reg->s32_min_value = max(true_reg->s32_min_value, true_smin); |
| 8930 | } else { |
| 8931 | s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1; |
| 8932 | s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval; |
| 8933 | |
| 8934 | false_reg->smax_value = min(false_reg->smax_value, false_smax); |
| 8935 | true_reg->smin_value = max(true_reg->smin_value, true_smin); |
| 8936 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8937 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8938 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8939 | case BPF_JLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8940 | case BPF_JLT: |
| 8941 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8942 | if (is_jmp32) { |
| 8943 | u32 false_umin = opcode == BPF_JLT ? val32 : val32 + 1; |
| 8944 | u32 true_umax = opcode == BPF_JLT ? val32 - 1 : val32; |
| 8945 | |
| 8946 | false_reg->u32_min_value = max(false_reg->u32_min_value, |
| 8947 | false_umin); |
| 8948 | true_reg->u32_max_value = min(true_reg->u32_max_value, |
| 8949 | true_umax); |
| 8950 | } else { |
| 8951 | u64 false_umin = opcode == BPF_JLT ? val : val + 1; |
| 8952 | u64 true_umax = opcode == BPF_JLT ? val - 1 : val; |
| 8953 | |
| 8954 | false_reg->umin_value = max(false_reg->umin_value, false_umin); |
| 8955 | true_reg->umax_value = min(true_reg->umax_value, true_umax); |
| 8956 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8957 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8958 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8959 | case BPF_JSLE: |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8960 | case BPF_JSLT: |
| 8961 | { |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8962 | if (is_jmp32) { |
| 8963 | s32 false_smin = opcode == BPF_JSLT ? sval32 : sval32 + 1; |
| 8964 | s32 true_smax = opcode == BPF_JSLT ? sval32 - 1 : sval32; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8965 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8966 | false_reg->s32_min_value = max(false_reg->s32_min_value, false_smin); |
| 8967 | true_reg->s32_max_value = min(true_reg->s32_max_value, true_smax); |
| 8968 | } else { |
| 8969 | s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1; |
| 8970 | s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval; |
| 8971 | |
| 8972 | false_reg->smin_value = max(false_reg->smin_value, false_smin); |
| 8973 | true_reg->smax_value = min(true_reg->smax_value, true_smax); |
| 8974 | } |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 8975 | break; |
Jiong Wang | a72dafa | 2019-01-26 12:26:00 -0500 | [diff] [blame] | 8976 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8977 | default: |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 8978 | return; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8979 | } |
| 8980 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 8981 | if (is_jmp32) { |
| 8982 | false_reg->var_off = tnum_or(tnum_clear_subreg(false_64off), |
| 8983 | tnum_subreg(false_32off)); |
| 8984 | true_reg->var_off = tnum_or(tnum_clear_subreg(true_64off), |
| 8985 | tnum_subreg(true_32off)); |
| 8986 | __reg_combine_32_into_64(false_reg); |
| 8987 | __reg_combine_32_into_64(true_reg); |
| 8988 | } else { |
| 8989 | false_reg->var_off = false_64off; |
| 8990 | true_reg->var_off = true_64off; |
| 8991 | __reg_combine_64_into_32(false_reg); |
| 8992 | __reg_combine_64_into_32(true_reg); |
| 8993 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8994 | } |
| 8995 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 8996 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 8997 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 8998 | */ |
| 8999 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9000 | struct bpf_reg_state *false_reg, |
| 9001 | u64 val, u32 val32, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9002 | u8 opcode, bool is_jmp32) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9003 | { |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9004 | opcode = flip_opcode(opcode); |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 9005 | /* This uses zero as "not present in table"; luckily the zero opcode, |
| 9006 | * BPF_JA, can't get here. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9007 | */ |
Jann Horn | 0fc31b1 | 2020-03-30 18:03:24 +0200 | [diff] [blame] | 9008 | if (opcode) |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9009 | reg_set_min_max(true_reg, false_reg, val, val32, opcode, is_jmp32); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9010 | } |
| 9011 | |
| 9012 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 9013 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 9014 | struct bpf_reg_state *dst_reg) |
| 9015 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9016 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 9017 | dst_reg->umin_value); |
| 9018 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 9019 | dst_reg->umax_value); |
| 9020 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 9021 | dst_reg->smin_value); |
| 9022 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 9023 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9024 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 9025 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9026 | /* We might have learned new bounds from the var_off. */ |
| 9027 | __update_reg_bounds(src_reg); |
| 9028 | __update_reg_bounds(dst_reg); |
| 9029 | /* We might have learned something about the sign bit. */ |
| 9030 | __reg_deduce_bounds(src_reg); |
| 9031 | __reg_deduce_bounds(dst_reg); |
| 9032 | /* We might have learned some bits from the bounds. */ |
| 9033 | __reg_bound_offset(src_reg); |
| 9034 | __reg_bound_offset(dst_reg); |
| 9035 | /* Intersecting with the old var_off might have improved our bounds |
| 9036 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 9037 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 9038 | */ |
| 9039 | __update_reg_bounds(src_reg); |
| 9040 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9041 | } |
| 9042 | |
| 9043 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 9044 | struct bpf_reg_state *true_dst, |
| 9045 | struct bpf_reg_state *false_src, |
| 9046 | struct bpf_reg_state *false_dst, |
| 9047 | u8 opcode) |
| 9048 | { |
| 9049 | switch (opcode) { |
| 9050 | case BPF_JEQ: |
| 9051 | __reg_combine_min_max(true_src, true_dst); |
| 9052 | break; |
| 9053 | case BPF_JNE: |
| 9054 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9055 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 9056 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9057 | } |
| 9058 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9059 | static void mark_ptr_or_null_reg(struct bpf_func_state *state, |
| 9060 | struct bpf_reg_state *reg, u32 id, |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9061 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9062 | { |
Martin KaFai Lau | 93c230e | 2020-10-19 12:42:12 -0700 | [diff] [blame] | 9063 | if (reg_type_may_be_null(reg->type) && reg->id == id && |
| 9064 | !WARN_ON_ONCE(!reg->id)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9065 | /* Old offset (both fixed and variable parts) should |
| 9066 | * have been known-zero, because we don't allow pointer |
| 9067 | * arithmetic on pointers that might be NULL. |
| 9068 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9069 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 9070 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9071 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9072 | __mark_reg_known_zero(reg); |
| 9073 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9074 | } |
| 9075 | if (is_null) { |
| 9076 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9077 | /* We don't need id and ref_obj_id from this point |
| 9078 | * onwards anymore, thus we should better reset it, |
| 9079 | * so that state pruning has chances to take effect. |
| 9080 | */ |
| 9081 | reg->id = 0; |
| 9082 | reg->ref_obj_id = 0; |
Dmitrii Banshchikov | 4ddb741 | 2021-02-13 00:56:40 +0400 | [diff] [blame] | 9083 | |
| 9084 | return; |
| 9085 | } |
| 9086 | |
| 9087 | mark_ptr_not_null_reg(reg); |
| 9088 | |
| 9089 | if (!reg_may_point_to_spin_lock(reg)) { |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9090 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 9091 | * in release_reg_references(). |
| 9092 | * |
| 9093 | * reg->id is still used by spin_lock ptr. Other |
| 9094 | * than spin_lock ptr type, reg->id can be reset. |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9095 | */ |
| 9096 | reg->id = 0; |
| 9097 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9098 | } |
| 9099 | } |
| 9100 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9101 | static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id, |
| 9102 | bool is_null) |
| 9103 | { |
| 9104 | struct bpf_reg_state *reg; |
| 9105 | int i; |
| 9106 | |
| 9107 | for (i = 0; i < MAX_BPF_REG; i++) |
| 9108 | mark_ptr_or_null_reg(state, &state->regs[i], id, is_null); |
| 9109 | |
| 9110 | bpf_for_each_spilled_reg(i, state, reg) { |
| 9111 | if (!reg) |
| 9112 | continue; |
| 9113 | mark_ptr_or_null_reg(state, reg, id, is_null); |
| 9114 | } |
| 9115 | } |
| 9116 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9117 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 9118 | * be folded together at some point. |
| 9119 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9120 | static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno, |
| 9121 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9122 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9123 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9124 | struct bpf_reg_state *regs = state->regs; |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9125 | u32 ref_obj_id = regs[regno].ref_obj_id; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 9126 | u32 id = regs[regno].id; |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9127 | int i; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9128 | |
Martin KaFai Lau | 1b98658 | 2019-03-12 10:23:02 -0700 | [diff] [blame] | 9129 | if (ref_obj_id && ref_obj_id == id && is_null) |
| 9130 | /* regs[regno] is in the " == NULL" branch. |
| 9131 | * No one could have freed the reference state before |
| 9132 | * doing the NULL check. |
| 9133 | */ |
| 9134 | WARN_ON_ONCE(release_reference_state(state, id)); |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9135 | |
Paul Chaignon | c6a9efa | 2019-04-24 21:50:42 +0200 | [diff] [blame] | 9136 | for (i = 0; i <= vstate->curframe; i++) |
| 9137 | __mark_ptr_or_null_regs(vstate->frame[i], id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9138 | } |
| 9139 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9140 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 9141 | struct bpf_reg_state *dst_reg, |
| 9142 | struct bpf_reg_state *src_reg, |
| 9143 | struct bpf_verifier_state *this_branch, |
| 9144 | struct bpf_verifier_state *other_branch) |
| 9145 | { |
| 9146 | if (BPF_SRC(insn->code) != BPF_X) |
| 9147 | return false; |
| 9148 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9149 | /* Pointers are always 64-bit. */ |
| 9150 | if (BPF_CLASS(insn->code) == BPF_JMP32) |
| 9151 | return false; |
| 9152 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9153 | switch (BPF_OP(insn->code)) { |
| 9154 | case BPF_JGT: |
| 9155 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9156 | src_reg->type == PTR_TO_PACKET_END) || |
| 9157 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9158 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9159 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 9160 | find_good_pkt_pointers(this_branch, dst_reg, |
| 9161 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9162 | mark_pkt_end(other_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9163 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9164 | src_reg->type == PTR_TO_PACKET) || |
| 9165 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9166 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9167 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 9168 | find_good_pkt_pointers(other_branch, src_reg, |
| 9169 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9170 | mark_pkt_end(this_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9171 | } else { |
| 9172 | return false; |
| 9173 | } |
| 9174 | break; |
| 9175 | case BPF_JLT: |
| 9176 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9177 | src_reg->type == PTR_TO_PACKET_END) || |
| 9178 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9179 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9180 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 9181 | find_good_pkt_pointers(other_branch, dst_reg, |
| 9182 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9183 | mark_pkt_end(this_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9184 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9185 | src_reg->type == PTR_TO_PACKET) || |
| 9186 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9187 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9188 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 9189 | find_good_pkt_pointers(this_branch, src_reg, |
| 9190 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9191 | mark_pkt_end(other_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9192 | } else { |
| 9193 | return false; |
| 9194 | } |
| 9195 | break; |
| 9196 | case BPF_JGE: |
| 9197 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9198 | src_reg->type == PTR_TO_PACKET_END) || |
| 9199 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9200 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9201 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 9202 | find_good_pkt_pointers(this_branch, dst_reg, |
| 9203 | dst_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9204 | mark_pkt_end(other_branch, insn->dst_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9205 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9206 | src_reg->type == PTR_TO_PACKET) || |
| 9207 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9208 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9209 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 9210 | find_good_pkt_pointers(other_branch, src_reg, |
| 9211 | src_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9212 | mark_pkt_end(this_branch, insn->src_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9213 | } else { |
| 9214 | return false; |
| 9215 | } |
| 9216 | break; |
| 9217 | case BPF_JLE: |
| 9218 | if ((dst_reg->type == PTR_TO_PACKET && |
| 9219 | src_reg->type == PTR_TO_PACKET_END) || |
| 9220 | (dst_reg->type == PTR_TO_PACKET_META && |
| 9221 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 9222 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 9223 | find_good_pkt_pointers(other_branch, dst_reg, |
| 9224 | dst_reg->type, false); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9225 | mark_pkt_end(this_branch, insn->dst_reg, true); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9226 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 9227 | src_reg->type == PTR_TO_PACKET) || |
| 9228 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 9229 | src_reg->type == PTR_TO_PACKET_META)) { |
| 9230 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 9231 | find_good_pkt_pointers(this_branch, src_reg, |
| 9232 | src_reg->type, true); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9233 | mark_pkt_end(other_branch, insn->src_reg, false); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9234 | } else { |
| 9235 | return false; |
| 9236 | } |
| 9237 | break; |
| 9238 | default: |
| 9239 | return false; |
| 9240 | } |
| 9241 | |
| 9242 | return true; |
| 9243 | } |
| 9244 | |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9245 | static void find_equal_scalars(struct bpf_verifier_state *vstate, |
| 9246 | struct bpf_reg_state *known_reg) |
| 9247 | { |
| 9248 | struct bpf_func_state *state; |
| 9249 | struct bpf_reg_state *reg; |
| 9250 | int i, j; |
| 9251 | |
| 9252 | for (i = 0; i <= vstate->curframe; i++) { |
| 9253 | state = vstate->frame[i]; |
| 9254 | for (j = 0; j < MAX_BPF_REG; j++) { |
| 9255 | reg = &state->regs[j]; |
| 9256 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 9257 | *reg = *known_reg; |
| 9258 | } |
| 9259 | |
| 9260 | bpf_for_each_spilled_reg(j, state, reg) { |
| 9261 | if (!reg) |
| 9262 | continue; |
| 9263 | if (reg->type == SCALAR_VALUE && reg->id == known_reg->id) |
| 9264 | *reg = *known_reg; |
| 9265 | } |
| 9266 | } |
| 9267 | } |
| 9268 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9269 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9270 | struct bpf_insn *insn, int *insn_idx) |
| 9271 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9272 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 9273 | struct bpf_verifier_state *other_branch; |
| 9274 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9275 | struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9276 | u8 opcode = BPF_OP(insn->code); |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9277 | bool is_jmp32; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9278 | int pred = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9279 | int err; |
| 9280 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9281 | /* Only conditional jumps are expected to reach here. */ |
| 9282 | if (opcode == BPF_JA || opcode > BPF_JSLE) { |
| 9283 | verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9284 | return -EINVAL; |
| 9285 | } |
| 9286 | |
| 9287 | if (BPF_SRC(insn->code) == BPF_X) { |
| 9288 | if (insn->imm != 0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9289 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9290 | return -EINVAL; |
| 9291 | } |
| 9292 | |
| 9293 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9294 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9295 | if (err) |
| 9296 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9297 | |
| 9298 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9299 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9300 | insn->src_reg); |
| 9301 | return -EACCES; |
| 9302 | } |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9303 | src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9304 | } else { |
| 9305 | if (insn->src_reg != BPF_REG_0) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9306 | verbose(env, "BPF_JMP/JMP32 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9307 | return -EINVAL; |
| 9308 | } |
| 9309 | } |
| 9310 | |
| 9311 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9312 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9313 | if (err) |
| 9314 | return err; |
| 9315 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9316 | dst_reg = ®s[insn->dst_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9317 | is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32; |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9318 | |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9319 | if (BPF_SRC(insn->code) == BPF_K) { |
| 9320 | pred = is_branch_taken(dst_reg, insn->imm, opcode, is_jmp32); |
| 9321 | } else if (src_reg->type == SCALAR_VALUE && |
| 9322 | is_jmp32 && tnum_is_const(tnum_subreg(src_reg->var_off))) { |
| 9323 | pred = is_branch_taken(dst_reg, |
| 9324 | tnum_subreg(src_reg->var_off).value, |
| 9325 | opcode, |
| 9326 | is_jmp32); |
| 9327 | } else if (src_reg->type == SCALAR_VALUE && |
| 9328 | !is_jmp32 && tnum_is_const(src_reg->var_off)) { |
| 9329 | pred = is_branch_taken(dst_reg, |
| 9330 | src_reg->var_off.value, |
| 9331 | opcode, |
| 9332 | is_jmp32); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9333 | } else if (reg_is_pkt_pointer_any(dst_reg) && |
| 9334 | reg_is_pkt_pointer_any(src_reg) && |
| 9335 | !is_jmp32) { |
| 9336 | pred = is_pkt_ptr_branch_taken(dst_reg, src_reg, opcode); |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9337 | } |
| 9338 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9339 | if (pred >= 0) { |
John Fastabend | cac616d | 2020-05-21 13:07:26 -0700 | [diff] [blame] | 9340 | /* If we get here with a dst_reg pointer type it is because |
| 9341 | * above is_branch_taken() special cased the 0 comparison. |
| 9342 | */ |
| 9343 | if (!__is_pointer_value(false, dst_reg)) |
| 9344 | err = mark_chain_precision(env, insn->dst_reg); |
Alexei Starovoitov | 6d94e74 | 2020-11-10 19:12:11 -0800 | [diff] [blame] | 9345 | if (BPF_SRC(insn->code) == BPF_X && !err && |
| 9346 | !__is_pointer_value(false, src_reg)) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 9347 | err = mark_chain_precision(env, insn->src_reg); |
| 9348 | if (err) |
| 9349 | return err; |
| 9350 | } |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9351 | |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9352 | if (pred == 1) { |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9353 | /* Only follow the goto, ignore fall-through. If needed, push |
| 9354 | * the fall-through branch for simulation under speculative |
| 9355 | * execution. |
| 9356 | */ |
| 9357 | if (!env->bypass_spec_v1 && |
| 9358 | !sanitize_speculative_path(env, insn, *insn_idx + 1, |
| 9359 | *insn_idx)) |
| 9360 | return -EFAULT; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9361 | *insn_idx += insn->off; |
| 9362 | return 0; |
| 9363 | } else if (pred == 0) { |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9364 | /* Only follow the fall-through branch, since that's where the |
| 9365 | * program will go. If needed, push the goto branch for |
| 9366 | * simulation under speculative execution. |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9367 | */ |
Daniel Borkmann | 9183671a | 2021-05-28 15:47:32 +0000 | [diff] [blame] | 9368 | if (!env->bypass_spec_v1 && |
| 9369 | !sanitize_speculative_path(env, insn, |
| 9370 | *insn_idx + insn->off + 1, |
| 9371 | *insn_idx)) |
| 9372 | return -EFAULT; |
Alexei Starovoitov | fb8d251 | 2019-06-15 12:12:19 -0700 | [diff] [blame] | 9373 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9374 | } |
| 9375 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 9376 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx, |
| 9377 | false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9378 | if (!other_branch) |
| 9379 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9380 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9381 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9382 | /* detect if we are comparing against a constant value so we can adjust |
| 9383 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9384 | * this is only legit if both are scalars (or pointers to the same |
| 9385 | * object, I suppose, but we don't support that right now), because |
| 9386 | * otherwise the different base pointers mean the offsets aren't |
| 9387 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9388 | */ |
| 9389 | if (BPF_SRC(insn->code) == BPF_X) { |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9390 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9391 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9392 | if (dst_reg->type == SCALAR_VALUE && |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9393 | src_reg->type == SCALAR_VALUE) { |
| 9394 | if (tnum_is_const(src_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9395 | (is_jmp32 && |
| 9396 | tnum_is_const(tnum_subreg(src_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9397 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9398 | dst_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9399 | src_reg->var_off.value, |
| 9400 | tnum_subreg(src_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9401 | opcode, is_jmp32); |
| 9402 | else if (tnum_is_const(dst_reg->var_off) || |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9403 | (is_jmp32 && |
| 9404 | tnum_is_const(tnum_subreg(dst_reg->var_off)))) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9405 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9406 | src_reg, |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9407 | dst_reg->var_off.value, |
| 9408 | tnum_subreg(dst_reg->var_off).value, |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9409 | opcode, is_jmp32); |
| 9410 | else if (!is_jmp32 && |
| 9411 | (opcode == BPF_JEQ || opcode == BPF_JNE)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9412 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9413 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 9414 | &other_branch_regs[insn->dst_reg], |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9415 | src_reg, dst_reg, opcode); |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 9416 | if (src_reg->id && |
| 9417 | !WARN_ON_ONCE(src_reg->id != other_branch_regs[insn->src_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9418 | find_equal_scalars(this_branch, src_reg); |
| 9419 | find_equal_scalars(other_branch, &other_branch_regs[insn->src_reg]); |
| 9420 | } |
| 9421 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 9422 | } |
| 9423 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 9424 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
John Fastabend | 3f50f13 | 2020-03-30 14:36:39 -0700 | [diff] [blame] | 9425 | dst_reg, insn->imm, (u32)insn->imm, |
| 9426 | opcode, is_jmp32); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 9427 | } |
| 9428 | |
Alexei Starovoitov | e688c3d | 2020-10-14 10:56:08 -0700 | [diff] [blame] | 9429 | if (dst_reg->type == SCALAR_VALUE && dst_reg->id && |
| 9430 | !WARN_ON_ONCE(dst_reg->id != other_branch_regs[insn->dst_reg].id)) { |
Alexei Starovoitov | 7574883 | 2020-10-08 18:12:37 -0700 | [diff] [blame] | 9431 | find_equal_scalars(this_branch, dst_reg); |
| 9432 | find_equal_scalars(other_branch, &other_branch_regs[insn->dst_reg]); |
| 9433 | } |
| 9434 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 9435 | /* detect if R == 0 where R is returned from bpf_map_lookup_elem(). |
| 9436 | * NOTE: these optimizations below are related with pointer comparison |
| 9437 | * which will never be JMP32. |
| 9438 | */ |
| 9439 | if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 9440 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9441 | reg_type_may_be_null(dst_reg->type)) { |
| 9442 | /* Mark all identical registers in each branch as either |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 9443 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 9444 | */ |
Joe Stringer | 840b961 | 2018-10-02 13:35:32 -0700 | [diff] [blame] | 9445 | mark_ptr_or_null_regs(this_branch, insn->dst_reg, |
| 9446 | opcode == BPF_JNE); |
| 9447 | mark_ptr_or_null_regs(other_branch, insn->dst_reg, |
| 9448 | opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 9449 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 9450 | this_branch, other_branch) && |
| 9451 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9452 | verbose(env, "R%d pointer comparison prohibited\n", |
| 9453 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 9454 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9455 | } |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 9456 | if (env->log.level & BPF_LOG_LEVEL) |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 9457 | print_insn_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9458 | return 0; |
| 9459 | } |
| 9460 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9461 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9462 | static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9463 | { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9464 | struct bpf_insn_aux_data *aux = cur_aux(env); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9465 | struct bpf_reg_state *regs = cur_regs(env); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9466 | struct bpf_reg_state *dst_reg; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9467 | struct bpf_map *map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9468 | int err; |
| 9469 | |
| 9470 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9471 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9472 | return -EINVAL; |
| 9473 | } |
| 9474 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9475 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9476 | return -EINVAL; |
| 9477 | } |
| 9478 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9479 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9480 | if (err) |
| 9481 | return err; |
| 9482 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9483 | dst_reg = ®s[insn->dst_reg]; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9484 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9485 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 9486 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9487 | dst_reg->type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 9488 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9489 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 9490 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9491 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9492 | if (insn->src_reg == BPF_PSEUDO_BTF_ID) { |
| 9493 | mark_reg_known_zero(env, regs, insn->dst_reg); |
| 9494 | |
| 9495 | dst_reg->type = aux->btf_var.reg_type; |
| 9496 | switch (dst_reg->type) { |
| 9497 | case PTR_TO_MEM: |
| 9498 | dst_reg->mem_size = aux->btf_var.mem_size; |
| 9499 | break; |
| 9500 | case PTR_TO_BTF_ID: |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 9501 | case PTR_TO_PERCPU_BTF_ID: |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 9502 | dst_reg->btf = aux->btf_var.btf; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9503 | dst_reg->btf_id = aux->btf_var.btf_id; |
| 9504 | break; |
| 9505 | default: |
| 9506 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9507 | return -EFAULT; |
| 9508 | } |
| 9509 | return 0; |
| 9510 | } |
| 9511 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9512 | if (insn->src_reg == BPF_PSEUDO_FUNC) { |
| 9513 | struct bpf_prog_aux *aux = env->prog->aux; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 9514 | u32 subprogno = find_subprog(env, |
| 9515 | env->insn_idx + insn->imm + 1); |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9516 | |
| 9517 | if (!aux->func_info) { |
| 9518 | verbose(env, "missing btf func_info\n"); |
| 9519 | return -EINVAL; |
| 9520 | } |
| 9521 | if (aux->func_info_aux[subprogno].linkage != BTF_FUNC_STATIC) { |
| 9522 | verbose(env, "callback function not static\n"); |
| 9523 | return -EINVAL; |
| 9524 | } |
| 9525 | |
| 9526 | dst_reg->type = PTR_TO_FUNC; |
| 9527 | dst_reg->subprogno = subprogno; |
| 9528 | return 0; |
| 9529 | } |
| 9530 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9531 | map = env->used_maps[aux->map_index]; |
| 9532 | mark_reg_known_zero(env, regs, insn->dst_reg); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9533 | dst_reg->map_ptr = map; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9534 | |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 9535 | if (insn->src_reg == BPF_PSEUDO_MAP_VALUE || |
| 9536 | insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9537 | dst_reg->type = PTR_TO_MAP_VALUE; |
| 9538 | dst_reg->off = aux->map_off; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9539 | if (map_value_has_spin_lock(map)) |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9540 | dst_reg->id = ++env->id_gen; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 9541 | } else if (insn->src_reg == BPF_PSEUDO_MAP_FD || |
| 9542 | insn->src_reg == BPF_PSEUDO_MAP_IDX) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 9543 | dst_reg->type = CONST_PTR_TO_MAP; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 9544 | } else { |
| 9545 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9546 | return -EINVAL; |
| 9547 | } |
| 9548 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 9549 | return 0; |
| 9550 | } |
| 9551 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 9552 | static bool may_access_skb(enum bpf_prog_type type) |
| 9553 | { |
| 9554 | switch (type) { |
| 9555 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 9556 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 9557 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 9558 | return true; |
| 9559 | default: |
| 9560 | return false; |
| 9561 | } |
| 9562 | } |
| 9563 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9564 | /* verify safety of LD_ABS|LD_IND instructions: |
| 9565 | * - they can only appear in the programs where ctx == skb |
| 9566 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 9567 | * preserve R6-R9, and store return value into R0 |
| 9568 | * |
| 9569 | * Implicit input: |
| 9570 | * ctx == skb == R6 == CTX |
| 9571 | * |
| 9572 | * Explicit input: |
| 9573 | * SRC == any register |
| 9574 | * IMM == 32-bit immediate |
| 9575 | * |
| 9576 | * Output: |
| 9577 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 9578 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 9579 | static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9580 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 9581 | struct bpf_reg_state *regs = cur_regs(env); |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9582 | static const int ctx_reg = BPF_REG_6; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9583 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9584 | int i, err; |
| 9585 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9586 | if (!may_access_skb(resolve_prog_type(env->prog))) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9587 | verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9588 | return -EINVAL; |
| 9589 | } |
| 9590 | |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 9591 | if (!env->ops->gen_ld_abs) { |
| 9592 | verbose(env, "bpf verifier is misconfigured\n"); |
| 9593 | return -EINVAL; |
| 9594 | } |
| 9595 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9596 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 9597 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9598 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9599 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9600 | return -EINVAL; |
| 9601 | } |
| 9602 | |
| 9603 | /* check whether implicit source operand (register R6) is readable */ |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9604 | err = check_reg_arg(env, ctx_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9605 | if (err) |
| 9606 | return err; |
| 9607 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 9608 | /* Disallow usage of BPF_LD_[ABS|IND] with reference tracking, as |
| 9609 | * gen_ld_abs() may terminate the program at runtime, leading to |
| 9610 | * reference leak. |
| 9611 | */ |
| 9612 | err = check_reference_leak(env); |
| 9613 | if (err) { |
| 9614 | verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n"); |
| 9615 | return err; |
| 9616 | } |
| 9617 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 9618 | if (env->cur_state->active_spin_lock) { |
| 9619 | verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n"); |
| 9620 | return -EINVAL; |
| 9621 | } |
| 9622 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9623 | if (regs[ctx_reg].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9624 | verbose(env, |
| 9625 | "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9626 | return -EINVAL; |
| 9627 | } |
| 9628 | |
| 9629 | if (mode == BPF_IND) { |
| 9630 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9631 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9632 | if (err) |
| 9633 | return err; |
| 9634 | } |
| 9635 | |
Daniel Borkmann | 6d4f151 | 2020-01-06 22:51:57 +0100 | [diff] [blame] | 9636 | err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg); |
| 9637 | if (err < 0) |
| 9638 | return err; |
| 9639 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9640 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9641 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9642 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9643 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 9644 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9645 | |
| 9646 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 9647 | * the value fetched from the packet. |
| 9648 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9649 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9650 | mark_reg_unknown(env, regs, BPF_REG_0); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 9651 | /* ld_abs load up to 32-bit skb data. */ |
| 9652 | regs[BPF_REG_0].subreg_def = env->insn_idx + 1; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 9653 | return 0; |
| 9654 | } |
| 9655 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9656 | static int check_return_code(struct bpf_verifier_env *env) |
| 9657 | { |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9658 | struct tnum enforce_attach_type_range = tnum_unknown; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9659 | const struct bpf_prog *prog = env->prog; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9660 | struct bpf_reg_state *reg; |
| 9661 | struct tnum range = tnum_range(0, 1); |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9662 | enum bpf_prog_type prog_type = resolve_prog_type(env->prog); |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9663 | int err; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9664 | struct bpf_func_state *frame = env->cur_state->frame[0]; |
| 9665 | const bool is_subprog = frame->subprogno; |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9666 | |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 9667 | /* LSM and struct_ops func-ptr's return type could be "void" */ |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9668 | if (!is_subprog && |
| 9669 | (prog_type == BPF_PROG_TYPE_STRUCT_OPS || |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9670 | prog_type == BPF_PROG_TYPE_LSM) && |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9671 | !prog->aux->attach_func_proto->type) |
| 9672 | return 0; |
| 9673 | |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 9674 | /* eBPF calling convention is such that R0 is used |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 9675 | * to return the value from eBPF program. |
| 9676 | * Make sure that it's readable at this time |
| 9677 | * of bpf_exit, which means that program wrote |
| 9678 | * something into it earlier |
| 9679 | */ |
| 9680 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
| 9681 | if (err) |
| 9682 | return err; |
| 9683 | |
| 9684 | if (is_pointer_value(env, BPF_REG_0)) { |
| 9685 | verbose(env, "R0 leaks addr as return value\n"); |
| 9686 | return -EACCES; |
| 9687 | } |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9688 | |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9689 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9690 | |
| 9691 | if (frame->in_async_callback_fn) { |
| 9692 | /* enforce return zero from async callbacks like timer */ |
| 9693 | if (reg->type != SCALAR_VALUE) { |
| 9694 | verbose(env, "In async callback the register R0 is not a known value (%s)\n", |
| 9695 | reg_type_str[reg->type]); |
| 9696 | return -EINVAL; |
| 9697 | } |
| 9698 | |
| 9699 | if (!tnum_in(tnum_const(0), reg->var_off)) { |
| 9700 | verbose_invalid_scalar(env, reg, &range, "async callback", "R0"); |
| 9701 | return -EINVAL; |
| 9702 | } |
| 9703 | return 0; |
| 9704 | } |
| 9705 | |
Dmitrii Banshchikov | f782e2c | 2020-11-13 17:17:56 +0000 | [diff] [blame] | 9706 | if (is_subprog) { |
| 9707 | if (reg->type != SCALAR_VALUE) { |
| 9708 | verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n", |
| 9709 | reg_type_str[reg->type]); |
| 9710 | return -EINVAL; |
| 9711 | } |
| 9712 | return 0; |
| 9713 | } |
| 9714 | |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 9715 | switch (prog_type) { |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 9716 | case BPF_PROG_TYPE_CGROUP_SOCK_ADDR: |
| 9717 | if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG || |
Daniel Borkmann | 1b66d25 | 2020-05-19 00:45:45 +0200 | [diff] [blame] | 9718 | env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG || |
| 9719 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETPEERNAME || |
| 9720 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETPEERNAME || |
| 9721 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 9722 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
Daniel Borkmann | 983695f | 2019-06-07 01:48:57 +0200 | [diff] [blame] | 9723 | range = tnum_range(1, 1); |
Stanislav Fomichev | 7724121 | 2021-01-27 11:31:39 -0800 | [diff] [blame] | 9724 | if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
| 9725 | env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) |
| 9726 | range = tnum_range(0, 3); |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 9727 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9728 | case BPF_PROG_TYPE_CGROUP_SKB: |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9729 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| 9730 | range = tnum_range(0, 3); |
| 9731 | enforce_attach_type_range = tnum_range(2, 3); |
| 9732 | } |
Gustavo A. R. Silva | ed4ed40 | 2019-07-11 11:22:33 -0500 | [diff] [blame] | 9733 | break; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9734 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 9735 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 9736 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Andrey Ignatov | 7b146ce | 2019-02-27 12:59:24 -0800 | [diff] [blame] | 9737 | case BPF_PROG_TYPE_CGROUP_SYSCTL: |
Stanislav Fomichev | 0d01da6 | 2019-06-27 13:38:47 -0700 | [diff] [blame] | 9738 | case BPF_PROG_TYPE_CGROUP_SOCKOPT: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9739 | break; |
Alexei Starovoitov | 15ab09b | 2019-10-28 20:24:26 -0700 | [diff] [blame] | 9740 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 9741 | if (!env->prog->aux->attach_btf_id) |
| 9742 | return 0; |
| 9743 | range = tnum_const(0); |
| 9744 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9745 | case BPF_PROG_TYPE_TRACING: |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9746 | switch (env->prog->expected_attach_type) { |
| 9747 | case BPF_TRACE_FENTRY: |
| 9748 | case BPF_TRACE_FEXIT: |
| 9749 | range = tnum_const(0); |
| 9750 | break; |
| 9751 | case BPF_TRACE_RAW_TP: |
| 9752 | case BPF_MODIFY_RETURN: |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9753 | return 0; |
Daniel Borkmann | 2ec0616 | 2020-05-16 00:39:18 +0200 | [diff] [blame] | 9754 | case BPF_TRACE_ITER: |
| 9755 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9756 | default: |
| 9757 | return -ENOTSUPP; |
| 9758 | } |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 9759 | break; |
Jakub Sitnicki | e9ddbb7 | 2020-07-17 12:35:23 +0200 | [diff] [blame] | 9760 | case BPF_PROG_TYPE_SK_LOOKUP: |
| 9761 | range = tnum_range(SK_DROP, SK_PASS); |
| 9762 | break; |
Yonghong Song | e92888c7 | 2020-05-13 22:32:05 -0700 | [diff] [blame] | 9763 | case BPF_PROG_TYPE_EXT: |
| 9764 | /* freplace program can return anything as its return value |
| 9765 | * depends on the to-be-replaced kernel func or bpf program. |
| 9766 | */ |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9767 | default: |
| 9768 | return 0; |
| 9769 | } |
| 9770 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9771 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9772 | verbose(env, "At program exit the register R0 is not a known value (%s)\n", |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9773 | reg_type_str[reg->type]); |
| 9774 | return -EINVAL; |
| 9775 | } |
| 9776 | |
| 9777 | if (!tnum_in(range, reg->var_off)) { |
Yonghong Song | bc2591d | 2021-02-26 12:49:22 -0800 | [diff] [blame] | 9778 | verbose_invalid_scalar(env, reg, &range, "program exit", "R0"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9779 | return -EINVAL; |
| 9780 | } |
brakmo | 5cf1e91 | 2019-05-28 16:59:36 -0700 | [diff] [blame] | 9781 | |
| 9782 | if (!tnum_is_unknown(enforce_attach_type_range) && |
| 9783 | tnum_in(enforce_attach_type_range, reg->var_off)) |
| 9784 | env->prog->enforce_expected_attach_type = 1; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 9785 | return 0; |
| 9786 | } |
| 9787 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9788 | /* non-recursive DFS pseudo code |
| 9789 | * 1 procedure DFS-iterative(G,v): |
| 9790 | * 2 label v as discovered |
| 9791 | * 3 let S be a stack |
| 9792 | * 4 S.push(v) |
| 9793 | * 5 while S is not empty |
| 9794 | * 6 t <- S.pop() |
| 9795 | * 7 if t is what we're looking for: |
| 9796 | * 8 return t |
| 9797 | * 9 for all edges e in G.adjacentEdges(t) do |
| 9798 | * 10 if edge e is already labelled |
| 9799 | * 11 continue with the next edge |
| 9800 | * 12 w <- G.adjacentVertex(t,e) |
| 9801 | * 13 if vertex w is not discovered and not explored |
| 9802 | * 14 label e as tree-edge |
| 9803 | * 15 label w as discovered |
| 9804 | * 16 S.push(w) |
| 9805 | * 17 continue at 5 |
| 9806 | * 18 else if vertex w is discovered |
| 9807 | * 19 label e as back-edge |
| 9808 | * 20 else |
| 9809 | * 21 // vertex w is explored |
| 9810 | * 22 label e as forward- or cross-edge |
| 9811 | * 23 label t as explored |
| 9812 | * 24 S.pop() |
| 9813 | * |
| 9814 | * convention: |
| 9815 | * 0x10 - discovered |
| 9816 | * 0x11 - discovered and fall-through edge labelled |
| 9817 | * 0x12 - discovered and fall-through and branch edges labelled |
| 9818 | * 0x20 - explored |
| 9819 | */ |
| 9820 | |
| 9821 | enum { |
| 9822 | DISCOVERED = 0x10, |
| 9823 | EXPLORED = 0x20, |
| 9824 | FALLTHROUGH = 1, |
| 9825 | BRANCH = 2, |
| 9826 | }; |
| 9827 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9828 | static u32 state_htab_size(struct bpf_verifier_env *env) |
| 9829 | { |
| 9830 | return env->prog->len; |
| 9831 | } |
| 9832 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9833 | static struct bpf_verifier_state_list **explored_state( |
| 9834 | struct bpf_verifier_env *env, |
| 9835 | int idx) |
| 9836 | { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 9837 | struct bpf_verifier_state *cur = env->cur_state; |
| 9838 | struct bpf_func_state *state = cur->frame[cur->curframe]; |
| 9839 | |
| 9840 | return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)]; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9841 | } |
| 9842 | |
| 9843 | static void init_explored_state(struct bpf_verifier_env *env, int idx) |
| 9844 | { |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 9845 | env->insn_aux_data[idx].prune_point = true; |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9846 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9847 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9848 | enum { |
| 9849 | DONE_EXPLORING = 0, |
| 9850 | KEEP_EXPLORING = 1, |
| 9851 | }; |
| 9852 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9853 | /* t, w, e - match pseudo-code above: |
| 9854 | * t - index of current instruction |
| 9855 | * w - next instruction |
| 9856 | * e - edge |
| 9857 | */ |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 9858 | static int push_insn(int t, int w, int e, struct bpf_verifier_env *env, |
| 9859 | bool loop_ok) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9860 | { |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9861 | int *insn_stack = env->cfg.insn_stack; |
| 9862 | int *insn_state = env->cfg.insn_state; |
| 9863 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9864 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9865 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9866 | |
| 9867 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9868 | return DONE_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9869 | |
| 9870 | if (w < 0 || w >= env->prog->len) { |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9871 | verbose_linfo(env, t, "%d: ", t); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9872 | verbose(env, "jump out of range from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9873 | return -EINVAL; |
| 9874 | } |
| 9875 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9876 | if (e == BRANCH) |
| 9877 | /* mark branch target for state pruning */ |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 9878 | init_explored_state(env, w); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 9879 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9880 | if (insn_state[w] == 0) { |
| 9881 | /* tree-edge */ |
| 9882 | insn_state[t] = DISCOVERED | e; |
| 9883 | insn_state[w] = DISCOVERED; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9884 | if (env->cfg.cur_stack >= env->prog->len) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9885 | return -E2BIG; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 9886 | insn_stack[env->cfg.cur_stack++] = w; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9887 | return KEEP_EXPLORING; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9888 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 9889 | if (loop_ok && env->bpf_capable) |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9890 | return DONE_EXPLORING; |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 9891 | verbose_linfo(env, t, "%d: ", t); |
| 9892 | verbose_linfo(env, w, "%d: ", w); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9893 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9894 | return -EINVAL; |
| 9895 | } else if (insn_state[w] == EXPLORED) { |
| 9896 | /* forward- or cross-edge */ |
| 9897 | insn_state[t] = DISCOVERED | e; |
| 9898 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 9899 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9900 | return -EFAULT; |
| 9901 | } |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9902 | return DONE_EXPLORING; |
| 9903 | } |
| 9904 | |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9905 | static int visit_func_call_insn(int t, int insn_cnt, |
| 9906 | struct bpf_insn *insns, |
| 9907 | struct bpf_verifier_env *env, |
| 9908 | bool visit_callee) |
| 9909 | { |
| 9910 | int ret; |
| 9911 | |
| 9912 | ret = push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 9913 | if (ret) |
| 9914 | return ret; |
| 9915 | |
| 9916 | if (t + 1 < insn_cnt) |
| 9917 | init_explored_state(env, t + 1); |
| 9918 | if (visit_callee) { |
| 9919 | init_explored_state(env, t); |
Alexei Starovoitov | 86fc6ee | 2021-07-14 17:54:13 -0700 | [diff] [blame] | 9920 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env, |
| 9921 | /* It's ok to allow recursion from CFG point of |
| 9922 | * view. __check_func_call() will do the actual |
| 9923 | * check. |
| 9924 | */ |
| 9925 | bpf_pseudo_func(insns + t)); |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9926 | } |
| 9927 | return ret; |
| 9928 | } |
| 9929 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9930 | /* Visits the instruction at index t and returns one of the following: |
| 9931 | * < 0 - an error occurred |
| 9932 | * DONE_EXPLORING - the instruction was fully explored |
| 9933 | * KEEP_EXPLORING - there is still work to be done before it is fully explored |
| 9934 | */ |
| 9935 | static int visit_insn(int t, int insn_cnt, struct bpf_verifier_env *env) |
| 9936 | { |
| 9937 | struct bpf_insn *insns = env->prog->insnsi; |
| 9938 | int ret; |
| 9939 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 9940 | if (bpf_pseudo_func(insns + t)) |
| 9941 | return visit_func_call_insn(t, insn_cnt, insns, env, true); |
| 9942 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9943 | /* All non-branch instructions have a single fall-through edge. */ |
| 9944 | if (BPF_CLASS(insns[t].code) != BPF_JMP && |
| 9945 | BPF_CLASS(insns[t].code) != BPF_JMP32) |
| 9946 | return push_insn(t, t + 1, FALLTHROUGH, env, false); |
| 9947 | |
| 9948 | switch (BPF_OP(insns[t].code)) { |
| 9949 | case BPF_EXIT: |
| 9950 | return DONE_EXPLORING; |
| 9951 | |
| 9952 | case BPF_CALL: |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 9953 | if (insns[t].imm == BPF_FUNC_timer_set_callback) |
| 9954 | /* Mark this call insn to trigger is_state_visited() check |
| 9955 | * before call itself is processed by __check_func_call(). |
| 9956 | * Otherwise new async state will be pushed for further |
| 9957 | * exploration. |
| 9958 | */ |
| 9959 | init_explored_state(env, t); |
Yonghong Song | efdb22d | 2021-02-26 12:49:20 -0800 | [diff] [blame] | 9960 | return visit_func_call_insn(t, insn_cnt, insns, env, |
| 9961 | insns[t].src_reg == BPF_PSEUDO_CALL); |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 9962 | |
| 9963 | case BPF_JA: |
| 9964 | if (BPF_SRC(insns[t].code) != BPF_K) |
| 9965 | return -EINVAL; |
| 9966 | |
| 9967 | /* unconditional jump with single edge */ |
| 9968 | ret = push_insn(t, t + insns[t].off + 1, FALLTHROUGH, env, |
| 9969 | true); |
| 9970 | if (ret) |
| 9971 | return ret; |
| 9972 | |
| 9973 | /* unconditional jmp is not a good pruning point, |
| 9974 | * but it's marked, since backtracking needs |
| 9975 | * to record jmp history in is_state_visited(). |
| 9976 | */ |
| 9977 | init_explored_state(env, t + insns[t].off + 1); |
| 9978 | /* tell verifier to check for equivalent states |
| 9979 | * after every call and jump |
| 9980 | */ |
| 9981 | if (t + 1 < insn_cnt) |
| 9982 | init_explored_state(env, t + 1); |
| 9983 | |
| 9984 | return ret; |
| 9985 | |
| 9986 | default: |
| 9987 | /* conditional jump with two edges */ |
| 9988 | init_explored_state(env, t); |
| 9989 | ret = push_insn(t, t + 1, FALLTHROUGH, env, true); |
| 9990 | if (ret) |
| 9991 | return ret; |
| 9992 | |
| 9993 | return push_insn(t, t + insns[t].off + 1, BRANCH, env, true); |
| 9994 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 9995 | } |
| 9996 | |
| 9997 | /* non-recursive depth-first-search to detect loops in BPF program |
| 9998 | * loop == back-edge in directed graph |
| 9999 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 10000 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10001 | { |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10002 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10003 | int *insn_stack, *insn_state; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10004 | int ret = 0; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10005 | int i; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10006 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10007 | insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10008 | if (!insn_state) |
| 10009 | return -ENOMEM; |
| 10010 | |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10011 | insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10012 | if (!insn_stack) { |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 10013 | kvfree(insn_state); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10014 | return -ENOMEM; |
| 10015 | } |
| 10016 | |
| 10017 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 10018 | insn_stack[0] = 0; /* 0 is the first instruction */ |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10019 | env->cfg.cur_stack = 1; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10020 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10021 | while (env->cfg.cur_stack > 0) { |
| 10022 | int t = insn_stack[env->cfg.cur_stack - 1]; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10023 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10024 | ret = visit_insn(t, insn_cnt, env); |
| 10025 | switch (ret) { |
| 10026 | case DONE_EXPLORING: |
| 10027 | insn_state[t] = EXPLORED; |
| 10028 | env->cfg.cur_stack--; |
| 10029 | break; |
| 10030 | case KEEP_EXPLORING: |
| 10031 | break; |
| 10032 | default: |
| 10033 | if (ret > 0) { |
| 10034 | verbose(env, "visit_insn internal bug\n"); |
| 10035 | ret = -EFAULT; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 10036 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10037 | goto err_free; |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10038 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10039 | } |
| 10040 | |
Wedson Almeida Filho | 59e2e27 | 2020-11-21 01:55:09 +0000 | [diff] [blame] | 10041 | if (env->cfg.cur_stack < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10042 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10043 | ret = -EFAULT; |
| 10044 | goto err_free; |
| 10045 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10046 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10047 | for (i = 0; i < insn_cnt; i++) { |
| 10048 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 10049 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10050 | ret = -EINVAL; |
| 10051 | goto err_free; |
| 10052 | } |
| 10053 | } |
| 10054 | ret = 0; /* cfg looks good */ |
| 10055 | |
| 10056 | err_free: |
Alexei Starovoitov | 71dde68 | 2019-04-01 21:27:43 -0700 | [diff] [blame] | 10057 | kvfree(insn_state); |
| 10058 | kvfree(insn_stack); |
Alexei Starovoitov | 7df737e | 2019-04-19 07:44:54 -0700 | [diff] [blame] | 10059 | env->cfg.insn_state = env->cfg.insn_stack = NULL; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 10060 | return ret; |
| 10061 | } |
| 10062 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10063 | static int check_abnormal_return(struct bpf_verifier_env *env) |
| 10064 | { |
| 10065 | int i; |
| 10066 | |
| 10067 | for (i = 1; i < env->subprog_cnt; i++) { |
| 10068 | if (env->subprog_info[i].has_ld_abs) { |
| 10069 | verbose(env, "LD_ABS is not allowed in subprogs without BTF\n"); |
| 10070 | return -EINVAL; |
| 10071 | } |
| 10072 | if (env->subprog_info[i].has_tail_call) { |
| 10073 | verbose(env, "tail_call is not allowed in subprogs without BTF\n"); |
| 10074 | return -EINVAL; |
| 10075 | } |
| 10076 | } |
| 10077 | return 0; |
| 10078 | } |
| 10079 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10080 | /* The minimum supported BTF func info size */ |
| 10081 | #define MIN_BPF_FUNCINFO_SIZE 8 |
| 10082 | #define MAX_FUNCINFO_REC_SIZE 252 |
| 10083 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10084 | static int check_btf_func(struct bpf_verifier_env *env, |
| 10085 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10086 | bpfptr_t uattr) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10087 | { |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10088 | const struct btf_type *type, *func_proto, *ret_type; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 10089 | u32 i, nfuncs, urec_size, min_size; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10090 | u32 krec_size = sizeof(struct bpf_func_info); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10091 | struct bpf_func_info *krecord; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10092 | struct bpf_func_info_aux *info_aux = NULL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10093 | struct bpf_prog *prog; |
| 10094 | const struct btf *btf; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10095 | bpfptr_t urecord; |
Peter Oskolkov | d0b2818 | 2019-01-16 10:43:01 -0800 | [diff] [blame] | 10096 | u32 prev_offset = 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10097 | bool scalar_return; |
Dan Carpenter | e7ed83d | 2020-06-04 11:54:36 +0300 | [diff] [blame] | 10098 | int ret = -ENOMEM; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10099 | |
| 10100 | nfuncs = attr->func_info_cnt; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10101 | if (!nfuncs) { |
| 10102 | if (check_abnormal_return(env)) |
| 10103 | return -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10104 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10105 | } |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10106 | |
| 10107 | if (nfuncs != env->subprog_cnt) { |
| 10108 | verbose(env, "number of funcs in func_info doesn't match number of subprogs\n"); |
| 10109 | return -EINVAL; |
| 10110 | } |
| 10111 | |
| 10112 | urec_size = attr->func_info_rec_size; |
| 10113 | if (urec_size < MIN_BPF_FUNCINFO_SIZE || |
| 10114 | urec_size > MAX_FUNCINFO_REC_SIZE || |
| 10115 | urec_size % sizeof(u32)) { |
| 10116 | verbose(env, "invalid func info rec size %u\n", urec_size); |
| 10117 | return -EINVAL; |
| 10118 | } |
| 10119 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10120 | prog = env->prog; |
| 10121 | btf = prog->aux->btf; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10122 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10123 | urecord = make_bpfptr(attr->func_info, uattr.is_kernel); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10124 | min_size = min_t(u32, krec_size, urec_size); |
| 10125 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10126 | krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10127 | if (!krecord) |
| 10128 | return -ENOMEM; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10129 | info_aux = kcalloc(nfuncs, sizeof(*info_aux), GFP_KERNEL | __GFP_NOWARN); |
| 10130 | if (!info_aux) |
| 10131 | goto err_free; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10132 | |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10133 | for (i = 0; i < nfuncs; i++) { |
| 10134 | ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size); |
| 10135 | if (ret) { |
| 10136 | if (ret == -E2BIG) { |
| 10137 | verbose(env, "nonzero tailing record in func info"); |
| 10138 | /* set the size kernel expects so loader can zero |
| 10139 | * out the rest of the record. |
| 10140 | */ |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10141 | if (copy_to_bpfptr_offset(uattr, |
| 10142 | offsetof(union bpf_attr, func_info_rec_size), |
| 10143 | &min_size, sizeof(min_size))) |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10144 | ret = -EFAULT; |
| 10145 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10146 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10147 | } |
| 10148 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10149 | if (copy_from_bpfptr(&krecord[i], urecord, min_size)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10150 | ret = -EFAULT; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10151 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10152 | } |
| 10153 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10154 | /* check insn_off */ |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10155 | ret = -EINVAL; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10156 | if (i == 0) { |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10157 | if (krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10158 | verbose(env, |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10159 | "nonzero insn_off %u for the first func info record", |
| 10160 | krecord[i].insn_off); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10161 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10162 | } |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10163 | } else if (krecord[i].insn_off <= prev_offset) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10164 | verbose(env, |
| 10165 | "same or smaller insn offset (%u) than previous func info record (%u)", |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10166 | krecord[i].insn_off, prev_offset); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10167 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10168 | } |
| 10169 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10170 | if (env->subprog_info[i].start != krecord[i].insn_off) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10171 | verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n"); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10172 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10173 | } |
| 10174 | |
| 10175 | /* check type_id */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10176 | type = btf_type_by_id(btf, krecord[i].type_id); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10177 | if (!type || !btf_type_is_func(type)) { |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10178 | verbose(env, "invalid type id %d in func info", |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10179 | krecord[i].type_id); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10180 | goto err_free; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10181 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 10182 | info_aux[i].linkage = BTF_INFO_VLEN(type->info); |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10183 | |
| 10184 | func_proto = btf_type_by_id(btf, type->type); |
| 10185 | if (unlikely(!func_proto || !btf_type_is_func_proto(func_proto))) |
| 10186 | /* btf_func_check() already verified it during BTF load */ |
| 10187 | goto err_free; |
| 10188 | ret_type = btf_type_skip_modifiers(btf, func_proto->type, NULL); |
| 10189 | scalar_return = |
| 10190 | btf_type_is_small_int(ret_type) || btf_type_is_enum(ret_type); |
| 10191 | if (i && !scalar_return && env->subprog_info[i].has_ld_abs) { |
| 10192 | verbose(env, "LD_ABS is only allowed in functions that return 'int'.\n"); |
| 10193 | goto err_free; |
| 10194 | } |
| 10195 | if (i && !scalar_return && env->subprog_info[i].has_tail_call) { |
| 10196 | verbose(env, "tail_call is only allowed in functions that return 'int'.\n"); |
| 10197 | goto err_free; |
| 10198 | } |
| 10199 | |
Martin KaFai Lau | d30d42e | 2018-12-05 17:35:44 -0800 | [diff] [blame] | 10200 | prev_offset = krecord[i].insn_off; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10201 | bpfptr_add(&urecord, urec_size); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10202 | } |
| 10203 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10204 | prog->aux->func_info = krecord; |
| 10205 | prog->aux->func_info_cnt = nfuncs; |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10206 | prog->aux->func_info_aux = info_aux; |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10207 | return 0; |
| 10208 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10209 | err_free: |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10210 | kvfree(krecord); |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10211 | kfree(info_aux); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 10212 | return ret; |
| 10213 | } |
| 10214 | |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10215 | static void adjust_btf_func(struct bpf_verifier_env *env) |
| 10216 | { |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10217 | struct bpf_prog_aux *aux = env->prog->aux; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10218 | int i; |
| 10219 | |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10220 | if (!aux->func_info) |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10221 | return; |
| 10222 | |
| 10223 | for (i = 0; i < env->subprog_cnt; i++) |
Alexei Starovoitov | 8c1b6e6 | 2019-11-14 10:57:16 -0800 | [diff] [blame] | 10224 | aux->func_info[i].insn_off = env->subprog_info[i].start; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 10225 | } |
| 10226 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10227 | #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \ |
| 10228 | sizeof(((struct bpf_line_info *)(0))->line_col)) |
| 10229 | #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE |
| 10230 | |
| 10231 | static int check_btf_line(struct bpf_verifier_env *env, |
| 10232 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10233 | bpfptr_t uattr) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10234 | { |
| 10235 | u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0; |
| 10236 | struct bpf_subprog_info *sub; |
| 10237 | struct bpf_line_info *linfo; |
| 10238 | struct bpf_prog *prog; |
| 10239 | const struct btf *btf; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10240 | bpfptr_t ulinfo; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10241 | int err; |
| 10242 | |
| 10243 | nr_linfo = attr->line_info_cnt; |
| 10244 | if (!nr_linfo) |
| 10245 | return 0; |
Bixuan Cui | 0e6491b | 2021-09-11 08:55:57 +0800 | [diff] [blame] | 10246 | if (nr_linfo > INT_MAX / sizeof(struct bpf_line_info)) |
| 10247 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10248 | |
| 10249 | rec_size = attr->line_info_rec_size; |
| 10250 | if (rec_size < MIN_BPF_LINEINFO_SIZE || |
| 10251 | rec_size > MAX_LINEINFO_REC_SIZE || |
| 10252 | rec_size & (sizeof(u32) - 1)) |
| 10253 | return -EINVAL; |
| 10254 | |
| 10255 | /* Need to zero it in case the userspace may |
| 10256 | * pass in a smaller bpf_line_info object. |
| 10257 | */ |
| 10258 | linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info), |
| 10259 | GFP_KERNEL | __GFP_NOWARN); |
| 10260 | if (!linfo) |
| 10261 | return -ENOMEM; |
| 10262 | |
| 10263 | prog = env->prog; |
| 10264 | btf = prog->aux->btf; |
| 10265 | |
| 10266 | s = 0; |
| 10267 | sub = env->subprog_info; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10268 | ulinfo = make_bpfptr(attr->line_info, uattr.is_kernel); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10269 | expected_size = sizeof(struct bpf_line_info); |
| 10270 | ncopy = min_t(u32, expected_size, rec_size); |
| 10271 | for (i = 0; i < nr_linfo; i++) { |
| 10272 | err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size); |
| 10273 | if (err) { |
| 10274 | if (err == -E2BIG) { |
| 10275 | verbose(env, "nonzero tailing record in line_info"); |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10276 | if (copy_to_bpfptr_offset(uattr, |
| 10277 | offsetof(union bpf_attr, line_info_rec_size), |
| 10278 | &expected_size, sizeof(expected_size))) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10279 | err = -EFAULT; |
| 10280 | } |
| 10281 | goto err_free; |
| 10282 | } |
| 10283 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10284 | if (copy_from_bpfptr(&linfo[i], ulinfo, ncopy)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10285 | err = -EFAULT; |
| 10286 | goto err_free; |
| 10287 | } |
| 10288 | |
| 10289 | /* |
| 10290 | * Check insn_off to ensure |
| 10291 | * 1) strictly increasing AND |
| 10292 | * 2) bounded by prog->len |
| 10293 | * |
| 10294 | * The linfo[0].insn_off == 0 check logically falls into |
| 10295 | * the later "missing bpf_line_info for func..." case |
| 10296 | * because the first linfo[0].insn_off must be the |
| 10297 | * first sub also and the first sub must have |
| 10298 | * subprog_info[0].start == 0. |
| 10299 | */ |
| 10300 | if ((i && linfo[i].insn_off <= prev_offset) || |
| 10301 | linfo[i].insn_off >= prog->len) { |
| 10302 | verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n", |
| 10303 | i, linfo[i].insn_off, prev_offset, |
| 10304 | prog->len); |
| 10305 | err = -EINVAL; |
| 10306 | goto err_free; |
| 10307 | } |
| 10308 | |
Martin KaFai Lau | fdbaa0b | 2018-12-19 13:01:01 -0800 | [diff] [blame] | 10309 | if (!prog->insnsi[linfo[i].insn_off].code) { |
| 10310 | verbose(env, |
| 10311 | "Invalid insn code at line_info[%u].insn_off\n", |
| 10312 | i); |
| 10313 | err = -EINVAL; |
| 10314 | goto err_free; |
| 10315 | } |
| 10316 | |
Martin KaFai Lau | 23127b3 | 2018-12-13 10:41:46 -0800 | [diff] [blame] | 10317 | if (!btf_name_by_offset(btf, linfo[i].line_off) || |
| 10318 | !btf_name_by_offset(btf, linfo[i].file_name_off)) { |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10319 | verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i); |
| 10320 | err = -EINVAL; |
| 10321 | goto err_free; |
| 10322 | } |
| 10323 | |
| 10324 | if (s != env->subprog_cnt) { |
| 10325 | if (linfo[i].insn_off == sub[s].start) { |
| 10326 | sub[s].linfo_idx = i; |
| 10327 | s++; |
| 10328 | } else if (sub[s].start < linfo[i].insn_off) { |
| 10329 | verbose(env, "missing bpf_line_info for func#%u\n", s); |
| 10330 | err = -EINVAL; |
| 10331 | goto err_free; |
| 10332 | } |
| 10333 | } |
| 10334 | |
| 10335 | prev_offset = linfo[i].insn_off; |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10336 | bpfptr_add(&ulinfo, rec_size); |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10337 | } |
| 10338 | |
| 10339 | if (s != env->subprog_cnt) { |
| 10340 | verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n", |
| 10341 | env->subprog_cnt - s, s); |
| 10342 | err = -EINVAL; |
| 10343 | goto err_free; |
| 10344 | } |
| 10345 | |
| 10346 | prog->aux->linfo = linfo; |
| 10347 | prog->aux->nr_linfo = nr_linfo; |
| 10348 | |
| 10349 | return 0; |
| 10350 | |
| 10351 | err_free: |
| 10352 | kvfree(linfo); |
| 10353 | return err; |
| 10354 | } |
| 10355 | |
Alexei Starovoitov | fbd94c7 | 2021-12-01 10:10:28 -0800 | [diff] [blame] | 10356 | #define MIN_CORE_RELO_SIZE sizeof(struct bpf_core_relo) |
| 10357 | #define MAX_CORE_RELO_SIZE MAX_FUNCINFO_REC_SIZE |
| 10358 | |
| 10359 | static int check_core_relo(struct bpf_verifier_env *env, |
| 10360 | const union bpf_attr *attr, |
| 10361 | bpfptr_t uattr) |
| 10362 | { |
| 10363 | u32 i, nr_core_relo, ncopy, expected_size, rec_size; |
| 10364 | struct bpf_core_relo core_relo = {}; |
| 10365 | struct bpf_prog *prog = env->prog; |
| 10366 | const struct btf *btf = prog->aux->btf; |
| 10367 | struct bpf_core_ctx ctx = { |
| 10368 | .log = &env->log, |
| 10369 | .btf = btf, |
| 10370 | }; |
| 10371 | bpfptr_t u_core_relo; |
| 10372 | int err; |
| 10373 | |
| 10374 | nr_core_relo = attr->core_relo_cnt; |
| 10375 | if (!nr_core_relo) |
| 10376 | return 0; |
| 10377 | if (nr_core_relo > INT_MAX / sizeof(struct bpf_core_relo)) |
| 10378 | return -EINVAL; |
| 10379 | |
| 10380 | rec_size = attr->core_relo_rec_size; |
| 10381 | if (rec_size < MIN_CORE_RELO_SIZE || |
| 10382 | rec_size > MAX_CORE_RELO_SIZE || |
| 10383 | rec_size % sizeof(u32)) |
| 10384 | return -EINVAL; |
| 10385 | |
| 10386 | u_core_relo = make_bpfptr(attr->core_relos, uattr.is_kernel); |
| 10387 | expected_size = sizeof(struct bpf_core_relo); |
| 10388 | ncopy = min_t(u32, expected_size, rec_size); |
| 10389 | |
| 10390 | /* Unlike func_info and line_info, copy and apply each CO-RE |
| 10391 | * relocation record one at a time. |
| 10392 | */ |
| 10393 | for (i = 0; i < nr_core_relo; i++) { |
| 10394 | /* future proofing when sizeof(bpf_core_relo) changes */ |
| 10395 | err = bpf_check_uarg_tail_zero(u_core_relo, expected_size, rec_size); |
| 10396 | if (err) { |
| 10397 | if (err == -E2BIG) { |
| 10398 | verbose(env, "nonzero tailing record in core_relo"); |
| 10399 | if (copy_to_bpfptr_offset(uattr, |
| 10400 | offsetof(union bpf_attr, core_relo_rec_size), |
| 10401 | &expected_size, sizeof(expected_size))) |
| 10402 | err = -EFAULT; |
| 10403 | } |
| 10404 | break; |
| 10405 | } |
| 10406 | |
| 10407 | if (copy_from_bpfptr(&core_relo, u_core_relo, ncopy)) { |
| 10408 | err = -EFAULT; |
| 10409 | break; |
| 10410 | } |
| 10411 | |
| 10412 | if (core_relo.insn_off % 8 || core_relo.insn_off / 8 >= prog->len) { |
| 10413 | verbose(env, "Invalid core_relo[%u].insn_off:%u prog->len:%u\n", |
| 10414 | i, core_relo.insn_off, prog->len); |
| 10415 | err = -EINVAL; |
| 10416 | break; |
| 10417 | } |
| 10418 | |
| 10419 | err = bpf_core_apply(&ctx, &core_relo, i, |
| 10420 | &prog->insnsi[core_relo.insn_off / 8]); |
| 10421 | if (err) |
| 10422 | break; |
| 10423 | bpfptr_add(&u_core_relo, rec_size); |
| 10424 | } |
| 10425 | return err; |
| 10426 | } |
| 10427 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10428 | static int check_btf_info(struct bpf_verifier_env *env, |
| 10429 | const union bpf_attr *attr, |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 10430 | bpfptr_t uattr) |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10431 | { |
| 10432 | struct btf *btf; |
| 10433 | int err; |
| 10434 | |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10435 | if (!attr->func_info_cnt && !attr->line_info_cnt) { |
| 10436 | if (check_abnormal_return(env)) |
| 10437 | return -EINVAL; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10438 | return 0; |
Alexei Starovoitov | 09b28d7 | 2020-09-17 19:09:18 -0700 | [diff] [blame] | 10439 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10440 | |
| 10441 | btf = btf_get_by_fd(attr->prog_btf_fd); |
| 10442 | if (IS_ERR(btf)) |
| 10443 | return PTR_ERR(btf); |
Alexei Starovoitov | 350a5c4 | 2021-03-07 14:52:48 -0800 | [diff] [blame] | 10444 | if (btf_is_kernel(btf)) { |
| 10445 | btf_put(btf); |
| 10446 | return -EACCES; |
| 10447 | } |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10448 | env->prog->aux->btf = btf; |
| 10449 | |
| 10450 | err = check_btf_func(env, attr, uattr); |
| 10451 | if (err) |
| 10452 | return err; |
| 10453 | |
| 10454 | err = check_btf_line(env, attr, uattr); |
| 10455 | if (err) |
| 10456 | return err; |
| 10457 | |
Alexei Starovoitov | fbd94c7 | 2021-12-01 10:10:28 -0800 | [diff] [blame] | 10458 | err = check_core_relo(env, attr, uattr); |
| 10459 | if (err) |
| 10460 | return err; |
| 10461 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 10462 | return 0; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 10463 | } |
| 10464 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10465 | /* check %cur's range satisfies %old's */ |
| 10466 | static bool range_within(struct bpf_reg_state *old, |
| 10467 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10468 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 10469 | return old->umin_value <= cur->umin_value && |
| 10470 | old->umax_value >= cur->umax_value && |
| 10471 | old->smin_value <= cur->smin_value && |
Daniel Borkmann | fd67518 | 2021-02-05 20:48:21 +0100 | [diff] [blame] | 10472 | old->smax_value >= cur->smax_value && |
| 10473 | old->u32_min_value <= cur->u32_min_value && |
| 10474 | old->u32_max_value >= cur->u32_max_value && |
| 10475 | old->s32_min_value <= cur->s32_min_value && |
| 10476 | old->s32_max_value >= cur->s32_max_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10477 | } |
| 10478 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10479 | /* If in the old state two registers had the same id, then they need to have |
| 10480 | * the same id in the new state as well. But that id could be different from |
| 10481 | * the old state, so we need to track the mapping from old to new ids. |
| 10482 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 10483 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 10484 | * regs with a different old id could still have new id 9, we don't care about |
| 10485 | * that. |
| 10486 | * So we look through our idmap to see if this old id has been seen before. If |
| 10487 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 10488 | */ |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10489 | static bool check_ids(u32 old_id, u32 cur_id, struct bpf_id_pair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10490 | { |
| 10491 | unsigned int i; |
| 10492 | |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10493 | for (i = 0; i < BPF_ID_MAP_SIZE; i++) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10494 | if (!idmap[i].old) { |
| 10495 | /* Reached an empty slot; haven't seen this id before */ |
| 10496 | idmap[i].old = old_id; |
| 10497 | idmap[i].cur = cur_id; |
| 10498 | return true; |
| 10499 | } |
| 10500 | if (idmap[i].old == old_id) |
| 10501 | return idmap[i].cur == cur_id; |
| 10502 | } |
| 10503 | /* We ran out of idmap slots, which should be impossible */ |
| 10504 | WARN_ON_ONCE(1); |
| 10505 | return false; |
| 10506 | } |
| 10507 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10508 | static void clean_func_state(struct bpf_verifier_env *env, |
| 10509 | struct bpf_func_state *st) |
| 10510 | { |
| 10511 | enum bpf_reg_liveness live; |
| 10512 | int i, j; |
| 10513 | |
| 10514 | for (i = 0; i < BPF_REG_FP; i++) { |
| 10515 | live = st->regs[i].live; |
| 10516 | /* liveness must not touch this register anymore */ |
| 10517 | st->regs[i].live |= REG_LIVE_DONE; |
| 10518 | if (!(live & REG_LIVE_READ)) |
| 10519 | /* since the register is unused, clear its state |
| 10520 | * to make further comparison simpler |
| 10521 | */ |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 10522 | __mark_reg_not_init(env, &st->regs[i]); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10523 | } |
| 10524 | |
| 10525 | for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) { |
| 10526 | live = st->stack[i].spilled_ptr.live; |
| 10527 | /* liveness must not touch this stack slot anymore */ |
| 10528 | st->stack[i].spilled_ptr.live |= REG_LIVE_DONE; |
| 10529 | if (!(live & REG_LIVE_READ)) { |
Daniel Borkmann | f54c789 | 2019-12-22 23:37:40 +0100 | [diff] [blame] | 10530 | __mark_reg_not_init(env, &st->stack[i].spilled_ptr); |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10531 | for (j = 0; j < BPF_REG_SIZE; j++) |
| 10532 | st->stack[i].slot_type[j] = STACK_INVALID; |
| 10533 | } |
| 10534 | } |
| 10535 | } |
| 10536 | |
| 10537 | static void clean_verifier_state(struct bpf_verifier_env *env, |
| 10538 | struct bpf_verifier_state *st) |
| 10539 | { |
| 10540 | int i; |
| 10541 | |
| 10542 | if (st->frame[0]->regs[0].live & REG_LIVE_DONE) |
| 10543 | /* all regs in this state in all frames were already marked */ |
| 10544 | return; |
| 10545 | |
| 10546 | for (i = 0; i <= st->curframe; i++) |
| 10547 | clean_func_state(env, st->frame[i]); |
| 10548 | } |
| 10549 | |
| 10550 | /* the parentage chains form a tree. |
| 10551 | * the verifier states are added to state lists at given insn and |
| 10552 | * pushed into state stack for future exploration. |
| 10553 | * when the verifier reaches bpf_exit insn some of the verifer states |
| 10554 | * stored in the state lists have their final liveness state already, |
| 10555 | * but a lot of states will get revised from liveness point of view when |
| 10556 | * the verifier explores other branches. |
| 10557 | * Example: |
| 10558 | * 1: r0 = 1 |
| 10559 | * 2: if r1 == 100 goto pc+1 |
| 10560 | * 3: r0 = 2 |
| 10561 | * 4: exit |
| 10562 | * when the verifier reaches exit insn the register r0 in the state list of |
| 10563 | * insn 2 will be seen as !REG_LIVE_READ. Then the verifier pops the other_branch |
| 10564 | * of insn 2 and goes exploring further. At the insn 4 it will walk the |
| 10565 | * parentage chain from insn 4 into insn 2 and will mark r0 as REG_LIVE_READ. |
| 10566 | * |
| 10567 | * Since the verifier pushes the branch states as it sees them while exploring |
| 10568 | * the program the condition of walking the branch instruction for the second |
| 10569 | * time means that all states below this branch were already explored and |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 10570 | * their final liveness marks are already propagated. |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10571 | * Hence when the verifier completes the search of state list in is_state_visited() |
| 10572 | * we can call this clean_live_states() function to mark all liveness states |
| 10573 | * as REG_LIVE_DONE to indicate that 'parent' pointers of 'struct bpf_reg_state' |
| 10574 | * will not be used. |
| 10575 | * This function also clears the registers and stack for states that !READ |
| 10576 | * to simplify state merging. |
| 10577 | * |
| 10578 | * Important note here that walking the same branch instruction in the callee |
| 10579 | * doesn't meant that the states are DONE. The verifier has to compare |
| 10580 | * the callsites |
| 10581 | */ |
| 10582 | static void clean_live_states(struct bpf_verifier_env *env, int insn, |
| 10583 | struct bpf_verifier_state *cur) |
| 10584 | { |
| 10585 | struct bpf_verifier_state_list *sl; |
| 10586 | int i; |
| 10587 | |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 10588 | sl = *explored_state(env, insn); |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 10589 | while (sl) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 10590 | if (sl->state.branches) |
| 10591 | goto next; |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 10592 | if (sl->state.insn_idx != insn || |
| 10593 | sl->state.curframe != cur->curframe) |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 10594 | goto next; |
| 10595 | for (i = 0; i <= cur->curframe; i++) |
| 10596 | if (sl->state.frame[i]->callsite != cur->frame[i]->callsite) |
| 10597 | goto next; |
| 10598 | clean_verifier_state(env, &sl->state); |
| 10599 | next: |
| 10600 | sl = sl->next; |
| 10601 | } |
| 10602 | } |
| 10603 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10604 | /* Returns true if (rold safe implies rcur safe) */ |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10605 | static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, |
| 10606 | struct bpf_reg_state *rcur, struct bpf_id_pair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10607 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10608 | bool equal; |
| 10609 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10610 | if (!(rold->live & REG_LIVE_READ)) |
| 10611 | /* explored state didn't use this */ |
| 10612 | return true; |
| 10613 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 10614 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10615 | |
| 10616 | if (rold->type == PTR_TO_STACK) |
| 10617 | /* two stack pointers are equal only if they're pointing to |
| 10618 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 10619 | */ |
| 10620 | return equal && rold->frameno == rcur->frameno; |
| 10621 | |
| 10622 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10623 | return true; |
| 10624 | |
| 10625 | if (rold->type == NOT_INIT) |
| 10626 | /* explored state can't have used this */ |
| 10627 | return true; |
| 10628 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10629 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10630 | switch (rold->type) { |
| 10631 | case SCALAR_VALUE: |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10632 | if (env->explore_alu_limits) |
| 10633 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10634 | if (rcur->type == SCALAR_VALUE) { |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 10635 | if (!rold->precise && !rcur->precise) |
| 10636 | return true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10637 | /* new val must satisfy old val knowledge */ |
| 10638 | return range_within(rold, rcur) && |
| 10639 | tnum_in(rold->var_off, rcur->var_off); |
| 10640 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 10641 | /* We're trying to use a pointer in place of a scalar. |
| 10642 | * Even if the scalar was unbounded, this could lead to |
| 10643 | * pointer leaks because scalars are allowed to leak |
| 10644 | * while pointers are not. We could make this safe in |
| 10645 | * special cases if root is calling us, but it's |
| 10646 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10647 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 10648 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10649 | } |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 10650 | case PTR_TO_MAP_KEY: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10651 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 10652 | /* If the new min/max/var_off satisfy the old ones and |
| 10653 | * everything else matches, we are OK. |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 10654 | * 'id' is not compared, since it's only used for maps with |
| 10655 | * bpf_spin_lock inside map element and in such cases if |
| 10656 | * the rest of the prog is valid for one map element then |
| 10657 | * it's valid for all map elements regardless of the key |
| 10658 | * used in bpf_map_lookup() |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 10659 | */ |
| 10660 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 10661 | range_within(rold, rcur) && |
| 10662 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10663 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 10664 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 10665 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 10666 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 10667 | * checked, doing so could have affected others with the same |
| 10668 | * id, and we can't check for that because we lost the id when |
| 10669 | * we converted to a PTR_TO_MAP_VALUE. |
| 10670 | */ |
| 10671 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 10672 | return false; |
| 10673 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 10674 | return false; |
| 10675 | /* Check our ids match any regs they're supposed to */ |
| 10676 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 10677 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10678 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 10679 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10680 | return false; |
| 10681 | /* We must have at least as much range as the old ptr |
| 10682 | * did, so that any accesses which were safe before are |
| 10683 | * still safe. This is true even if old range < old off, |
| 10684 | * since someone could have accessed through (ptr - k), or |
| 10685 | * even done ptr -= k in a register, to get a safe access. |
| 10686 | */ |
| 10687 | if (rold->range > rcur->range) |
| 10688 | return false; |
| 10689 | /* If the offsets don't match, we can't trust our alignment; |
| 10690 | * nor can we be sure that we won't fall out of range. |
| 10691 | */ |
| 10692 | if (rold->off != rcur->off) |
| 10693 | return false; |
| 10694 | /* id relations must be preserved */ |
| 10695 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 10696 | return false; |
| 10697 | /* new val must satisfy old val knowledge */ |
| 10698 | return range_within(rold, rcur) && |
| 10699 | tnum_in(rold->var_off, rcur->var_off); |
| 10700 | case PTR_TO_CTX: |
| 10701 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10702 | case PTR_TO_PACKET_END: |
Petar Penkov | d58e468 | 2018-09-14 07:46:18 -0700 | [diff] [blame] | 10703 | case PTR_TO_FLOW_KEYS: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 10704 | case PTR_TO_SOCKET: |
| 10705 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 10706 | case PTR_TO_SOCK_COMMON: |
| 10707 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 10708 | case PTR_TO_TCP_SOCK: |
| 10709 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 10710 | case PTR_TO_XDP_SOCK: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10711 | /* Only valid matches are exact, which memcmp() above |
| 10712 | * would have accepted |
| 10713 | */ |
| 10714 | default: |
| 10715 | /* Don't know what's going on, just say it's not safe */ |
| 10716 | return false; |
| 10717 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10718 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10719 | /* Shouldn't get here; if we do, say it's not safe */ |
| 10720 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 10721 | return false; |
| 10722 | } |
| 10723 | |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10724 | static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, |
| 10725 | struct bpf_func_state *cur, struct bpf_id_pair *idmap) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10726 | { |
| 10727 | int i, spi; |
| 10728 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10729 | /* walk slots of the explored stack and ignore any additional |
| 10730 | * slots in the current stack, since explored(safe) state |
| 10731 | * didn't use them |
| 10732 | */ |
| 10733 | for (i = 0; i < old->allocated_stack; i++) { |
| 10734 | spi = i / BPF_REG_SIZE; |
| 10735 | |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 10736 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) { |
| 10737 | i += BPF_REG_SIZE - 1; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10738 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 10739 | continue; |
Alexei Starovoitov | b233920 | 2018-12-13 11:42:31 -0800 | [diff] [blame] | 10740 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10741 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10742 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 10743 | continue; |
Alexei Starovoitov | 19e2dbb | 2018-12-13 11:42:33 -0800 | [diff] [blame] | 10744 | |
| 10745 | /* explored stack has more populated slots than current stack |
| 10746 | * and these slots were used |
| 10747 | */ |
| 10748 | if (i >= cur->allocated_stack) |
| 10749 | return false; |
| 10750 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 10751 | /* if old state was safe with misc data in the stack |
| 10752 | * it will be safe with zero-initialized stack. |
| 10753 | * The opposite is not true |
| 10754 | */ |
| 10755 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 10756 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 10757 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10758 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 10759 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 10760 | /* Ex: old explored (safe) state has STACK_SPILL in |
Randy Dunlap | b8c1a30 | 2020-08-06 20:31:41 -0700 | [diff] [blame] | 10761 | * this stack slot, but current has STACK_MISC -> |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10762 | * this verifier states are not equivalent, |
| 10763 | * return false to continue verification of this path |
| 10764 | */ |
| 10765 | return false; |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10766 | if (i % BPF_REG_SIZE != BPF_REG_SIZE - 1) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10767 | continue; |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10768 | if (!is_spilled_reg(&old->stack[spi])) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10769 | continue; |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10770 | if (!regsafe(env, &old->stack[spi].spilled_ptr, |
| 10771 | &cur->stack[spi].spilled_ptr, idmap)) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 10772 | /* when explored and current stack slot are both storing |
| 10773 | * spilled registers, check that stored pointers types |
| 10774 | * are the same as well. |
| 10775 | * Ex: explored safe path could have stored |
| 10776 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 10777 | * but current path has stored: |
| 10778 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 10779 | * such verifier states are not equivalent. |
| 10780 | * return false to continue verification of this path |
| 10781 | */ |
| 10782 | return false; |
| 10783 | } |
| 10784 | return true; |
| 10785 | } |
| 10786 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 10787 | static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur) |
| 10788 | { |
| 10789 | if (old->acquired_refs != cur->acquired_refs) |
| 10790 | return false; |
| 10791 | return !memcmp(old->refs, cur->refs, |
| 10792 | sizeof(*old->refs) * old->acquired_refs); |
| 10793 | } |
| 10794 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10795 | /* compare two verifier states |
| 10796 | * |
| 10797 | * all states stored in state_list are known to be valid, since |
| 10798 | * verifier reached 'bpf_exit' instruction through them |
| 10799 | * |
| 10800 | * this function is called when verifier exploring different branches of |
| 10801 | * execution popped from the state stack. If it sees an old state that has |
| 10802 | * more strict register state and more strict stack state then this execution |
| 10803 | * branch doesn't need to be explored further, since verifier already |
| 10804 | * concluded that more strict state leads to valid finish. |
| 10805 | * |
| 10806 | * Therefore two states are equivalent if register state is more conservative |
| 10807 | * and explored stack state is more conservative than the current one. |
| 10808 | * Example: |
| 10809 | * explored current |
| 10810 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 10811 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 10812 | * |
| 10813 | * In other words if current stack state (one being explored) has more |
| 10814 | * valid slots than old one that already passed validation, it means |
| 10815 | * the verifier can stop exploring and conclude that current state is valid too |
| 10816 | * |
| 10817 | * Similarly with registers. If explored state has register type as invalid |
| 10818 | * whereas register type in current state is meaningful, it means that |
| 10819 | * the current state will reach 'bpf_exit' instruction safely |
| 10820 | */ |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10821 | static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_state *old, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10822 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10823 | { |
| 10824 | int i; |
| 10825 | |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10826 | memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch)); |
| 10827 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10828 | if (!regsafe(env, &old->regs[i], &cur->regs[i], |
| 10829 | env->idmap_scratch)) |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10830 | return false; |
| 10831 | |
Daniel Borkmann | e042aa5 | 2021-07-16 09:18:21 +0000 | [diff] [blame] | 10832 | if (!stacksafe(env, old, cur, env->idmap_scratch)) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 10833 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 10834 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 10835 | if (!refsafe(old, cur)) |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10836 | return false; |
| 10837 | |
| 10838 | return true; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 10839 | } |
| 10840 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10841 | static bool states_equal(struct bpf_verifier_env *env, |
| 10842 | struct bpf_verifier_state *old, |
| 10843 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10844 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10845 | int i; |
| 10846 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10847 | if (old->curframe != cur->curframe) |
| 10848 | return false; |
| 10849 | |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 10850 | /* Verification state from speculative execution simulation |
| 10851 | * must never prune a non-speculative execution one. |
| 10852 | */ |
| 10853 | if (old->speculative && !cur->speculative) |
| 10854 | return false; |
| 10855 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 10856 | if (old->active_spin_lock != cur->active_spin_lock) |
| 10857 | return false; |
| 10858 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10859 | /* for states to be equal callsites have to be the same |
| 10860 | * and all frame states need to be equivalent |
| 10861 | */ |
| 10862 | for (i = 0; i <= old->curframe; i++) { |
| 10863 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 10864 | return false; |
Lorenz Bauer | c9e73e3 | 2021-04-29 14:46:56 +0100 | [diff] [blame] | 10865 | if (!func_states_equal(env, old->frame[i], cur->frame[i])) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10866 | return false; |
| 10867 | } |
| 10868 | return true; |
| 10869 | } |
| 10870 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10871 | /* Return 0 if no propagation happened. Return negative error code if error |
| 10872 | * happened. Otherwise, return the propagated bit. |
| 10873 | */ |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10874 | static int propagate_liveness_reg(struct bpf_verifier_env *env, |
| 10875 | struct bpf_reg_state *reg, |
| 10876 | struct bpf_reg_state *parent_reg) |
| 10877 | { |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10878 | u8 parent_flag = parent_reg->live & REG_LIVE_READ; |
| 10879 | u8 flag = reg->live & REG_LIVE_READ; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10880 | int err; |
| 10881 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10882 | /* When comes here, read flags of PARENT_REG or REG could be any of |
| 10883 | * REG_LIVE_READ64, REG_LIVE_READ32, REG_LIVE_NONE. There is no need |
| 10884 | * of propagation if PARENT_REG has strongest REG_LIVE_READ64. |
| 10885 | */ |
| 10886 | if (parent_flag == REG_LIVE_READ64 || |
| 10887 | /* Or if there is no read flag from REG. */ |
| 10888 | !flag || |
| 10889 | /* Or if the read flag from REG is the same as PARENT_REG. */ |
| 10890 | parent_flag == flag) |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10891 | return 0; |
| 10892 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10893 | err = mark_reg_read(env, reg, parent_reg, flag); |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10894 | if (err) |
| 10895 | return err; |
| 10896 | |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10897 | return flag; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10898 | } |
| 10899 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10900 | /* A write screens off any subsequent reads; but write marks come from the |
| 10901 | * straight-line code between a state and its parent. When we arrive at an |
| 10902 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 10903 | * code, so read marks in the state must propagate to the parent regardless |
| 10904 | * of the state's write marks. That's what 'parent == state->parent' comparison |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 10905 | * in mark_reg_read() is for. |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10906 | */ |
| 10907 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 10908 | const struct bpf_verifier_state *vstate, |
| 10909 | struct bpf_verifier_state *vparent) |
| 10910 | { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10911 | struct bpf_reg_state *state_reg, *parent_reg; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10912 | struct bpf_func_state *state, *parent; |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10913 | int i, frame, err = 0; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10914 | |
| 10915 | if (vparent->curframe != vstate->curframe) { |
| 10916 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 10917 | vparent->curframe, vstate->curframe); |
| 10918 | return -EFAULT; |
| 10919 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10920 | /* Propagate read liveness of registers... */ |
| 10921 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 10922 | for (frame = 0; frame <= vstate->curframe; frame++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10923 | parent = vparent->frame[frame]; |
| 10924 | state = vstate->frame[frame]; |
| 10925 | parent_reg = parent->regs; |
| 10926 | state_reg = state->regs; |
Jakub Kicinski | 83d1631 | 2019-03-21 14:34:36 -0700 | [diff] [blame] | 10927 | /* We don't need to worry about FP liveness, it's read-only */ |
| 10928 | for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) { |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10929 | err = propagate_liveness_reg(env, &state_reg[i], |
| 10930 | &parent_reg[i]); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10931 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10932 | return err; |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10933 | if (err == REG_LIVE_READ64) |
| 10934 | mark_insn_zext(env, &parent_reg[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10935 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10936 | |
Jiong Wang | 1b04aee | 2019-04-12 22:59:34 +0100 | [diff] [blame] | 10937 | /* Propagate stack slots. */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10938 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 10939 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10940 | parent_reg = &parent->stack[i].spilled_ptr; |
| 10941 | state_reg = &state->stack[i].spilled_ptr; |
Jiong Wang | 55e7f3b | 2019-04-12 22:59:36 +0100 | [diff] [blame] | 10942 | err = propagate_liveness_reg(env, state_reg, |
| 10943 | parent_reg); |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10944 | if (err < 0) |
Jiong Wang | 3f8cafa | 2019-04-12 22:59:35 +0100 | [diff] [blame] | 10945 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 10946 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10947 | } |
Jiong Wang | 5327ed3 | 2019-05-24 23:25:12 +0100 | [diff] [blame] | 10948 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 10949 | } |
| 10950 | |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 10951 | /* find precise scalars in the previous equivalent state and |
| 10952 | * propagate them into the current state |
| 10953 | */ |
| 10954 | static int propagate_precision(struct bpf_verifier_env *env, |
| 10955 | const struct bpf_verifier_state *old) |
| 10956 | { |
| 10957 | struct bpf_reg_state *state_reg; |
| 10958 | struct bpf_func_state *state; |
| 10959 | int i, err = 0; |
| 10960 | |
| 10961 | state = old->frame[old->curframe]; |
| 10962 | state_reg = state->regs; |
| 10963 | for (i = 0; i < BPF_REG_FP; i++, state_reg++) { |
| 10964 | if (state_reg->type != SCALAR_VALUE || |
| 10965 | !state_reg->precise) |
| 10966 | continue; |
| 10967 | if (env->log.level & BPF_LOG_LEVEL2) |
| 10968 | verbose(env, "propagating r%d\n", i); |
| 10969 | err = mark_chain_precision(env, i); |
| 10970 | if (err < 0) |
| 10971 | return err; |
| 10972 | } |
| 10973 | |
| 10974 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Martin KaFai Lau | 27113c5 | 2021-09-21 17:49:34 -0700 | [diff] [blame] | 10975 | if (!is_spilled_reg(&state->stack[i])) |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 10976 | continue; |
| 10977 | state_reg = &state->stack[i].spilled_ptr; |
| 10978 | if (state_reg->type != SCALAR_VALUE || |
| 10979 | !state_reg->precise) |
| 10980 | continue; |
| 10981 | if (env->log.level & BPF_LOG_LEVEL2) |
| 10982 | verbose(env, "propagating fp%d\n", |
| 10983 | (-i - 1) * BPF_REG_SIZE); |
| 10984 | err = mark_chain_precision_stack(env, i); |
| 10985 | if (err < 0) |
| 10986 | return err; |
| 10987 | } |
| 10988 | return 0; |
| 10989 | } |
| 10990 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 10991 | static bool states_maybe_looping(struct bpf_verifier_state *old, |
| 10992 | struct bpf_verifier_state *cur) |
| 10993 | { |
| 10994 | struct bpf_func_state *fold, *fcur; |
| 10995 | int i, fr = cur->curframe; |
| 10996 | |
| 10997 | if (old->curframe != fr) |
| 10998 | return false; |
| 10999 | |
| 11000 | fold = old->frame[fr]; |
| 11001 | fcur = cur->frame[fr]; |
| 11002 | for (i = 0; i < MAX_BPF_REG; i++) |
| 11003 | if (memcmp(&fold->regs[i], &fcur->regs[i], |
| 11004 | offsetof(struct bpf_reg_state, parent))) |
| 11005 | return false; |
| 11006 | return true; |
| 11007 | } |
| 11008 | |
| 11009 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11010 | static int is_state_visited(struct bpf_verifier_env *env, int insn_idx) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11011 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11012 | struct bpf_verifier_state_list *new_sl; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11013 | struct bpf_verifier_state_list *sl, **pprev; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11014 | struct bpf_verifier_state *cur = env->cur_state, *new; |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 11015 | int i, j, err, states_cnt = 0; |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 11016 | bool add_new_state = env->test_state_freq ? true : false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11017 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11018 | cur->last_insn_idx = env->prev_insn_idx; |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11019 | if (!env->insn_aux_data[insn_idx].prune_point) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11020 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 11021 | * be doing state search here |
| 11022 | */ |
| 11023 | return 0; |
| 11024 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11025 | /* bpf progs typically have pruning point every 4 instructions |
| 11026 | * http://vger.kernel.org/bpfconf2019.html#session-1 |
| 11027 | * Do not add new state for future pruning if the verifier hasn't seen |
| 11028 | * at least 2 jumps and at least 8 instructions. |
| 11029 | * This heuristics helps decrease 'total_states' and 'peak_states' metric. |
| 11030 | * In tests that amounts to up to 50% reduction into total verifier |
| 11031 | * memory consumption and 20% verifier time speedup. |
| 11032 | */ |
| 11033 | if (env->jmps_processed - env->prev_jmps_processed >= 2 && |
| 11034 | env->insn_processed - env->prev_insn_processed >= 8) |
| 11035 | add_new_state = true; |
| 11036 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11037 | pprev = explored_state(env, insn_idx); |
| 11038 | sl = *pprev; |
| 11039 | |
Alexei Starovoitov | 9242b5f | 2018-12-13 11:42:34 -0800 | [diff] [blame] | 11040 | clean_live_states(env, insn_idx, cur); |
| 11041 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 11042 | while (sl) { |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11043 | states_cnt++; |
| 11044 | if (sl->state.insn_idx != insn_idx) |
| 11045 | goto next; |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 11046 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11047 | if (sl->state.branches) { |
Alexei Starovoitov | bfc6bb7 | 2021-07-14 17:54:14 -0700 | [diff] [blame] | 11048 | struct bpf_func_state *frame = sl->state.frame[sl->state.curframe]; |
| 11049 | |
| 11050 | if (frame->in_async_callback_fn && |
| 11051 | frame->async_entry_cnt != cur->frame[cur->curframe]->async_entry_cnt) { |
| 11052 | /* Different async_entry_cnt means that the verifier is |
| 11053 | * processing another entry into async callback. |
| 11054 | * Seeing the same state is not an indication of infinite |
| 11055 | * loop or infinite recursion. |
| 11056 | * But finding the same state doesn't mean that it's safe |
| 11057 | * to stop processing the current state. The previous state |
| 11058 | * hasn't yet reached bpf_exit, since state.branches > 0. |
| 11059 | * Checking in_async_callback_fn alone is not enough either. |
| 11060 | * Since the verifier still needs to catch infinite loops |
| 11061 | * inside async callbacks. |
| 11062 | */ |
| 11063 | } else if (states_maybe_looping(&sl->state, cur) && |
| 11064 | states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11065 | verbose_linfo(env, insn_idx, "; "); |
| 11066 | verbose(env, "infinite loop detected at insn %d\n", insn_idx); |
| 11067 | return -EINVAL; |
| 11068 | } |
| 11069 | /* if the verifier is processing a loop, avoid adding new state |
| 11070 | * too often, since different loop iterations have distinct |
| 11071 | * states and may not help future pruning. |
| 11072 | * This threshold shouldn't be too low to make sure that |
| 11073 | * a loop with large bound will be rejected quickly. |
| 11074 | * The most abusive loop will be: |
| 11075 | * r1 += 1 |
| 11076 | * if r1 < 1000000 goto pc-2 |
| 11077 | * 1M insn_procssed limit / 100 == 10k peak states. |
| 11078 | * This threshold shouldn't be too high either, since states |
| 11079 | * at the end of the loop are likely to be useful in pruning. |
| 11080 | */ |
| 11081 | if (env->jmps_processed - env->prev_jmps_processed < 20 && |
| 11082 | env->insn_processed - env->prev_insn_processed < 100) |
| 11083 | add_new_state = false; |
| 11084 | goto miss; |
| 11085 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11086 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11087 | sl->hit_cnt++; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11088 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11089 | * prune the search. |
| 11090 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 11091 | * If we have any write marks in env->cur_state, they |
| 11092 | * will prevent corresponding reads in the continuation |
| 11093 | * from reaching our parent (an explored_state). Our |
| 11094 | * own state will get the read marks recorded, but |
| 11095 | * they'll be immediately forgotten as we're pruning |
| 11096 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11097 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11098 | err = propagate_liveness(env, &sl->state, cur); |
Alexei Starovoitov | a3ce685 | 2019-06-28 09:24:09 -0700 | [diff] [blame] | 11099 | |
| 11100 | /* if previous state reached the exit with precision and |
| 11101 | * current state is equivalent to it (except precsion marks) |
| 11102 | * the precision needs to be propagated back in |
| 11103 | * the current state. |
| 11104 | */ |
| 11105 | err = err ? : push_jmp_history(env, cur); |
| 11106 | err = err ? : propagate_precision(env, &sl->state); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11107 | if (err) |
| 11108 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11109 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11110 | } |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11111 | miss: |
| 11112 | /* when new state is not going to be added do not increase miss count. |
| 11113 | * Otherwise several loop iterations will remove the state |
| 11114 | * recorded earlier. The goal of these heuristics is to have |
| 11115 | * states from some iterations of the loop (some in the beginning |
| 11116 | * and some at the end) to help pruning. |
| 11117 | */ |
| 11118 | if (add_new_state) |
| 11119 | sl->miss_cnt++; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11120 | /* heuristic to determine whether this state is beneficial |
| 11121 | * to keep checking from state equivalence point of view. |
| 11122 | * Higher numbers increase max_states_per_insn and verification time, |
| 11123 | * but do not meaningfully decrease insn_processed. |
| 11124 | */ |
| 11125 | if (sl->miss_cnt > sl->hit_cnt * 3 + 3) { |
| 11126 | /* the state is unlikely to be useful. Remove it to |
| 11127 | * speed up verification |
| 11128 | */ |
| 11129 | *pprev = sl->next; |
| 11130 | if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) { |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11131 | u32 br = sl->state.branches; |
| 11132 | |
| 11133 | WARN_ONCE(br, |
| 11134 | "BUG live_done but branches_to_explore %d\n", |
| 11135 | br); |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11136 | free_verifier_state(&sl->state, false); |
| 11137 | kfree(sl); |
| 11138 | env->peak_states--; |
| 11139 | } else { |
| 11140 | /* cannot free this state, since parentage chain may |
| 11141 | * walk it later. Add it for free_list instead to |
| 11142 | * be freed at the end of verification |
| 11143 | */ |
| 11144 | sl->next = env->free_list; |
| 11145 | env->free_list = sl; |
| 11146 | } |
| 11147 | sl = *pprev; |
| 11148 | continue; |
| 11149 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11150 | next: |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 11151 | pprev = &sl->next; |
| 11152 | sl = *pprev; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11153 | } |
| 11154 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11155 | if (env->max_states_per_insn < states_cnt) |
| 11156 | env->max_states_per_insn = states_cnt; |
| 11157 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 11158 | if (!env->bpf_capable && states_cnt > BPF_COMPLEXITY_LIMIT_STATES) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11159 | return push_jmp_history(env, cur); |
Alexei Starovoitov | ceefbc9 | 2018-12-03 22:46:06 -0800 | [diff] [blame] | 11160 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11161 | if (!add_new_state) |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11162 | return push_jmp_history(env, cur); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11163 | |
| 11164 | /* There were no equivalent states, remember the current one. |
| 11165 | * Technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11166 | * but it will either reach outer most bpf_exit (which means it's safe) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11167 | * or it will be rejected. When there are no loops the verifier won't be |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11168 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11169 | * again on the way to bpf_exit. |
| 11170 | * When looping the sl->state.branches will be > 0 and this state |
| 11171 | * will not be considered for equivalence until branches == 0. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11172 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11173 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11174 | if (!new_sl) |
| 11175 | return -ENOMEM; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11176 | env->total_states++; |
| 11177 | env->peak_states++; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11178 | env->prev_jmps_processed = env->jmps_processed; |
| 11179 | env->prev_insn_processed = env->insn_processed; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11180 | |
| 11181 | /* add new state to the head of linked list */ |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11182 | new = &new_sl->state; |
| 11183 | err = copy_verifier_state(new, cur); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 11184 | if (err) { |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11185 | free_verifier_state(new, false); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 11186 | kfree(new_sl); |
| 11187 | return err; |
| 11188 | } |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 11189 | new->insn_idx = insn_idx; |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11190 | WARN_ONCE(new->branches != 1, |
| 11191 | "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11192 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11193 | cur->parent = new; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11194 | cur->first_insn_idx = insn_idx; |
| 11195 | clear_jmp_history(cur); |
Alexei Starovoitov | 5d83902 | 2019-05-21 20:17:05 -0700 | [diff] [blame] | 11196 | new_sl->next = *explored_state(env, insn_idx); |
| 11197 | *explored_state(env, insn_idx) = new_sl; |
Jakub Kicinski | 7640ead | 2018-12-12 16:29:07 -0800 | [diff] [blame] | 11198 | /* connect new state to parentage chain. Current frame needs all |
| 11199 | * registers connected. Only r6 - r9 of the callers are alive (pushed |
| 11200 | * to the stack implicitly by JITs) so in callers' frames connect just |
| 11201 | * r6 - r9 as an optimization. Callers will have r1 - r5 connected to |
| 11202 | * the state of the call instruction (with WRITTEN set), and r0 comes |
| 11203 | * from callee with its full parentage chain, anyway. |
| 11204 | */ |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 11205 | /* clear write marks in current state: the writes we did are not writes |
| 11206 | * our child did, so they don't screen off its reads from us. |
| 11207 | * (There are no read marks in current state, because reads always mark |
| 11208 | * their parent and current state never has children yet. Only |
| 11209 | * explored_states can get read marks.) |
| 11210 | */ |
Alexei Starovoitov | eea1c22 | 2019-06-15 12:12:21 -0700 | [diff] [blame] | 11211 | for (j = 0; j <= cur->curframe; j++) { |
| 11212 | for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) |
| 11213 | cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i]; |
| 11214 | for (i = 0; i < BPF_REG_FP; i++) |
| 11215 | cur->frame[j]->regs[i].live = REG_LIVE_NONE; |
| 11216 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11217 | |
| 11218 | /* all stack frames are accessible from callee, clear them all */ |
| 11219 | for (j = 0; j <= cur->curframe; j++) { |
| 11220 | struct bpf_func_state *frame = cur->frame[j]; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11221 | struct bpf_func_state *newframe = new->frame[j]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11222 | |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11223 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 11224 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Edward Cree | 679c782 | 2018-08-22 20:02:19 +0100 | [diff] [blame] | 11225 | frame->stack[i].spilled_ptr.parent = |
| 11226 | &newframe->stack[i].spilled_ptr; |
| 11227 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11228 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11229 | return 0; |
| 11230 | } |
| 11231 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11232 | /* Return true if it's OK to have the same insn return a different type. */ |
| 11233 | static bool reg_type_mismatch_ok(enum bpf_reg_type type) |
| 11234 | { |
| 11235 | switch (type) { |
| 11236 | case PTR_TO_CTX: |
| 11237 | case PTR_TO_SOCKET: |
| 11238 | case PTR_TO_SOCKET_OR_NULL: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 11239 | case PTR_TO_SOCK_COMMON: |
| 11240 | case PTR_TO_SOCK_COMMON_OR_NULL: |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 11241 | case PTR_TO_TCP_SOCK: |
| 11242 | case PTR_TO_TCP_SOCK_OR_NULL: |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 11243 | case PTR_TO_XDP_SOCK: |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 11244 | case PTR_TO_BTF_ID: |
Yonghong Song | b121b34 | 2020-05-09 10:59:12 -0700 | [diff] [blame] | 11245 | case PTR_TO_BTF_ID_OR_NULL: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11246 | return false; |
| 11247 | default: |
| 11248 | return true; |
| 11249 | } |
| 11250 | } |
| 11251 | |
| 11252 | /* If an instruction was previously used with particular pointer types, then we |
| 11253 | * need to be careful to avoid cases such as the below, where it may be ok |
| 11254 | * for one branch accessing the pointer, but not ok for the other branch: |
| 11255 | * |
| 11256 | * R1 = sock_ptr |
| 11257 | * goto X; |
| 11258 | * ... |
| 11259 | * R1 = some_other_valid_ptr; |
| 11260 | * goto X; |
| 11261 | * ... |
| 11262 | * R2 = *(u32 *)(R1 + 0); |
| 11263 | */ |
| 11264 | static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev) |
| 11265 | { |
| 11266 | return src != prev && (!reg_type_mismatch_ok(src) || |
| 11267 | !reg_type_mismatch_ok(prev)); |
| 11268 | } |
| 11269 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 11270 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11271 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11272 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 11273 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11274 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11275 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11276 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11277 | bool do_print_state = false; |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11278 | int prev_insn_idx = -1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11279 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11280 | for (;;) { |
| 11281 | struct bpf_insn *insn; |
| 11282 | u8 class; |
| 11283 | int err; |
| 11284 | |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11285 | env->prev_insn_idx = prev_insn_idx; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11286 | if (env->insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11287 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11288 | env->insn_idx, insn_cnt); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11289 | return -EFAULT; |
| 11290 | } |
| 11291 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11292 | insn = &insns[env->insn_idx]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11293 | class = BPF_CLASS(insn->code); |
| 11294 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11295 | if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11296 | verbose(env, |
| 11297 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11298 | env->insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11299 | return -E2BIG; |
| 11300 | } |
| 11301 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11302 | err = is_state_visited(env, env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11303 | if (err < 0) |
| 11304 | return err; |
| 11305 | if (err == 1) { |
| 11306 | /* found equivalent state, can prune the search */ |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11307 | if (env->log.level & BPF_LOG_LEVEL) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11308 | if (do_print_state) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 11309 | verbose(env, "\nfrom %d to %d%s: safe\n", |
| 11310 | env->prev_insn_idx, env->insn_idx, |
| 11311 | env->cur_state->speculative ? |
| 11312 | " (speculative execution)" : ""); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11313 | else |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11314 | verbose(env, "%d: safe\n", env->insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11315 | } |
| 11316 | goto process_bpf_exit; |
| 11317 | } |
| 11318 | |
Alexei Starovoitov | c349480 | 2018-12-03 22:46:04 -0800 | [diff] [blame] | 11319 | if (signal_pending(current)) |
| 11320 | return -EAGAIN; |
| 11321 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 11322 | if (need_resched()) |
| 11323 | cond_resched(); |
| 11324 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 11325 | if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) { |
| 11326 | verbose(env, "\nfrom %d to %d%s:", |
| 11327 | env->prev_insn_idx, env->insn_idx, |
| 11328 | env->cur_state->speculative ? |
| 11329 | " (speculative execution)" : ""); |
| 11330 | print_verifier_state(env, state->frame[state->curframe], true); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11331 | do_print_state = false; |
| 11332 | } |
| 11333 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 11334 | if (env->log.level & BPF_LOG_LEVEL) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11335 | const struct bpf_insn_cbs cbs = { |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11336 | .cb_call = disasm_kfunc_name, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11337 | .cb_print = verbose, |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 11338 | .private_data = env, |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 11339 | }; |
| 11340 | |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 11341 | if (verifier_state_scratched(env)) |
| 11342 | print_insn_state(env, state->frame[state->curframe]); |
| 11343 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11344 | verbose_linfo(env, env->insn_idx, "; "); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 11345 | env->prev_log_len = env->log.len_used; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11346 | verbose(env, "%d: ", env->insn_idx); |
Jiri Olsa | abe0884 | 2018-03-23 11:41:28 +0100 | [diff] [blame] | 11347 | print_bpf_insn(&cbs, insn, env->allow_ptr_leaks); |
Christy Lee | 2e57664 | 2021-12-16 19:42:45 -0800 | [diff] [blame^] | 11348 | env->prev_insn_print_len = env->log.len_used - env->prev_log_len; |
| 11349 | env->prev_log_len = env->log.len_used; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11350 | } |
| 11351 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 11352 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11353 | err = bpf_prog_offload_verify_insn(env, env->insn_idx, |
| 11354 | env->prev_insn_idx); |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 11355 | if (err) |
| 11356 | return err; |
| 11357 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 11358 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11359 | regs = cur_regs(env); |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 11360 | sanitize_mark_insn_seen(env); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11361 | prev_insn_idx = env->insn_idx; |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 11362 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11363 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 11364 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11365 | if (err) |
| 11366 | return err; |
| 11367 | |
| 11368 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11369 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11370 | |
| 11371 | /* check for reserved fields is already done */ |
| 11372 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11373 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11374 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11375 | if (err) |
| 11376 | return err; |
| 11377 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11378 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11379 | if (err) |
| 11380 | return err; |
| 11381 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 11382 | src_reg_type = regs[insn->src_reg].type; |
| 11383 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11384 | /* check that memory (src_reg + off) is readable, |
| 11385 | * the state of dst_reg will be updated by this func |
| 11386 | */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11387 | err = check_mem_access(env, env->insn_idx, insn->src_reg, |
| 11388 | insn->off, BPF_SIZE(insn->code), |
| 11389 | BPF_READ, insn->dst_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11390 | if (err) |
| 11391 | return err; |
| 11392 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11393 | prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11394 | |
| 11395 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11396 | /* saw a valid insn |
| 11397 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11398 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11399 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11400 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11401 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11402 | } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11403 | /* ABuser program is trying to use the same insn |
| 11404 | * dst_reg = *(u32*) (src_reg + off) |
| 11405 | * with different pointer types: |
| 11406 | * src_reg == ctx in one branch and |
| 11407 | * src_reg == stack|map in some other branch. |
| 11408 | * Reject it. |
| 11409 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11410 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11411 | return -EINVAL; |
| 11412 | } |
| 11413 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11414 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11415 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11416 | |
Brendan Jackman | 91c960b | 2021-01-14 18:17:44 +0000 | [diff] [blame] | 11417 | if (BPF_MODE(insn->code) == BPF_ATOMIC) { |
| 11418 | err = check_atomic(env, env->insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11419 | if (err) |
| 11420 | return err; |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11421 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11422 | continue; |
| 11423 | } |
| 11424 | |
Brendan Jackman | 5ca419f | 2021-01-14 18:17:46 +0000 | [diff] [blame] | 11425 | if (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0) { |
| 11426 | verbose(env, "BPF_STX uses reserved fields\n"); |
| 11427 | return -EINVAL; |
| 11428 | } |
| 11429 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11430 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11431 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11432 | if (err) |
| 11433 | return err; |
| 11434 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11435 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11436 | if (err) |
| 11437 | return err; |
| 11438 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11439 | dst_reg_type = regs[insn->dst_reg].type; |
| 11440 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11441 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11442 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 11443 | insn->off, BPF_SIZE(insn->code), |
| 11444 | BPF_WRITE, insn->src_reg, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11445 | if (err) |
| 11446 | return err; |
| 11447 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11448 | prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 11449 | |
| 11450 | if (*prev_dst_type == NOT_INIT) { |
| 11451 | *prev_dst_type = dst_reg_type; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 11452 | } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11453 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11454 | return -EINVAL; |
| 11455 | } |
| 11456 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11457 | } else if (class == BPF_ST) { |
| 11458 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 11459 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11460 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11461 | return -EINVAL; |
| 11462 | } |
| 11463 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 11464 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11465 | if (err) |
| 11466 | return err; |
| 11467 | |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 11468 | if (is_ctx_reg(env, insn->dst_reg)) { |
Joe Stringer | 9d2be44 | 2018-10-02 13:35:31 -0700 | [diff] [blame] | 11469 | verbose(env, "BPF_ST stores into R%d %s is not allowed\n", |
Daniel Borkmann | 2a159c6 | 2018-10-21 02:09:24 +0200 | [diff] [blame] | 11470 | insn->dst_reg, |
| 11471 | reg_type_str[reg_state(env, insn->dst_reg)->type]); |
Daniel Borkmann | f37a8cb | 2018-01-16 23:30:10 +0100 | [diff] [blame] | 11472 | return -EACCES; |
| 11473 | } |
| 11474 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11475 | /* check that memory (dst_reg + off) is writeable */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11476 | err = check_mem_access(env, env->insn_idx, insn->dst_reg, |
| 11477 | insn->off, BPF_SIZE(insn->code), |
| 11478 | BPF_WRITE, -1, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11479 | if (err) |
| 11480 | return err; |
| 11481 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11482 | } else if (class == BPF_JMP || class == BPF_JMP32) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11483 | u8 opcode = BPF_OP(insn->code); |
| 11484 | |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11485 | env->jmps_processed++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11486 | if (opcode == BPF_CALL) { |
| 11487 | if (BPF_SRC(insn->code) != BPF_K || |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 11488 | (insn->src_reg != BPF_PSEUDO_KFUNC_CALL |
| 11489 | && insn->off != 0) || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11490 | (insn->src_reg != BPF_REG_0 && |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11491 | insn->src_reg != BPF_PSEUDO_CALL && |
| 11492 | insn->src_reg != BPF_PSEUDO_KFUNC_CALL) || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11493 | insn->dst_reg != BPF_REG_0 || |
| 11494 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11495 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11496 | return -EINVAL; |
| 11497 | } |
| 11498 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11499 | if (env->cur_state->active_spin_lock && |
| 11500 | (insn->src_reg == BPF_PSEUDO_CALL || |
| 11501 | insn->imm != BPF_FUNC_spin_unlock)) { |
| 11502 | verbose(env, "function calls are not allowed while holding a lock\n"); |
| 11503 | return -EINVAL; |
| 11504 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11505 | if (insn->src_reg == BPF_PSEUDO_CALL) |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11506 | err = check_func_call(env, insn, &env->insn_idx); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 11507 | else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) |
| 11508 | err = check_kfunc_call(env, insn); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11509 | else |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 11510 | err = check_helper_call(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11511 | if (err) |
| 11512 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11513 | } else if (opcode == BPF_JA) { |
| 11514 | if (BPF_SRC(insn->code) != BPF_K || |
| 11515 | insn->imm != 0 || |
| 11516 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11517 | insn->dst_reg != BPF_REG_0 || |
| 11518 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11519 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11520 | return -EINVAL; |
| 11521 | } |
| 11522 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11523 | env->insn_idx += insn->off + 1; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11524 | continue; |
| 11525 | |
| 11526 | } else if (opcode == BPF_EXIT) { |
| 11527 | if (BPF_SRC(insn->code) != BPF_K || |
| 11528 | insn->imm != 0 || |
| 11529 | insn->src_reg != BPF_REG_0 || |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 11530 | insn->dst_reg != BPF_REG_0 || |
| 11531 | class == BPF_JMP32) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11532 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11533 | return -EINVAL; |
| 11534 | } |
| 11535 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11536 | if (env->cur_state->active_spin_lock) { |
| 11537 | verbose(env, "bpf_spin_unlock is missing\n"); |
| 11538 | return -EINVAL; |
| 11539 | } |
| 11540 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11541 | if (state->curframe) { |
| 11542 | /* exit from nested function */ |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11543 | err = prepare_func_exit(env, &env->insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 11544 | if (err) |
| 11545 | return err; |
| 11546 | do_print_state = true; |
| 11547 | continue; |
| 11548 | } |
| 11549 | |
Joe Stringer | fd978bf7 | 2018-10-02 13:35:35 -0700 | [diff] [blame] | 11550 | err = check_reference_leak(env); |
| 11551 | if (err) |
| 11552 | return err; |
| 11553 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 11554 | err = check_return_code(env); |
| 11555 | if (err) |
| 11556 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 11557 | process_bpf_exit: |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 11558 | mark_verifier_state_scratched(env); |
Alexei Starovoitov | 2589726 | 2019-06-15 12:12:20 -0700 | [diff] [blame] | 11559 | update_branch_counts(env, env->cur_state); |
Alexei Starovoitov | b5dc016 | 2019-06-15 12:12:25 -0700 | [diff] [blame] | 11560 | err = pop_stack(env, &prev_insn_idx, |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 11561 | &env->insn_idx, pop_log); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 11562 | if (err < 0) { |
| 11563 | if (err != -ENOENT) |
| 11564 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11565 | break; |
| 11566 | } else { |
| 11567 | do_print_state = true; |
| 11568 | continue; |
| 11569 | } |
| 11570 | } else { |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11571 | err = check_cond_jmp_op(env, insn, &env->insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11572 | if (err) |
| 11573 | return err; |
| 11574 | } |
| 11575 | } else if (class == BPF_LD) { |
| 11576 | u8 mode = BPF_MODE(insn->code); |
| 11577 | |
| 11578 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 11579 | err = check_ld_abs(env, insn); |
| 11580 | if (err) |
| 11581 | return err; |
| 11582 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11583 | } else if (mode == BPF_IMM) { |
| 11584 | err = check_ld_imm(env, insn); |
| 11585 | if (err) |
| 11586 | return err; |
| 11587 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11588 | env->insn_idx++; |
Daniel Borkmann | fe9a5ca | 2021-05-28 13:47:27 +0000 | [diff] [blame] | 11589 | sanitize_mark_insn_seen(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11590 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11591 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11592 | return -EINVAL; |
| 11593 | } |
| 11594 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11595 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11596 | return -EINVAL; |
| 11597 | } |
| 11598 | |
Daniel Borkmann | c08435e | 2019-01-03 00:58:27 +0100 | [diff] [blame] | 11599 | env->insn_idx++; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 11600 | } |
| 11601 | |
| 11602 | return 0; |
| 11603 | } |
| 11604 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11605 | static int find_btf_percpu_datasec(struct btf *btf) |
| 11606 | { |
| 11607 | const struct btf_type *t; |
| 11608 | const char *tname; |
| 11609 | int i, n; |
| 11610 | |
| 11611 | /* |
| 11612 | * Both vmlinux and module each have their own ".data..percpu" |
| 11613 | * DATASECs in BTF. So for module's case, we need to skip vmlinux BTF |
| 11614 | * types to look at only module's own BTF types. |
| 11615 | */ |
| 11616 | n = btf_nr_types(btf); |
| 11617 | if (btf_is_module(btf)) |
| 11618 | i = btf_nr_types(btf_vmlinux); |
| 11619 | else |
| 11620 | i = 1; |
| 11621 | |
| 11622 | for(; i < n; i++) { |
| 11623 | t = btf_type_by_id(btf, i); |
| 11624 | if (BTF_INFO_KIND(t->info) != BTF_KIND_DATASEC) |
| 11625 | continue; |
| 11626 | |
| 11627 | tname = btf_name_by_offset(btf, t->name_off); |
| 11628 | if (!strcmp(tname, ".data..percpu")) |
| 11629 | return i; |
| 11630 | } |
| 11631 | |
| 11632 | return -ENOENT; |
| 11633 | } |
| 11634 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11635 | /* replace pseudo btf_id with kernel symbol address */ |
| 11636 | static int check_pseudo_btf_id(struct bpf_verifier_env *env, |
| 11637 | struct bpf_insn *insn, |
| 11638 | struct bpf_insn_aux_data *aux) |
| 11639 | { |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11640 | const struct btf_var_secinfo *vsi; |
| 11641 | const struct btf_type *datasec; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11642 | struct btf_mod_pair *btf_mod; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11643 | const struct btf_type *t; |
| 11644 | const char *sym_name; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11645 | bool percpu = false; |
Kaixu Xia | f16e631 | 2020-11-11 13:03:46 +0800 | [diff] [blame] | 11646 | u32 type, id = insn->imm; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11647 | struct btf *btf; |
Kaixu Xia | f16e631 | 2020-11-11 13:03:46 +0800 | [diff] [blame] | 11648 | s32 datasec_id; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11649 | u64 addr; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11650 | int i, btf_fd, err; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11651 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11652 | btf_fd = insn[1].imm; |
| 11653 | if (btf_fd) { |
| 11654 | btf = btf_get_by_fd(btf_fd); |
| 11655 | if (IS_ERR(btf)) { |
| 11656 | verbose(env, "invalid module BTF object FD specified.\n"); |
| 11657 | return -EINVAL; |
| 11658 | } |
| 11659 | } else { |
| 11660 | if (!btf_vmlinux) { |
| 11661 | verbose(env, "kernel is missing BTF, make sure CONFIG_DEBUG_INFO_BTF=y is specified in Kconfig.\n"); |
| 11662 | return -EINVAL; |
| 11663 | } |
| 11664 | btf = btf_vmlinux; |
| 11665 | btf_get(btf); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11666 | } |
| 11667 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11668 | t = btf_type_by_id(btf, id); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11669 | if (!t) { |
| 11670 | verbose(env, "ldimm64 insn specifies invalid btf_id %d.\n", id); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11671 | err = -ENOENT; |
| 11672 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11673 | } |
| 11674 | |
| 11675 | if (!btf_type_is_var(t)) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11676 | verbose(env, "pseudo btf_id %d in ldimm64 isn't KIND_VAR.\n", id); |
| 11677 | err = -EINVAL; |
| 11678 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11679 | } |
| 11680 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11681 | sym_name = btf_name_by_offset(btf, t->name_off); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11682 | addr = kallsyms_lookup_name(sym_name); |
| 11683 | if (!addr) { |
| 11684 | verbose(env, "ldimm64 failed to find the address for kernel symbol '%s'.\n", |
| 11685 | sym_name); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11686 | err = -ENOENT; |
| 11687 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11688 | } |
| 11689 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11690 | datasec_id = find_btf_percpu_datasec(btf); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11691 | if (datasec_id > 0) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11692 | datasec = btf_type_by_id(btf, datasec_id); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11693 | for_each_vsi(i, datasec, vsi) { |
| 11694 | if (vsi->type == id) { |
| 11695 | percpu = true; |
| 11696 | break; |
| 11697 | } |
| 11698 | } |
| 11699 | } |
| 11700 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11701 | insn[0].imm = (u32)addr; |
| 11702 | insn[1].imm = addr >> 32; |
| 11703 | |
| 11704 | type = t->type; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11705 | t = btf_type_skip_modifiers(btf, type, NULL); |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11706 | if (percpu) { |
| 11707 | aux->btf_var.reg_type = PTR_TO_PERCPU_BTF_ID; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11708 | aux->btf_var.btf = btf; |
Hao Luo | eaa6bcb | 2020-09-29 16:50:47 -0700 | [diff] [blame] | 11709 | aux->btf_var.btf_id = type; |
| 11710 | } else if (!btf_type_is_struct(t)) { |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11711 | const struct btf_type *ret; |
| 11712 | const char *tname; |
| 11713 | u32 tsize; |
| 11714 | |
| 11715 | /* resolve the type size of ksym. */ |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11716 | ret = btf_resolve_size(btf, t, &tsize); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11717 | if (IS_ERR(ret)) { |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11718 | tname = btf_name_by_offset(btf, t->name_off); |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11719 | verbose(env, "ldimm64 unable to resolve the size of type '%s': %ld\n", |
| 11720 | tname, PTR_ERR(ret)); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11721 | err = -EINVAL; |
| 11722 | goto err_put; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11723 | } |
| 11724 | aux->btf_var.reg_type = PTR_TO_MEM; |
| 11725 | aux->btf_var.mem_size = tsize; |
| 11726 | } else { |
| 11727 | aux->btf_var.reg_type = PTR_TO_BTF_ID; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11728 | aux->btf_var.btf = btf; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11729 | aux->btf_var.btf_id = type; |
| 11730 | } |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11731 | |
| 11732 | /* check whether we recorded this BTF (and maybe module) already */ |
| 11733 | for (i = 0; i < env->used_btf_cnt; i++) { |
| 11734 | if (env->used_btfs[i].btf == btf) { |
| 11735 | btf_put(btf); |
| 11736 | return 0; |
| 11737 | } |
| 11738 | } |
| 11739 | |
| 11740 | if (env->used_btf_cnt >= MAX_USED_BTFS) { |
| 11741 | err = -E2BIG; |
| 11742 | goto err_put; |
| 11743 | } |
| 11744 | |
| 11745 | btf_mod = &env->used_btfs[env->used_btf_cnt]; |
| 11746 | btf_mod->btf = btf; |
| 11747 | btf_mod->module = NULL; |
| 11748 | |
| 11749 | /* if we reference variables from kernel module, bump its refcount */ |
| 11750 | if (btf_is_module(btf)) { |
| 11751 | btf_mod->module = btf_try_get_module(btf); |
| 11752 | if (!btf_mod->module) { |
| 11753 | err = -ENXIO; |
| 11754 | goto err_put; |
| 11755 | } |
| 11756 | } |
| 11757 | |
| 11758 | env->used_btf_cnt++; |
| 11759 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11760 | return 0; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 11761 | err_put: |
| 11762 | btf_put(btf); |
| 11763 | return err; |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11764 | } |
| 11765 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11766 | static int check_map_prealloc(struct bpf_map *map) |
| 11767 | { |
| 11768 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 11769 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 11770 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11771 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 11772 | } |
| 11773 | |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11774 | static bool is_tracing_prog_type(enum bpf_prog_type type) |
| 11775 | { |
| 11776 | switch (type) { |
| 11777 | case BPF_PROG_TYPE_KPROBE: |
| 11778 | case BPF_PROG_TYPE_TRACEPOINT: |
| 11779 | case BPF_PROG_TYPE_PERF_EVENT: |
| 11780 | case BPF_PROG_TYPE_RAW_TRACEPOINT: |
| 11781 | return true; |
| 11782 | default: |
| 11783 | return false; |
| 11784 | } |
| 11785 | } |
| 11786 | |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11787 | static bool is_preallocated_map(struct bpf_map *map) |
| 11788 | { |
| 11789 | if (!check_map_prealloc(map)) |
| 11790 | return false; |
| 11791 | if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) |
| 11792 | return false; |
| 11793 | return true; |
| 11794 | } |
| 11795 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11796 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 11797 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11798 | struct bpf_prog *prog) |
| 11799 | |
| 11800 | { |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 11801 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11802 | /* |
| 11803 | * Validate that trace type programs use preallocated hash maps. |
| 11804 | * |
| 11805 | * For programs attached to PERF events this is mandatory as the |
| 11806 | * perf NMI can hit any arbitrary code sequence. |
| 11807 | * |
| 11808 | * All other trace types using preallocated hash maps are unsafe as |
| 11809 | * well because tracepoint or kprobes can be inside locked regions |
| 11810 | * of the memory allocator or at a place where a recursion into the |
| 11811 | * memory allocator would see inconsistent state. |
| 11812 | * |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 11813 | * On RT enabled kernels run-time allocation of all trace type |
| 11814 | * programs is strictly prohibited due to lock type constraints. On |
| 11815 | * !RT kernels it is allowed for backwards compatibility reasons for |
| 11816 | * now, but warnings are emitted so developers are made aware of |
| 11817 | * the unsafety and can fix their programs before this is enforced. |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11818 | */ |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 11819 | if (is_tracing_prog_type(prog_type) && !is_preallocated_map(map)) { |
| 11820 | if (prog_type == BPF_PROG_TYPE_PERF_EVENT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11821 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 11822 | return -EINVAL; |
| 11823 | } |
Thomas Gleixner | 2ed905c | 2020-02-24 15:01:33 +0100 | [diff] [blame] | 11824 | if (IS_ENABLED(CONFIG_PREEMPT_RT)) { |
| 11825 | verbose(env, "trace type programs can only use preallocated hash map\n"); |
| 11826 | return -EINVAL; |
| 11827 | } |
Thomas Gleixner | 94dacdb | 2020-02-24 15:01:32 +0100 | [diff] [blame] | 11828 | WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); |
| 11829 | verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11830 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11831 | |
KP Singh | 9e7a4d9 | 2020-11-06 10:37:39 +0000 | [diff] [blame] | 11832 | if (map_value_has_spin_lock(map)) { |
| 11833 | if (prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { |
| 11834 | verbose(env, "socket filter progs cannot use bpf_spin_lock yet\n"); |
| 11835 | return -EINVAL; |
| 11836 | } |
| 11837 | |
| 11838 | if (is_tracing_prog_type(prog_type)) { |
| 11839 | verbose(env, "tracing progs cannot use bpf_spin_lock yet\n"); |
| 11840 | return -EINVAL; |
| 11841 | } |
| 11842 | |
| 11843 | if (prog->aux->sleepable) { |
| 11844 | verbose(env, "sleepable progs cannot use bpf_spin_lock yet\n"); |
| 11845 | return -EINVAL; |
| 11846 | } |
Alexei Starovoitov | d83525c | 2019-01-31 15:40:04 -0800 | [diff] [blame] | 11847 | } |
| 11848 | |
Dmitrii Banshchikov | 5e0bc30 | 2021-11-13 18:22:26 +0400 | [diff] [blame] | 11849 | if (map_value_has_timer(map)) { |
| 11850 | if (is_tracing_prog_type(prog_type)) { |
| 11851 | verbose(env, "tracing progs cannot use bpf_timer yet\n"); |
| 11852 | return -EINVAL; |
| 11853 | } |
| 11854 | } |
| 11855 | |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11856 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
Jakub Kicinski | 0972826 | 2018-07-17 10:53:23 -0700 | [diff] [blame] | 11857 | !bpf_offload_prog_map_match(prog, map)) { |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 11858 | verbose(env, "offload device mismatch between prog and map\n"); |
| 11859 | return -EINVAL; |
| 11860 | } |
| 11861 | |
Martin KaFai Lau | 85d33df | 2020-01-08 16:35:05 -0800 | [diff] [blame] | 11862 | if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) { |
| 11863 | verbose(env, "bpf_struct_ops map cannot be used in prog\n"); |
| 11864 | return -EINVAL; |
| 11865 | } |
| 11866 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11867 | if (prog->aux->sleepable) |
| 11868 | switch (map->map_type) { |
| 11869 | case BPF_MAP_TYPE_HASH: |
| 11870 | case BPF_MAP_TYPE_LRU_HASH: |
| 11871 | case BPF_MAP_TYPE_ARRAY: |
Alexei Starovoitov | 638e4b8 | 2021-02-09 19:36:33 -0800 | [diff] [blame] | 11872 | case BPF_MAP_TYPE_PERCPU_HASH: |
| 11873 | case BPF_MAP_TYPE_PERCPU_ARRAY: |
| 11874 | case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
| 11875 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
| 11876 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11877 | if (!is_preallocated_map(map)) { |
| 11878 | verbose(env, |
Alexei Starovoitov | 638e4b8 | 2021-02-09 19:36:33 -0800 | [diff] [blame] | 11879 | "Sleepable programs can only use preallocated maps\n"); |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11880 | return -EINVAL; |
| 11881 | } |
| 11882 | break; |
KP Singh | ba90c2c | 2021-02-04 19:36:21 +0000 | [diff] [blame] | 11883 | case BPF_MAP_TYPE_RINGBUF: |
| 11884 | break; |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11885 | default: |
| 11886 | verbose(env, |
KP Singh | ba90c2c | 2021-02-04 19:36:21 +0000 | [diff] [blame] | 11887 | "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 11888 | return -EINVAL; |
| 11889 | } |
| 11890 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11891 | return 0; |
| 11892 | } |
| 11893 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 11894 | static bool bpf_map_is_cgroup_storage(struct bpf_map *map) |
| 11895 | { |
| 11896 | return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE || |
| 11897 | map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); |
| 11898 | } |
| 11899 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11900 | /* find and rewrite pseudo imm in ld_imm64 instructions: |
| 11901 | * |
| 11902 | * 1. if it accesses map FD, replace it with actual map pointer. |
| 11903 | * 2. if it accesses btf_id of a VAR, replace it with pointer to the var. |
| 11904 | * |
| 11905 | * NOTE: btf_vmlinux is required for converting pseudo btf_id. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11906 | */ |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11907 | static int resolve_pseudo_ldimm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11908 | { |
| 11909 | struct bpf_insn *insn = env->prog->insnsi; |
| 11910 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11911 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11912 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 11913 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 11914 | if (err) |
| 11915 | return err; |
| 11916 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11917 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11918 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 11919 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11920 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 11921 | return -EINVAL; |
| 11922 | } |
| 11923 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11924 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11925 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11926 | struct bpf_map *map; |
| 11927 | struct fd f; |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11928 | u64 addr; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11929 | u32 fd; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11930 | |
| 11931 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 11932 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 11933 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11934 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11935 | return -EINVAL; |
| 11936 | } |
| 11937 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11938 | if (insn[0].src_reg == 0) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11939 | /* valid generic load 64-bit imm */ |
| 11940 | goto next_insn; |
| 11941 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 11942 | if (insn[0].src_reg == BPF_PSEUDO_BTF_ID) { |
| 11943 | aux = &env->insn_aux_data[i]; |
| 11944 | err = check_pseudo_btf_id(env, insn, aux); |
| 11945 | if (err) |
| 11946 | return err; |
| 11947 | goto next_insn; |
| 11948 | } |
| 11949 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 11950 | if (insn[0].src_reg == BPF_PSEUDO_FUNC) { |
| 11951 | aux = &env->insn_aux_data[i]; |
| 11952 | aux->ptr_type = PTR_TO_FUNC; |
| 11953 | goto next_insn; |
| 11954 | } |
| 11955 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 11956 | /* In final convert_pseudo_ld_imm64() step, this is |
| 11957 | * converted into regular 64-bit imm load insn. |
| 11958 | */ |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11959 | switch (insn[0].src_reg) { |
| 11960 | case BPF_PSEUDO_MAP_VALUE: |
| 11961 | case BPF_PSEUDO_MAP_IDX_VALUE: |
| 11962 | break; |
| 11963 | case BPF_PSEUDO_MAP_FD: |
| 11964 | case BPF_PSEUDO_MAP_IDX: |
| 11965 | if (insn[1].imm == 0) |
| 11966 | break; |
| 11967 | fallthrough; |
| 11968 | default: |
| 11969 | verbose(env, "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11970 | return -EINVAL; |
| 11971 | } |
| 11972 | |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 11973 | switch (insn[0].src_reg) { |
| 11974 | case BPF_PSEUDO_MAP_IDX_VALUE: |
| 11975 | case BPF_PSEUDO_MAP_IDX: |
| 11976 | if (bpfptr_is_null(env->fd_array)) { |
| 11977 | verbose(env, "fd_idx without fd_array is invalid\n"); |
| 11978 | return -EPROTO; |
| 11979 | } |
| 11980 | if (copy_from_bpfptr_offset(&fd, env->fd_array, |
| 11981 | insn[0].imm * sizeof(fd), |
| 11982 | sizeof(fd))) |
| 11983 | return -EFAULT; |
| 11984 | break; |
| 11985 | default: |
| 11986 | fd = insn[0].imm; |
| 11987 | break; |
| 11988 | } |
| 11989 | |
| 11990 | f = fdget(fd); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 11991 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11992 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11993 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Daniel Borkmann | 2018239 | 2019-03-04 21:08:53 +0100 | [diff] [blame] | 11994 | insn[0].imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 11995 | return PTR_ERR(map); |
| 11996 | } |
| 11997 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 11998 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 11999 | if (err) { |
| 12000 | fdput(f); |
| 12001 | return err; |
| 12002 | } |
| 12003 | |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12004 | aux = &env->insn_aux_data[i]; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 12005 | if (insn[0].src_reg == BPF_PSEUDO_MAP_FD || |
| 12006 | insn[0].src_reg == BPF_PSEUDO_MAP_IDX) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12007 | addr = (unsigned long)map; |
| 12008 | } else { |
| 12009 | u32 off = insn[1].imm; |
| 12010 | |
| 12011 | if (off >= BPF_MAX_VAR_OFF) { |
| 12012 | verbose(env, "direct value offset of %u is not allowed\n", off); |
| 12013 | fdput(f); |
| 12014 | return -EINVAL; |
| 12015 | } |
| 12016 | |
| 12017 | if (!map->ops->map_direct_value_addr) { |
| 12018 | verbose(env, "no direct value access support for this map type\n"); |
| 12019 | fdput(f); |
| 12020 | return -EINVAL; |
| 12021 | } |
| 12022 | |
| 12023 | err = map->ops->map_direct_value_addr(map, &addr, off); |
| 12024 | if (err) { |
| 12025 | verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n", |
| 12026 | map->value_size, off); |
| 12027 | fdput(f); |
| 12028 | return err; |
| 12029 | } |
| 12030 | |
| 12031 | aux->map_off = off; |
| 12032 | addr += off; |
| 12033 | } |
| 12034 | |
| 12035 | insn[0].imm = (u32)addr; |
| 12036 | insn[1].imm = addr >> 32; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12037 | |
| 12038 | /* check whether we recorded this map already */ |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12039 | for (j = 0; j < env->used_map_cnt; j++) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12040 | if (env->used_maps[j] == map) { |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12041 | aux->map_index = j; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12042 | fdput(f); |
| 12043 | goto next_insn; |
| 12044 | } |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12045 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12046 | |
| 12047 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 12048 | fdput(f); |
| 12049 | return -E2BIG; |
| 12050 | } |
| 12051 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12052 | /* hold the map. If the program is rejected by verifier, |
| 12053 | * the map will be released by release_maps() or it |
| 12054 | * will be used by the valid program until it's unloaded |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 12055 | * and all maps are released in free_used_maps() |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12056 | */ |
Andrii Nakryiko | 1e0bd5a | 2019-11-17 09:28:02 -0800 | [diff] [blame] | 12057 | bpf_map_inc(map); |
Daniel Borkmann | d8eca5b | 2019-04-09 23:20:03 +0200 | [diff] [blame] | 12058 | |
| 12059 | aux->map_index = env->used_map_cnt; |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 12060 | env->used_maps[env->used_map_cnt++] = map; |
| 12061 | |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 12062 | if (bpf_map_is_cgroup_storage(map) && |
Daniel Borkmann | e473042 | 2019-12-17 13:28:16 +0100 | [diff] [blame] | 12063 | bpf_cgroup_storage_assign(env->prog->aux, map)) { |
Roman Gushchin | b741f16 | 2018-09-28 14:45:43 +0000 | [diff] [blame] | 12064 | verbose(env, "only one cgroup storage of each type is allowed\n"); |
Roman Gushchin | de9cbba | 2018-08-02 14:27:18 -0700 | [diff] [blame] | 12065 | fdput(f); |
| 12066 | return -EBUSY; |
| 12067 | } |
| 12068 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12069 | fdput(f); |
| 12070 | next_insn: |
| 12071 | insn++; |
| 12072 | i++; |
Daniel Borkmann | 5e581da | 2018-01-26 23:33:38 +0100 | [diff] [blame] | 12073 | continue; |
| 12074 | } |
| 12075 | |
| 12076 | /* Basic sanity check before we invest more work here. */ |
| 12077 | if (!bpf_opcode_in_insntable(insn->code)) { |
| 12078 | verbose(env, "unknown opcode %02x\n", insn->code); |
| 12079 | return -EINVAL; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12080 | } |
| 12081 | } |
| 12082 | |
| 12083 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 12084 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 12085 | * These pointers will be used later by verifier to validate map access. |
| 12086 | */ |
| 12087 | return 0; |
| 12088 | } |
| 12089 | |
| 12090 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12091 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12092 | { |
Daniel Borkmann | a2ea074 | 2019-12-16 17:49:00 +0100 | [diff] [blame] | 12093 | __bpf_free_used_maps(env->prog->aux, env->used_maps, |
| 12094 | env->used_map_cnt); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12095 | } |
| 12096 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 12097 | /* drop refcnt of maps used by the rejected program */ |
| 12098 | static void release_btfs(struct bpf_verifier_env *env) |
| 12099 | { |
| 12100 | __bpf_free_used_btfs(env->prog->aux, env->used_btfs, |
| 12101 | env->used_btf_cnt); |
| 12102 | } |
| 12103 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12104 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12105 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12106 | { |
| 12107 | struct bpf_insn *insn = env->prog->insnsi; |
| 12108 | int insn_cnt = env->prog->len; |
| 12109 | int i; |
| 12110 | |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12111 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 12112 | if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) |
| 12113 | continue; |
| 12114 | if (insn->src_reg == BPF_PSEUDO_FUNC) |
| 12115 | continue; |
| 12116 | insn->src_reg = 0; |
| 12117 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 12118 | } |
| 12119 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12120 | /* single env->prog->insni[off] instruction was replaced with the range |
| 12121 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 12122 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 12123 | */ |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12124 | static void adjust_insn_aux_data(struct bpf_verifier_env *env, |
| 12125 | struct bpf_insn_aux_data *new_data, |
| 12126 | struct bpf_prog *new_prog, u32 off, u32 cnt) |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12127 | { |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12128 | struct bpf_insn_aux_data *old_data = env->insn_aux_data; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12129 | struct bpf_insn *insn = new_prog->insnsi; |
Daniel Borkmann | d203b0f | 2021-05-28 13:03:30 +0000 | [diff] [blame] | 12130 | u32 old_seen = old_data[off].seen; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12131 | u32 prog_len; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12132 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12133 | |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12134 | /* aux info at OFF always needs adjustment, no matter fast path |
| 12135 | * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the |
| 12136 | * original insn at old prog. |
| 12137 | */ |
| 12138 | old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1); |
| 12139 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12140 | if (cnt == 1) |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12141 | return; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12142 | prog_len = new_prog->len; |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12143 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12144 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 12145 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 12146 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12147 | for (i = off; i < off + cnt - 1; i++) { |
Daniel Borkmann | d203b0f | 2021-05-28 13:03:30 +0000 | [diff] [blame] | 12148 | /* Expand insni[off]'s seen count to the patched range. */ |
| 12149 | new_data[i].seen = old_seen; |
Jiong Wang | b325fbc | 2019-05-24 23:25:13 +0100 | [diff] [blame] | 12150 | new_data[i].zext_dst = insn_has_def32(env, insn + i); |
| 12151 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12152 | env->insn_aux_data = new_data; |
| 12153 | vfree(old_data); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12154 | } |
| 12155 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12156 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 12157 | { |
| 12158 | int i; |
| 12159 | |
| 12160 | if (len == 1) |
| 12161 | return; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 12162 | /* NOTE: fake 'exit' subprog should be updated as well. */ |
| 12163 | for (i = 0; i <= env->subprog_cnt; i++) { |
Edward Cree | afd5942 | 2018-11-16 12:00:07 +0000 | [diff] [blame] | 12164 | if (env->subprog_info[i].start <= off) |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12165 | continue; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 12166 | env->subprog_info[i].start += len - 1; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12167 | } |
| 12168 | } |
| 12169 | |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12170 | static void adjust_poke_descs(struct bpf_prog *prog, u32 off, u32 len) |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12171 | { |
| 12172 | struct bpf_jit_poke_descriptor *tab = prog->aux->poke_tab; |
| 12173 | int i, sz = prog->aux->size_poke_tab; |
| 12174 | struct bpf_jit_poke_descriptor *desc; |
| 12175 | |
| 12176 | for (i = 0; i < sz; i++) { |
| 12177 | desc = &tab[i]; |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12178 | if (desc->insn_idx <= off) |
| 12179 | continue; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12180 | desc->insn_idx += len - 1; |
| 12181 | } |
| 12182 | } |
| 12183 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12184 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 12185 | const struct bpf_insn *patch, u32 len) |
| 12186 | { |
| 12187 | struct bpf_prog *new_prog; |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12188 | struct bpf_insn_aux_data *new_data = NULL; |
| 12189 | |
| 12190 | if (len > 1) { |
| 12191 | new_data = vzalloc(array_size(env->prog->len + len - 1, |
| 12192 | sizeof(struct bpf_insn_aux_data))); |
| 12193 | if (!new_data) |
| 12194 | return NULL; |
| 12195 | } |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12196 | |
| 12197 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 12198 | if (IS_ERR(new_prog)) { |
| 12199 | if (PTR_ERR(new_prog) == -ERANGE) |
| 12200 | verbose(env, |
| 12201 | "insn %d cannot be patched due to 16-bit range\n", |
| 12202 | env->insn_aux_data[off].orig_idx); |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12203 | vfree(new_data); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12204 | return NULL; |
Alexei Starovoitov | 4f73379 | 2019-04-01 21:27:44 -0700 | [diff] [blame] | 12205 | } |
He Fengqing | 75f0fc7 | 2021-07-14 10:18:15 +0000 | [diff] [blame] | 12206 | adjust_insn_aux_data(env, new_data, new_prog, off, len); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 12207 | adjust_subprog_starts(env, off, len); |
John Fastabend | 7506d21 | 2021-06-16 15:55:00 -0700 | [diff] [blame] | 12208 | adjust_poke_descs(new_prog, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12209 | return new_prog; |
| 12210 | } |
| 12211 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12212 | static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env, |
| 12213 | u32 off, u32 cnt) |
| 12214 | { |
| 12215 | int i, j; |
| 12216 | |
| 12217 | /* find first prog starting at or after off (first to remove) */ |
| 12218 | for (i = 0; i < env->subprog_cnt; i++) |
| 12219 | if (env->subprog_info[i].start >= off) |
| 12220 | break; |
| 12221 | /* find first prog starting at or after off + cnt (first to stay) */ |
| 12222 | for (j = i; j < env->subprog_cnt; j++) |
| 12223 | if (env->subprog_info[j].start >= off + cnt) |
| 12224 | break; |
| 12225 | /* if j doesn't start exactly at off + cnt, we are just removing |
| 12226 | * the front of previous prog |
| 12227 | */ |
| 12228 | if (env->subprog_info[j].start != off + cnt) |
| 12229 | j--; |
| 12230 | |
| 12231 | if (j > i) { |
| 12232 | struct bpf_prog_aux *aux = env->prog->aux; |
| 12233 | int move; |
| 12234 | |
| 12235 | /* move fake 'exit' subprog as well */ |
| 12236 | move = env->subprog_cnt + 1 - j; |
| 12237 | |
| 12238 | memmove(env->subprog_info + i, |
| 12239 | env->subprog_info + j, |
| 12240 | sizeof(*env->subprog_info) * move); |
| 12241 | env->subprog_cnt -= j - i; |
| 12242 | |
| 12243 | /* remove func_info */ |
| 12244 | if (aux->func_info) { |
| 12245 | move = aux->func_info_cnt - j; |
| 12246 | |
| 12247 | memmove(aux->func_info + i, |
| 12248 | aux->func_info + j, |
| 12249 | sizeof(*aux->func_info) * move); |
| 12250 | aux->func_info_cnt -= j - i; |
| 12251 | /* func_info->insn_off is set after all code rewrites, |
| 12252 | * in adjust_btf_func() - no need to adjust |
| 12253 | */ |
| 12254 | } |
| 12255 | } else { |
| 12256 | /* convert i from "first prog to remove" to "first to adjust" */ |
| 12257 | if (env->subprog_info[i].start == off) |
| 12258 | i++; |
| 12259 | } |
| 12260 | |
| 12261 | /* update fake 'exit' subprog as well */ |
| 12262 | for (; i <= env->subprog_cnt; i++) |
| 12263 | env->subprog_info[i].start -= cnt; |
| 12264 | |
| 12265 | return 0; |
| 12266 | } |
| 12267 | |
| 12268 | static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off, |
| 12269 | u32 cnt) |
| 12270 | { |
| 12271 | struct bpf_prog *prog = env->prog; |
| 12272 | u32 i, l_off, l_cnt, nr_linfo; |
| 12273 | struct bpf_line_info *linfo; |
| 12274 | |
| 12275 | nr_linfo = prog->aux->nr_linfo; |
| 12276 | if (!nr_linfo) |
| 12277 | return 0; |
| 12278 | |
| 12279 | linfo = prog->aux->linfo; |
| 12280 | |
| 12281 | /* find first line info to remove, count lines to be removed */ |
| 12282 | for (i = 0; i < nr_linfo; i++) |
| 12283 | if (linfo[i].insn_off >= off) |
| 12284 | break; |
| 12285 | |
| 12286 | l_off = i; |
| 12287 | l_cnt = 0; |
| 12288 | for (; i < nr_linfo; i++) |
| 12289 | if (linfo[i].insn_off < off + cnt) |
| 12290 | l_cnt++; |
| 12291 | else |
| 12292 | break; |
| 12293 | |
| 12294 | /* First live insn doesn't match first live linfo, it needs to "inherit" |
| 12295 | * last removed linfo. prog is already modified, so prog->len == off |
| 12296 | * means no live instructions after (tail of the program was removed). |
| 12297 | */ |
| 12298 | if (prog->len != off && l_cnt && |
| 12299 | (i == nr_linfo || linfo[i].insn_off != off + cnt)) { |
| 12300 | l_cnt--; |
| 12301 | linfo[--i].insn_off = off + cnt; |
| 12302 | } |
| 12303 | |
| 12304 | /* remove the line info which refer to the removed instructions */ |
| 12305 | if (l_cnt) { |
| 12306 | memmove(linfo + l_off, linfo + i, |
| 12307 | sizeof(*linfo) * (nr_linfo - i)); |
| 12308 | |
| 12309 | prog->aux->nr_linfo -= l_cnt; |
| 12310 | nr_linfo = prog->aux->nr_linfo; |
| 12311 | } |
| 12312 | |
| 12313 | /* pull all linfo[i].insn_off >= off + cnt in by cnt */ |
| 12314 | for (i = l_off; i < nr_linfo; i++) |
| 12315 | linfo[i].insn_off -= cnt; |
| 12316 | |
| 12317 | /* fix up all subprogs (incl. 'exit') which start >= off */ |
| 12318 | for (i = 0; i <= env->subprog_cnt; i++) |
| 12319 | if (env->subprog_info[i].linfo_idx > l_off) { |
| 12320 | /* program may have started in the removed region but |
| 12321 | * may not be fully removed |
| 12322 | */ |
| 12323 | if (env->subprog_info[i].linfo_idx >= l_off + l_cnt) |
| 12324 | env->subprog_info[i].linfo_idx -= l_cnt; |
| 12325 | else |
| 12326 | env->subprog_info[i].linfo_idx = l_off; |
| 12327 | } |
| 12328 | |
| 12329 | return 0; |
| 12330 | } |
| 12331 | |
| 12332 | static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) |
| 12333 | { |
| 12334 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12335 | unsigned int orig_prog_len = env->prog->len; |
| 12336 | int err; |
| 12337 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 12338 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 12339 | bpf_prog_offload_remove_insns(env, off, cnt); |
| 12340 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12341 | err = bpf_remove_insns(env->prog, off, cnt); |
| 12342 | if (err) |
| 12343 | return err; |
| 12344 | |
| 12345 | err = adjust_subprog_starts_after_remove(env, off, cnt); |
| 12346 | if (err) |
| 12347 | return err; |
| 12348 | |
| 12349 | err = bpf_adj_linfo_after_remove(env, off, cnt); |
| 12350 | if (err) |
| 12351 | return err; |
| 12352 | |
| 12353 | memmove(aux_data + off, aux_data + off + cnt, |
| 12354 | sizeof(*aux_data) * (orig_prog_len - off - cnt)); |
| 12355 | |
| 12356 | return 0; |
| 12357 | } |
| 12358 | |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12359 | /* The verifier does more data flow analysis than llvm and will not |
| 12360 | * explore branches that are dead at run time. Malicious programs can |
| 12361 | * have dead code too. Therefore replace all dead at-run-time code |
| 12362 | * with 'ja -1'. |
| 12363 | * |
| 12364 | * Just nops are not optimal, e.g. if they would sit at the end of the |
| 12365 | * program and through another bug we would manage to jump there, then |
| 12366 | * we'd execute beyond program memory otherwise. Returning exception |
| 12367 | * code also wouldn't work since we can have subprogs where the dead |
| 12368 | * code could be located. |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12369 | */ |
| 12370 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 12371 | { |
| 12372 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12373 | struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12374 | struct bpf_insn *insn = env->prog->insnsi; |
| 12375 | const int insn_cnt = env->prog->len; |
| 12376 | int i; |
| 12377 | |
| 12378 | for (i = 0; i < insn_cnt; i++) { |
| 12379 | if (aux_data[i].seen) |
| 12380 | continue; |
Daniel Borkmann | 2a5418a | 2018-01-26 23:33:37 +0100 | [diff] [blame] | 12381 | memcpy(insn + i, &trap, sizeof(trap)); |
Ilya Leoshkevich | 45c709f | 2021-08-12 17:18:10 +0200 | [diff] [blame] | 12382 | aux_data[i].zext_dst = false; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 12383 | } |
| 12384 | } |
| 12385 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12386 | static bool insn_is_cond_jump(u8 code) |
| 12387 | { |
| 12388 | u8 op; |
| 12389 | |
Jiong Wang | 092ed09 | 2019-01-26 12:26:01 -0500 | [diff] [blame] | 12390 | if (BPF_CLASS(code) == BPF_JMP32) |
| 12391 | return true; |
| 12392 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12393 | if (BPF_CLASS(code) != BPF_JMP) |
| 12394 | return false; |
| 12395 | |
| 12396 | op = BPF_OP(code); |
| 12397 | return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL; |
| 12398 | } |
| 12399 | |
| 12400 | static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env) |
| 12401 | { |
| 12402 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12403 | struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 12404 | struct bpf_insn *insn = env->prog->insnsi; |
| 12405 | const int insn_cnt = env->prog->len; |
| 12406 | int i; |
| 12407 | |
| 12408 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 12409 | if (!insn_is_cond_jump(insn->code)) |
| 12410 | continue; |
| 12411 | |
| 12412 | if (!aux_data[i + 1].seen) |
| 12413 | ja.off = insn->off; |
| 12414 | else if (!aux_data[i + 1 + insn->off].seen) |
| 12415 | ja.off = 0; |
| 12416 | else |
| 12417 | continue; |
| 12418 | |
Jakub Kicinski | 08ca90a | 2019-01-22 22:45:24 -0800 | [diff] [blame] | 12419 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
| 12420 | bpf_prog_offload_replace_insn(env, i, &ja); |
| 12421 | |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 12422 | memcpy(insn, &ja, sizeof(ja)); |
| 12423 | } |
| 12424 | } |
| 12425 | |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 12426 | static int opt_remove_dead_code(struct bpf_verifier_env *env) |
| 12427 | { |
| 12428 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 12429 | int insn_cnt = env->prog->len; |
| 12430 | int i, err; |
| 12431 | |
| 12432 | for (i = 0; i < insn_cnt; i++) { |
| 12433 | int j; |
| 12434 | |
| 12435 | j = 0; |
| 12436 | while (i + j < insn_cnt && !aux_data[i + j].seen) |
| 12437 | j++; |
| 12438 | if (!j) |
| 12439 | continue; |
| 12440 | |
| 12441 | err = verifier_remove_insns(env, i, j); |
| 12442 | if (err) |
| 12443 | return err; |
| 12444 | insn_cnt = env->prog->len; |
| 12445 | } |
| 12446 | |
| 12447 | return 0; |
| 12448 | } |
| 12449 | |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 12450 | static int opt_remove_nops(struct bpf_verifier_env *env) |
| 12451 | { |
| 12452 | const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0); |
| 12453 | struct bpf_insn *insn = env->prog->insnsi; |
| 12454 | int insn_cnt = env->prog->len; |
| 12455 | int i, err; |
| 12456 | |
| 12457 | for (i = 0; i < insn_cnt; i++) { |
| 12458 | if (memcmp(&insn[i], &ja, sizeof(ja))) |
| 12459 | continue; |
| 12460 | |
| 12461 | err = verifier_remove_insns(env, i, 1); |
| 12462 | if (err) |
| 12463 | return err; |
| 12464 | insn_cnt--; |
| 12465 | i--; |
| 12466 | } |
| 12467 | |
| 12468 | return 0; |
| 12469 | } |
| 12470 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12471 | static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, |
| 12472 | const union bpf_attr *attr) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12473 | { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12474 | struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4]; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12475 | struct bpf_insn_aux_data *aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12476 | int i, patch_len, delta = 0, len = env->prog->len; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12477 | struct bpf_insn *insns = env->prog->insnsi; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12478 | struct bpf_prog *new_prog; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12479 | bool rnd_hi32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12480 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12481 | rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12482 | zext_patch[1] = BPF_ZEXT_REG(0); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12483 | rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0); |
| 12484 | rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32); |
| 12485 | rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12486 | for (i = 0; i < len; i++) { |
| 12487 | int adj_idx = i + delta; |
| 12488 | struct bpf_insn insn; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12489 | int load_reg; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12490 | |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12491 | insn = insns[adj_idx]; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12492 | load_reg = insn_def_regno(&insn); |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12493 | if (!aux[adj_idx].zext_dst) { |
| 12494 | u8 code, class; |
| 12495 | u32 imm_rnd; |
| 12496 | |
| 12497 | if (!rnd_hi32) |
| 12498 | continue; |
| 12499 | |
| 12500 | code = insn.code; |
| 12501 | class = BPF_CLASS(code); |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12502 | if (load_reg == -1) |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12503 | continue; |
| 12504 | |
| 12505 | /* NOTE: arg "reg" (the fourth one) is only used for |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12506 | * BPF_STX + SRC_OP, so it is safe to pass NULL |
| 12507 | * here. |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12508 | */ |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12509 | if (is_reg64(env, &insn, load_reg, NULL, DST_OP)) { |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12510 | if (class == BPF_LD && |
| 12511 | BPF_MODE(code) == BPF_IMM) |
| 12512 | i++; |
| 12513 | continue; |
| 12514 | } |
| 12515 | |
| 12516 | /* ctx load could be transformed into wider load. */ |
| 12517 | if (class == BPF_LDX && |
| 12518 | aux[adj_idx].ptr_type == PTR_TO_CTX) |
| 12519 | continue; |
| 12520 | |
| 12521 | imm_rnd = get_random_int(); |
| 12522 | rnd_hi32_patch[0] = insn; |
| 12523 | rnd_hi32_patch[1].imm = imm_rnd; |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12524 | rnd_hi32_patch[3].dst_reg = load_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12525 | patch = rnd_hi32_patch; |
| 12526 | patch_len = 4; |
| 12527 | goto apply_patch_buffer; |
| 12528 | } |
| 12529 | |
Brendan Jackman | 3949186 | 2021-03-04 18:56:46 -0800 | [diff] [blame] | 12530 | /* Add in an zero-extend instruction if a) the JIT has requested |
| 12531 | * it or b) it's a CMPXCHG. |
| 12532 | * |
| 12533 | * The latter is because: BPF_CMPXCHG always loads a value into |
| 12534 | * R0, therefore always zero-extends. However some archs' |
| 12535 | * equivalent instruction only does this load when the |
| 12536 | * comparison is successful. This detail of CMPXCHG is |
| 12537 | * orthogonal to the general zero-extension behaviour of the |
| 12538 | * CPU, so it's treated independently of bpf_jit_needs_zext. |
| 12539 | */ |
| 12540 | if (!bpf_jit_needs_zext() && !is_cmpxchg_insn(&insn)) |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12541 | continue; |
| 12542 | |
Ilya Leoshkevich | 83a2881 | 2021-03-01 16:40:19 +0100 | [diff] [blame] | 12543 | if (WARN_ON(load_reg == -1)) { |
| 12544 | verbose(env, "verifier bug. zext_dst is set, but no reg is defined\n"); |
| 12545 | return -EFAULT; |
Ilya Leoshkevich | b2e37a7 | 2021-02-10 21:45:02 +0100 | [diff] [blame] | 12546 | } |
| 12547 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12548 | zext_patch[0] = insn; |
Ilya Leoshkevich | b2e37a7 | 2021-02-10 21:45:02 +0100 | [diff] [blame] | 12549 | zext_patch[1].dst_reg = load_reg; |
| 12550 | zext_patch[1].src_reg = load_reg; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12551 | patch = zext_patch; |
| 12552 | patch_len = 2; |
| 12553 | apply_patch_buffer: |
| 12554 | new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len); |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12555 | if (!new_prog) |
| 12556 | return -ENOMEM; |
| 12557 | env->prog = new_prog; |
| 12558 | insns = new_prog->insnsi; |
| 12559 | aux = env->insn_aux_data; |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 12560 | delta += patch_len - 1; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 12561 | } |
| 12562 | |
| 12563 | return 0; |
| 12564 | } |
| 12565 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12566 | /* convert load instructions that access fields of a context type into a |
| 12567 | * sequence of instructions that access fields of the underlying structure: |
| 12568 | * struct __sk_buff -> struct sk_buff |
| 12569 | * struct bpf_sock_ops -> struct sock |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12570 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 12571 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12572 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 12573 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12574 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12575 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12576 | struct bpf_insn insn_buf[16], *insn; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12577 | u32 target_size, size_default, off; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12578 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12579 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12580 | bool is_narrower_load; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12581 | |
Daniel Borkmann | b09928b | 2018-10-24 22:05:49 +0200 | [diff] [blame] | 12582 | if (ops->gen_prologue || env->seen_direct_write) { |
| 12583 | if (!ops->gen_prologue) { |
| 12584 | verbose(env, "bpf verifier is misconfigured\n"); |
| 12585 | return -EINVAL; |
| 12586 | } |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12587 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 12588 | env->prog); |
| 12589 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12590 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12591 | return -EINVAL; |
| 12592 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12593 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12594 | if (!new_prog) |
| 12595 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12596 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12597 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12598 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12599 | } |
| 12600 | } |
| 12601 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12602 | if (bpf_prog_is_dev_bound(env->prog->aux)) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12603 | return 0; |
| 12604 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12605 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 12606 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12607 | for (i = 0; i < insn_cnt; i++, insn++) { |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12608 | bpf_convert_ctx_access_t convert_ctx_access; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12609 | bool ctx_access; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12610 | |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 12611 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 12612 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 12613 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12614 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12615 | type = BPF_READ; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12616 | ctx_access = true; |
| 12617 | } else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 12618 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 12619 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 12620 | insn->code == (BPF_STX | BPF_MEM | BPF_DW) || |
| 12621 | insn->code == (BPF_ST | BPF_MEM | BPF_B) || |
| 12622 | insn->code == (BPF_ST | BPF_MEM | BPF_H) || |
| 12623 | insn->code == (BPF_ST | BPF_MEM | BPF_W) || |
| 12624 | insn->code == (BPF_ST | BPF_MEM | BPF_DW)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 12625 | type = BPF_WRITE; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12626 | ctx_access = BPF_CLASS(insn->code) == BPF_STX; |
| 12627 | } else { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12628 | continue; |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12629 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12630 | |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12631 | if (type == BPF_WRITE && |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12632 | env->insn_aux_data[i + delta].sanitize_stack_spill) { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12633 | struct bpf_insn patch[] = { |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12634 | *insn, |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12635 | BPF_ST_NOSPEC(), |
Alexei Starovoitov | af86ca4 | 2018-05-15 09:27:05 -0700 | [diff] [blame] | 12636 | }; |
| 12637 | |
| 12638 | cnt = ARRAY_SIZE(patch); |
| 12639 | new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt); |
| 12640 | if (!new_prog) |
| 12641 | return -ENOMEM; |
| 12642 | |
| 12643 | delta += cnt - 1; |
| 12644 | env->prog = new_prog; |
| 12645 | insn = new_prog->insnsi + i + delta; |
| 12646 | continue; |
| 12647 | } |
| 12648 | |
Daniel Borkmann | 2039f26 | 2021-07-13 08:18:31 +0000 | [diff] [blame] | 12649 | if (!ctx_access) |
| 12650 | continue; |
| 12651 | |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12652 | switch (env->insn_aux_data[i + delta].ptr_type) { |
| 12653 | case PTR_TO_CTX: |
| 12654 | if (!ops->convert_ctx_access) |
| 12655 | continue; |
| 12656 | convert_ctx_access = ops->convert_ctx_access; |
| 12657 | break; |
| 12658 | case PTR_TO_SOCKET: |
Martin KaFai Lau | 46f8bc9 | 2019-02-09 23:22:20 -0800 | [diff] [blame] | 12659 | case PTR_TO_SOCK_COMMON: |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12660 | convert_ctx_access = bpf_sock_convert_ctx_access; |
| 12661 | break; |
Martin KaFai Lau | 655a51e | 2019-02-09 23:22:24 -0800 | [diff] [blame] | 12662 | case PTR_TO_TCP_SOCK: |
| 12663 | convert_ctx_access = bpf_tcp_sock_convert_ctx_access; |
| 12664 | break; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 12665 | case PTR_TO_XDP_SOCK: |
| 12666 | convert_ctx_access = bpf_xdp_sock_convert_ctx_access; |
| 12667 | break; |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12668 | case PTR_TO_BTF_ID: |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 12669 | if (type == BPF_READ) { |
| 12670 | insn->code = BPF_LDX | BPF_PROBE_MEM | |
| 12671 | BPF_SIZE((insn)->code); |
| 12672 | env->prog->aux->num_exentries++; |
Udip Pant | 7e40781 | 2020-08-25 16:20:00 -0700 | [diff] [blame] | 12673 | } else if (resolve_prog_type(env->prog) != BPF_PROG_TYPE_STRUCT_OPS) { |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12674 | verbose(env, "Writes through BTF pointers are not allowed\n"); |
| 12675 | return -EINVAL; |
| 12676 | } |
Alexei Starovoitov | 2a02759 | 2019-10-15 20:25:02 -0700 | [diff] [blame] | 12677 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12678 | default: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12679 | continue; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12680 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12681 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12682 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12683 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12684 | |
| 12685 | /* If the read access is a narrower load of the field, |
| 12686 | * convert to a 4/8-byte load, to minimum program type specific |
| 12687 | * convert_ctx_access changes. If conversion is successful, |
| 12688 | * we will apply proper mask to the result. |
| 12689 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12690 | is_narrower_load = size < ctx_field_size; |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12691 | size_default = bpf_ctx_off_adjust_machine(ctx_field_size); |
| 12692 | off = insn->off; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12693 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12694 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12695 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12696 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12697 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12698 | return -EINVAL; |
| 12699 | } |
| 12700 | |
| 12701 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12702 | if (ctx_field_size == 4) |
| 12703 | size_code = BPF_W; |
| 12704 | else if (ctx_field_size == 8) |
| 12705 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12706 | |
Daniel Borkmann | bc23105 | 2018-06-02 23:06:39 +0200 | [diff] [blame] | 12707 | insn->off = off & ~(size_default - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12708 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 12709 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12710 | |
| 12711 | target_size = 0; |
Joe Stringer | c64b798 | 2018-10-02 13:35:33 -0700 | [diff] [blame] | 12712 | cnt = convert_ctx_access(type, insn, insn_buf, env->prog, |
| 12713 | &target_size); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12714 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 12715 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 12716 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12717 | return -EINVAL; |
| 12718 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12719 | |
| 12720 | if (is_narrower_load && size < target_size) { |
Ilya Leoshkevich | d895a0f | 2019-08-16 12:53:00 +0200 | [diff] [blame] | 12721 | u8 shift = bpf_ctx_narrow_access_offset( |
| 12722 | off, size, size_default) * 8; |
Andrey Ignatov | d7af7e4 | 2021-08-20 09:39:35 -0700 | [diff] [blame] | 12723 | if (shift && cnt + 1 >= ARRAY_SIZE(insn_buf)) { |
| 12724 | verbose(env, "bpf verifier narrow ctx load misconfigured\n"); |
| 12725 | return -EINVAL; |
| 12726 | } |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12727 | if (ctx_field_size <= 4) { |
| 12728 | if (shift) |
| 12729 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH, |
| 12730 | insn->dst_reg, |
| 12731 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12732 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 12733 | (1 << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12734 | } else { |
| 12735 | if (shift) |
| 12736 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH, |
| 12737 | insn->dst_reg, |
| 12738 | shift); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12739 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Krzesimir Nowak | e2f7fc0 | 2019-05-08 18:08:58 +0200 | [diff] [blame] | 12740 | (1ULL << size * 8) - 1); |
Andrey Ignatov | 46f53a6 | 2018-11-10 22:15:13 -0800 | [diff] [blame] | 12741 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 12742 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12743 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 12744 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12745 | if (!new_prog) |
| 12746 | return -ENOMEM; |
| 12747 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12748 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12749 | |
| 12750 | /* keep walking new program and skip insns we just inserted */ |
| 12751 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 12752 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 12753 | } |
| 12754 | |
| 12755 | return 0; |
| 12756 | } |
| 12757 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12758 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 12759 | { |
| 12760 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 12761 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12762 | struct bpf_map *map_ptr; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12763 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12764 | void *old_bpf_func; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 12765 | int err, num_exentries; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12766 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12767 | if (env->subprog_cnt <= 1) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12768 | return 0; |
| 12769 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12770 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12771 | if (!bpf_pseudo_func(insn) && !bpf_pseudo_call(insn)) |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12772 | continue; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12773 | |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12774 | /* Upon error here we cannot fall back to interpreter but |
| 12775 | * need a hard reject of the program. Thus -EFAULT is |
| 12776 | * propagated in any case. |
| 12777 | */ |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12778 | subprog = find_subprog(env, i + insn->imm + 1); |
| 12779 | if (subprog < 0) { |
| 12780 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 12781 | i + insn->imm + 1); |
| 12782 | return -EFAULT; |
| 12783 | } |
| 12784 | /* temporarily remember subprog id inside insn instead of |
| 12785 | * aux_data, since next loop will split up all insns into funcs |
| 12786 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12787 | insn->off = subprog; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12788 | /* remember original imm in case JIT fails and fallback |
| 12789 | * to interpreter will be needed |
| 12790 | */ |
| 12791 | env->insn_aux_data[i].call_imm = insn->imm; |
| 12792 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 12793 | insn->imm = 1; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12794 | if (bpf_pseudo_func(insn)) |
| 12795 | /* jit (e.g. x86_64) may emit fewer instructions |
| 12796 | * if it learns a u32 imm is the same as a u64 imm. |
| 12797 | * Force a non zero here. |
| 12798 | */ |
| 12799 | insn[1].imm = 1; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12800 | } |
| 12801 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 12802 | err = bpf_prog_alloc_jited_linfo(prog); |
| 12803 | if (err) |
| 12804 | goto out_undo_insn; |
| 12805 | |
| 12806 | err = -ENOMEM; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 12807 | func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12808 | if (!func) |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12809 | goto out_undo_insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12810 | |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12811 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12812 | subprog_start = subprog_end; |
Jiong Wang | 4cb3d99 | 2018-05-02 16:17:19 -0400 | [diff] [blame] | 12813 | subprog_end = env->subprog_info[i + 1].start; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12814 | |
| 12815 | len = subprog_end - subprog_start; |
Andrii Nakryiko | fb7dd8b | 2021-08-15 00:05:54 -0700 | [diff] [blame] | 12816 | /* bpf_prog_run() doesn't call subprogs directly, |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 12817 | * hence main prog stats include the runtime of subprogs. |
| 12818 | * subprogs don't have IDs and not reachable via prog_get_next_id |
Alexei Starovoitov | 700d479 | 2021-02-09 19:36:26 -0800 | [diff] [blame] | 12819 | * func[i]->stats will never be accessed and stays NULL |
Alexei Starovoitov | 492ecee | 2019-02-25 14:28:39 -0800 | [diff] [blame] | 12820 | */ |
| 12821 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12822 | if (!func[i]) |
| 12823 | goto out_free; |
| 12824 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 12825 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 12826 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12827 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 12828 | if (bpf_prog_calc_tag(func[i])) |
| 12829 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12830 | func[i]->is_func = 1; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12831 | func[i]->aux->func_idx = i; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12832 | /* Below members will be freed only at prog->aux */ |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12833 | func[i]->aux->btf = prog->aux->btf; |
| 12834 | func[i]->aux->func_info = prog->aux->func_info; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12835 | func[i]->aux->poke_tab = prog->aux->poke_tab; |
| 12836 | func[i]->aux->size_poke_tab = prog->aux->size_poke_tab; |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 12837 | |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12838 | for (j = 0; j < prog->aux->size_poke_tab; j++) { |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12839 | struct bpf_jit_poke_descriptor *poke; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12840 | |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12841 | poke = &prog->aux->poke_tab[j]; |
| 12842 | if (poke->insn_idx < subprog_end && |
| 12843 | poke->insn_idx >= subprog_start) |
| 12844 | poke->aux = func[i]->aux; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12845 | } |
| 12846 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12847 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 12848 | * Long term would need debug info to populate names |
| 12849 | */ |
| 12850 | func[i]->aux->name[0] = 'F'; |
Jiong Wang | 9c8105b | 2018-05-02 16:17:18 -0400 | [diff] [blame] | 12851 | func[i]->aux->stack_depth = env->subprog_info[i].stack_depth; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12852 | func[i]->jit_requested = 1; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 12853 | func[i]->aux->kfunc_tab = prog->aux->kfunc_tab; |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 12854 | func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab; |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 12855 | func[i]->aux->linfo = prog->aux->linfo; |
| 12856 | func[i]->aux->nr_linfo = prog->aux->nr_linfo; |
| 12857 | func[i]->aux->jited_linfo = prog->aux->jited_linfo; |
| 12858 | func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx; |
Yonghong Song | c4c0bdc | 2020-06-23 17:10:54 -0700 | [diff] [blame] | 12859 | num_exentries = 0; |
| 12860 | insn = func[i]->insnsi; |
| 12861 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 12862 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 12863 | BPF_MODE(insn->code) == BPF_PROBE_MEM) |
| 12864 | num_exentries++; |
| 12865 | } |
| 12866 | func[i]->aux->num_exentries = num_exentries; |
Maciej Fijalkowski | ebf7d1f | 2020-09-16 23:10:08 +0200 | [diff] [blame] | 12867 | func[i]->aux->tail_call_reachable = env->subprog_info[i].tail_call_reachable; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12868 | func[i] = bpf_int_jit_compile(func[i]); |
| 12869 | if (!func[i]->jited) { |
| 12870 | err = -ENOTSUPP; |
| 12871 | goto out_free; |
| 12872 | } |
| 12873 | cond_resched(); |
| 12874 | } |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12875 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12876 | /* at this point all bpf functions were successfully JITed |
| 12877 | * now populate all bpf_calls with correct addresses and |
| 12878 | * run last pass of JIT |
| 12879 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12880 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12881 | insn = func[i]->insnsi; |
| 12882 | for (j = 0; j < func[i]->len; j++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12883 | if (bpf_pseudo_func(insn)) { |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12884 | subprog = insn->off; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12885 | insn[0].imm = (u32)(long)func[subprog]->bpf_func; |
| 12886 | insn[1].imm = ((u64)(long)func[subprog]->bpf_func) >> 32; |
| 12887 | continue; |
| 12888 | } |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12889 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12890 | continue; |
| 12891 | subprog = insn->off; |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 12892 | insn->imm = BPF_CALL_IMM(func[subprog]->bpf_func); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12893 | } |
Sandipan Das | 2162fed | 2018-05-24 12:26:45 +0530 | [diff] [blame] | 12894 | |
| 12895 | /* we use the aux data to keep a list of the start addresses |
| 12896 | * of the JITed images for each function in the program |
| 12897 | * |
| 12898 | * for some architectures, such as powerpc64, the imm field |
| 12899 | * might not be large enough to hold the offset of the start |
| 12900 | * address of the callee's JITed image from __bpf_call_base |
| 12901 | * |
| 12902 | * in such cases, we can lookup the start address of a callee |
| 12903 | * by using its subprog id, available from the off field of |
| 12904 | * the call instruction, as an index for this list |
| 12905 | */ |
| 12906 | func[i]->aux->func = func; |
| 12907 | func[i]->aux->func_cnt = env->subprog_cnt; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12908 | } |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12909 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12910 | old_bpf_func = func[i]->bpf_func; |
| 12911 | tmp = bpf_int_jit_compile(func[i]); |
| 12912 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 12913 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12914 | err = -ENOTSUPP; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12915 | goto out_free; |
| 12916 | } |
| 12917 | cond_resched(); |
| 12918 | } |
| 12919 | |
| 12920 | /* finally lock prog and jit images for all functions and |
| 12921 | * populate kallsysm |
| 12922 | */ |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12923 | for (i = 0; i < env->subprog_cnt; i++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12924 | bpf_prog_lock_ro(func[i]); |
| 12925 | bpf_prog_kallsyms_add(func[i]); |
| 12926 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12927 | |
| 12928 | /* Last step: make now unused interpreter insns from main |
| 12929 | * prog consistent for later dump requests, so they can |
| 12930 | * later look the same as if they were interpreted only. |
| 12931 | */ |
| 12932 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12933 | if (bpf_pseudo_func(insn)) { |
| 12934 | insn[0].imm = env->insn_aux_data[i].call_imm; |
Martin KaFai Lau | 3990ed4 | 2021-11-05 18:40:14 -0700 | [diff] [blame] | 12935 | insn[1].imm = insn->off; |
| 12936 | insn->off = 0; |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 12937 | continue; |
| 12938 | } |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12939 | if (!bpf_pseudo_call(insn)) |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12940 | continue; |
| 12941 | insn->off = env->insn_aux_data[i].call_imm; |
| 12942 | subprog = find_subprog(env, i + insn->off + 1); |
Sandipan Das | dbecd73 | 2018-05-24 12:26:48 +0530 | [diff] [blame] | 12943 | insn->imm = subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 12944 | } |
| 12945 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12946 | prog->jited = 1; |
| 12947 | prog->bpf_func = func[0]->bpf_func; |
| 12948 | prog->aux->func = func; |
Jiong Wang | f910cef | 2018-05-02 16:17:17 -0400 | [diff] [blame] | 12949 | prog->aux->func_cnt = env->subprog_cnt; |
Martin KaFai Lau | e16301f | 2021-03-24 18:51:30 -0700 | [diff] [blame] | 12950 | bpf_prog_jit_attempt_done(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12951 | return 0; |
| 12952 | out_free: |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12953 | /* We failed JIT'ing, so at this point we need to unregister poke |
| 12954 | * descriptors from subprogs, so that kernel is not attempting to |
| 12955 | * patch it anymore as we're freeing the subprog JIT memory. |
| 12956 | */ |
| 12957 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 12958 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 12959 | map_ptr->ops->map_poke_untrack(map_ptr, prog->aux); |
| 12960 | } |
| 12961 | /* At this point we're guaranteed that poke descriptors are not |
| 12962 | * live anymore. We can just unlink its descriptor table as it's |
| 12963 | * released with the main prog. |
| 12964 | */ |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12965 | for (i = 0; i < env->subprog_cnt; i++) { |
| 12966 | if (!func[i]) |
| 12967 | continue; |
John Fastabend | f263a81 | 2021-07-07 15:38:47 -0700 | [diff] [blame] | 12968 | func[i]->aux->poke_tab = NULL; |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 12969 | bpf_jit_free(func[i]); |
| 12970 | } |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12971 | kfree(func); |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 12972 | out_undo_insn: |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12973 | /* cleanup main prog to be interpreted */ |
| 12974 | prog->jit_requested = 0; |
| 12975 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 12976 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12977 | continue; |
| 12978 | insn->off = 0; |
| 12979 | insn->imm = env->insn_aux_data[i].call_imm; |
| 12980 | } |
Martin KaFai Lau | e16301f | 2021-03-24 18:51:30 -0700 | [diff] [blame] | 12981 | bpf_prog_jit_attempt_done(prog); |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12982 | return err; |
| 12983 | } |
| 12984 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 12985 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 12986 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 12987 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 12988 | struct bpf_prog *prog = env->prog; |
| 12989 | struct bpf_insn *insn = prog->insnsi; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 12990 | bool has_kfunc_call = bpf_prog_has_kfunc_call(prog); |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 12991 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 12992 | #endif |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 12993 | int err = 0; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 12994 | |
Quentin Monnet | e4052d0 | 2018-10-07 12:56:58 +0100 | [diff] [blame] | 12995 | if (env->prog->jit_requested && |
| 12996 | !bpf_prog_is_dev_bound(env->prog->aux)) { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 12997 | err = jit_subprogs(env); |
| 12998 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 12999 | return 0; |
Daniel Borkmann | c7a8978 | 2018-07-12 21:44:28 +0200 | [diff] [blame] | 13000 | if (err == -EFAULT) |
| 13001 | return err; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13002 | } |
| 13003 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13004 | if (has_kfunc_call) { |
| 13005 | verbose(env, "calling kernel functions are not allowed in non-JITed programs\n"); |
| 13006 | return -EINVAL; |
| 13007 | } |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 13008 | if (env->subprog_cnt > 1 && env->prog->aux->tail_call_reachable) { |
| 13009 | /* When JIT fails the progs with bpf2bpf calls and tail_calls |
| 13010 | * have to be rejected, since interpreter doesn't support them yet. |
| 13011 | */ |
| 13012 | verbose(env, "tail_calls are not allowed in non-JITed programs with bpf-to-bpf calls\n"); |
| 13013 | return -EINVAL; |
| 13014 | } |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13015 | for (i = 0; i < prog->len; i++, insn++) { |
Yonghong Song | 69c087b | 2021-02-26 12:49:25 -0800 | [diff] [blame] | 13016 | if (bpf_pseudo_func(insn)) { |
| 13017 | /* When JIT fails the progs with callback calls |
| 13018 | * have to be rejected, since interpreter doesn't support them yet. |
| 13019 | */ |
| 13020 | verbose(env, "callbacks are not allowed in non-JITed programs\n"); |
| 13021 | return -EINVAL; |
| 13022 | } |
| 13023 | |
Yonghong Song | 23a2d70 | 2021-02-04 15:48:27 -0800 | [diff] [blame] | 13024 | if (!bpf_pseudo_call(insn)) |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13025 | continue; |
| 13026 | depth = get_callee_stack_depth(env, insn, i); |
| 13027 | if (depth < 0) |
| 13028 | return depth; |
| 13029 | bpf_patch_call_args(insn, depth); |
| 13030 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 13031 | err = 0; |
| 13032 | #endif |
| 13033 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 13034 | } |
| 13035 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13036 | static int fixup_kfunc_call(struct bpf_verifier_env *env, |
| 13037 | struct bpf_insn *insn) |
| 13038 | { |
| 13039 | const struct bpf_kfunc_desc *desc; |
| 13040 | |
Kumar Kartikeya Dwivedi | a5d8272 | 2021-10-02 06:47:50 +0530 | [diff] [blame] | 13041 | if (!insn->imm) { |
| 13042 | verbose(env, "invalid kernel function call not eliminated in verifier pass\n"); |
| 13043 | return -EINVAL; |
| 13044 | } |
| 13045 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13046 | /* insn->imm has the btf func_id. Replace it with |
| 13047 | * an address (relative to __bpf_base_call). |
| 13048 | */ |
Kumar Kartikeya Dwivedi | 2357672 | 2021-10-02 06:47:49 +0530 | [diff] [blame] | 13049 | desc = find_kfunc_desc(env->prog, insn->imm, insn->off); |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13050 | if (!desc) { |
| 13051 | verbose(env, "verifier internal error: kernel function descriptor not found for func_id %u\n", |
| 13052 | insn->imm); |
| 13053 | return -EFAULT; |
| 13054 | } |
| 13055 | |
| 13056 | insn->imm = desc->imm; |
| 13057 | |
| 13058 | return 0; |
| 13059 | } |
| 13060 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13061 | /* Do various post-verification rewrites in a single program pass. |
| 13062 | * These rewrites simplify JIT and interpreter implementations. |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13063 | */ |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13064 | static int do_misc_fixups(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13065 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13066 | struct bpf_prog *prog = env->prog; |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13067 | enum bpf_attach_type eatype = prog->expected_attach_type; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13068 | bool expect_blinding = bpf_jit_blinding_enabled(prog); |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13069 | enum bpf_prog_type prog_type = resolve_prog_type(prog); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13070 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13071 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13072 | const int insn_cnt = prog->len; |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13073 | const struct bpf_map_ops *ops; |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13074 | struct bpf_insn_aux_data *aux; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13075 | struct bpf_insn insn_buf[16]; |
| 13076 | struct bpf_prog *new_prog; |
| 13077 | struct bpf_map *map_ptr; |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13078 | int i, ret, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13079 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13080 | for (i = 0; i < insn_cnt; i++, insn++) { |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13081 | /* Make divide-by-zero exceptions impossible. */ |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13082 | if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) || |
| 13083 | insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) || |
| 13084 | insn->code == (BPF_ALU | BPF_MOD | BPF_X) || |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 13085 | insn->code == (BPF_ALU | BPF_DIV | BPF_X)) { |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13086 | bool is64 = BPF_CLASS(insn->code) == BPF_ALU64; |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13087 | bool isdiv = BPF_OP(insn->code) == BPF_DIV; |
| 13088 | struct bpf_insn *patchlet; |
| 13089 | struct bpf_insn chk_and_div[] = { |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13090 | /* [R,W]x div 0 -> 0 */ |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13091 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 13092 | BPF_JNE | BPF_K, insn->src_reg, |
| 13093 | 0, 2, 0), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13094 | BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg), |
| 13095 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 13096 | *insn, |
| 13097 | }; |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13098 | struct bpf_insn chk_and_mod[] = { |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13099 | /* [R,W]x mod 0 -> [R,W]x */ |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13100 | BPF_RAW_INSN((is64 ? BPF_JMP : BPF_JMP32) | |
| 13101 | BPF_JEQ | BPF_K, insn->src_reg, |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13102 | 0, 1 + (is64 ? 0 : 1), 0), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13103 | *insn, |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13104 | BPF_JMP_IMM(BPF_JA, 0, 0, 1), |
| 13105 | BPF_MOV32_REG(insn->dst_reg, insn->dst_reg), |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13106 | }; |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13107 | |
Daniel Borkmann | e88b2c6 | 2021-02-09 18:46:10 +0000 | [diff] [blame] | 13108 | patchlet = isdiv ? chk_and_div : chk_and_mod; |
| 13109 | cnt = isdiv ? ARRAY_SIZE(chk_and_div) : |
Daniel Borkmann | 9b00f1b | 2021-02-10 14:14:42 +0100 | [diff] [blame] | 13110 | ARRAY_SIZE(chk_and_mod) - (is64 ? 2 : 0); |
Daniel Borkmann | f6b1b3b | 2018-01-26 23:33:39 +0100 | [diff] [blame] | 13111 | |
| 13112 | new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt); |
Alexei Starovoitov | 68fda45 | 2018-01-12 18:59:52 -0800 | [diff] [blame] | 13113 | if (!new_prog) |
| 13114 | return -ENOMEM; |
| 13115 | |
| 13116 | delta += cnt - 1; |
| 13117 | env->prog = prog = new_prog; |
| 13118 | insn = new_prog->insnsi + i + delta; |
| 13119 | continue; |
| 13120 | } |
| 13121 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13122 | /* Implement LD_ABS and LD_IND with a rewrite, if supported by the program type. */ |
Daniel Borkmann | e0cea7c | 2018-05-04 01:08:14 +0200 | [diff] [blame] | 13123 | if (BPF_CLASS(insn->code) == BPF_LD && |
| 13124 | (BPF_MODE(insn->code) == BPF_ABS || |
| 13125 | BPF_MODE(insn->code) == BPF_IND)) { |
| 13126 | cnt = env->ops->gen_ld_abs(insn, insn_buf); |
| 13127 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 13128 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13129 | return -EINVAL; |
| 13130 | } |
| 13131 | |
| 13132 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13133 | if (!new_prog) |
| 13134 | return -ENOMEM; |
| 13135 | |
| 13136 | delta += cnt - 1; |
| 13137 | env->prog = prog = new_prog; |
| 13138 | insn = new_prog->insnsi + i + delta; |
| 13139 | continue; |
| 13140 | } |
| 13141 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13142 | /* Rewrite pointer arithmetic to mitigate speculation attacks. */ |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13143 | if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) || |
| 13144 | insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) { |
| 13145 | const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X; |
| 13146 | const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13147 | struct bpf_insn *patch = &insn_buf[0]; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13148 | bool issrc, isneg, isimm; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13149 | u32 off_reg; |
| 13150 | |
| 13151 | aux = &env->insn_aux_data[i + delta]; |
Daniel Borkmann | 3612af7 | 2019-03-01 22:05:29 +0100 | [diff] [blame] | 13152 | if (!aux->alu_state || |
| 13153 | aux->alu_state == BPF_ALU_NON_POINTER) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13154 | continue; |
| 13155 | |
| 13156 | isneg = aux->alu_state & BPF_ALU_NEG_VALUE; |
| 13157 | issrc = (aux->alu_state & BPF_ALU_SANITIZE) == |
| 13158 | BPF_ALU_SANITIZE_SRC; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13159 | isimm = aux->alu_state & BPF_ALU_IMMEDIATE; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13160 | |
| 13161 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13162 | if (isimm) { |
| 13163 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 13164 | } else { |
| 13165 | if (isneg) |
| 13166 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 13167 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 13168 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 13169 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 13170 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |
| 13171 | *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63); |
| 13172 | *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX, off_reg); |
| 13173 | } |
Daniel Borkmann | b9b34dd | 2021-04-30 16:21:46 +0200 | [diff] [blame] | 13174 | if (!issrc) |
| 13175 | *patch++ = BPF_MOV64_REG(insn->dst_reg, insn->src_reg); |
| 13176 | insn->src_reg = BPF_REG_AX; |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13177 | if (isneg) |
| 13178 | insn->code = insn->code == code_add ? |
| 13179 | code_sub : code_add; |
| 13180 | *patch++ = *insn; |
Daniel Borkmann | 801c605 | 2021-04-29 15:19:37 +0000 | [diff] [blame] | 13181 | if (issrc && isneg && !isimm) |
Daniel Borkmann | 979d63d | 2019-01-03 00:58:34 +0100 | [diff] [blame] | 13182 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 13183 | cnt = patch - insn_buf; |
| 13184 | |
| 13185 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13186 | if (!new_prog) |
| 13187 | return -ENOMEM; |
| 13188 | |
| 13189 | delta += cnt - 1; |
| 13190 | env->prog = prog = new_prog; |
| 13191 | insn = new_prog->insnsi + i + delta; |
| 13192 | continue; |
| 13193 | } |
| 13194 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13195 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 13196 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 13197 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 13198 | continue; |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13199 | if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) { |
| 13200 | ret = fixup_kfunc_call(env, insn); |
| 13201 | if (ret) |
| 13202 | return ret; |
| 13203 | continue; |
| 13204 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13205 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13206 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 13207 | prog->dst_needed = 1; |
| 13208 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 13209 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 13210 | if (insn->imm == BPF_FUNC_override_return) |
| 13211 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13212 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 13213 | /* If we tail call into other programs, we |
| 13214 | * cannot make any assumptions since they can |
| 13215 | * be replaced dynamically during runtime in |
| 13216 | * the program array. |
| 13217 | */ |
| 13218 | prog->cb_access = 1; |
Maciej Fijalkowski | e411901 | 2020-09-16 23:10:09 +0200 | [diff] [blame] | 13219 | if (!allow_tail_call_in_subprogs(env)) |
| 13220 | prog->aux->stack_depth = MAX_BPF_STACK; |
| 13221 | prog->aux->max_pkt_offset = MAX_PACKET_OFF; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 13222 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13223 | /* mark bpf_tail_call as different opcode to avoid |
Zhen Lei | 8fb33b6 | 2021-05-25 10:56:59 +0800 | [diff] [blame] | 13224 | * conditional branch in the interpreter for every normal |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13225 | * call and to prevent accidental JITing by JIT compiler |
| 13226 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13227 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13228 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 13229 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13230 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13231 | aux = &env->insn_aux_data[i + delta]; |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 13232 | if (env->bpf_capable && !expect_blinding && |
Daniel Borkmann | cc52d91 | 2019-12-19 22:19:50 +0100 | [diff] [blame] | 13233 | prog->jit_requested && |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13234 | !bpf_map_key_poisoned(aux) && |
| 13235 | !bpf_map_ptr_poisoned(aux) && |
| 13236 | !bpf_map_ptr_unpriv(aux)) { |
| 13237 | struct bpf_jit_poke_descriptor desc = { |
| 13238 | .reason = BPF_POKE_REASON_TAIL_CALL, |
| 13239 | .tail_call.map = BPF_MAP_PTR(aux->map_ptr_state), |
| 13240 | .tail_call.key = bpf_map_key_immediate(aux), |
Maciej Fijalkowski | a748c69 | 2020-09-16 23:10:05 +0200 | [diff] [blame] | 13241 | .insn_idx = i + delta, |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13242 | }; |
| 13243 | |
| 13244 | ret = bpf_jit_add_poke_descriptor(prog, &desc); |
| 13245 | if (ret < 0) { |
| 13246 | verbose(env, "adding tail call poke descriptor failed\n"); |
| 13247 | return ret; |
| 13248 | } |
| 13249 | |
| 13250 | insn->imm = ret + 1; |
| 13251 | continue; |
| 13252 | } |
| 13253 | |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13254 | if (!bpf_map_ptr_unpriv(aux)) |
| 13255 | continue; |
| 13256 | |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13257 | /* instead of changing every JIT dealing with tail_call |
| 13258 | * emit two extra insns: |
| 13259 | * if (index >= max_entries) goto out; |
| 13260 | * index &= array->index_mask; |
| 13261 | * to avoid out-of-bounds cpu speculation |
| 13262 | */ |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13263 | if (bpf_map_ptr_poisoned(aux)) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 13264 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13265 | return -EINVAL; |
| 13266 | } |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13267 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13268 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 13269 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 13270 | map_ptr->max_entries, 2); |
| 13271 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 13272 | container_of(map_ptr, |
| 13273 | struct bpf_array, |
| 13274 | map)->index_mask); |
| 13275 | insn_buf[2] = *insn; |
| 13276 | cnt = 3; |
| 13277 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13278 | if (!new_prog) |
| 13279 | return -ENOMEM; |
| 13280 | |
| 13281 | delta += cnt - 1; |
| 13282 | env->prog = prog = new_prog; |
| 13283 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 13284 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13285 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 13286 | |
Alexei Starovoitov | b00628b | 2021-07-14 17:54:09 -0700 | [diff] [blame] | 13287 | if (insn->imm == BPF_FUNC_timer_set_callback) { |
| 13288 | /* The verifier will process callback_fn as many times as necessary |
| 13289 | * with different maps and the register states prepared by |
| 13290 | * set_timer_callback_state will be accurate. |
| 13291 | * |
| 13292 | * The following use case is valid: |
| 13293 | * map1 is shared by prog1, prog2, prog3. |
| 13294 | * prog1 calls bpf_timer_init for some map1 elements |
| 13295 | * prog2 calls bpf_timer_set_callback for some map1 elements. |
| 13296 | * Those that were not bpf_timer_init-ed will return -EINVAL. |
| 13297 | * prog3 calls bpf_timer_start for some map1 elements. |
| 13298 | * Those that were not both bpf_timer_init-ed and |
| 13299 | * bpf_timer_set_callback-ed will return -EINVAL. |
| 13300 | */ |
| 13301 | struct bpf_insn ld_addrs[2] = { |
| 13302 | BPF_LD_IMM64(BPF_REG_3, (long)prog->aux), |
| 13303 | }; |
| 13304 | |
| 13305 | insn_buf[0] = ld_addrs[0]; |
| 13306 | insn_buf[1] = ld_addrs[1]; |
| 13307 | insn_buf[2] = *insn; |
| 13308 | cnt = 3; |
| 13309 | |
| 13310 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13311 | if (!new_prog) |
| 13312 | return -ENOMEM; |
| 13313 | |
| 13314 | delta += cnt - 1; |
| 13315 | env->prog = prog = new_prog; |
| 13316 | insn = new_prog->insnsi + i + delta; |
| 13317 | goto patch_call_imm; |
| 13318 | } |
| 13319 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 13320 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13321 | * and other inlining handlers are currently limited to 64 bit |
| 13322 | * only. |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 13323 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 13324 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13325 | (insn->imm == BPF_FUNC_map_lookup_elem || |
| 13326 | insn->imm == BPF_FUNC_map_update_elem || |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13327 | insn->imm == BPF_FUNC_map_delete_elem || |
| 13328 | insn->imm == BPF_FUNC_map_push_elem || |
| 13329 | insn->imm == BPF_FUNC_map_pop_elem || |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13330 | insn->imm == BPF_FUNC_map_peek_elem || |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13331 | insn->imm == BPF_FUNC_redirect_map || |
| 13332 | insn->imm == BPF_FUNC_for_each_map_elem)) { |
Daniel Borkmann | c93552c | 2018-05-24 02:32:53 +0200 | [diff] [blame] | 13333 | aux = &env->insn_aux_data[i + delta]; |
| 13334 | if (bpf_map_ptr_poisoned(aux)) |
| 13335 | goto patch_call_imm; |
| 13336 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13337 | map_ptr = BPF_MAP_PTR(aux->map_ptr_state); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13338 | ops = map_ptr->ops; |
| 13339 | if (insn->imm == BPF_FUNC_map_lookup_elem && |
| 13340 | ops->map_gen_lookup) { |
| 13341 | cnt = ops->map_gen_lookup(map_ptr, insn_buf); |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 13342 | if (cnt == -EOPNOTSUPP) |
| 13343 | goto patch_map_ops_generic; |
| 13344 | if (cnt <= 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13345 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13346 | return -EINVAL; |
| 13347 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13348 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13349 | new_prog = bpf_patch_insn_data(env, i + delta, |
| 13350 | insn_buf, cnt); |
| 13351 | if (!new_prog) |
| 13352 | return -ENOMEM; |
| 13353 | |
| 13354 | delta += cnt - 1; |
| 13355 | env->prog = prog = new_prog; |
| 13356 | insn = new_prog->insnsi + i + delta; |
| 13357 | continue; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 13358 | } |
| 13359 | |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13360 | BUILD_BUG_ON(!__same_type(ops->map_lookup_elem, |
| 13361 | (void *(*)(struct bpf_map *map, void *key))NULL)); |
| 13362 | BUILD_BUG_ON(!__same_type(ops->map_delete_elem, |
| 13363 | (int (*)(struct bpf_map *map, void *key))NULL)); |
| 13364 | BUILD_BUG_ON(!__same_type(ops->map_update_elem, |
| 13365 | (int (*)(struct bpf_map *map, void *key, void *value, |
| 13366 | u64 flags))NULL)); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13367 | BUILD_BUG_ON(!__same_type(ops->map_push_elem, |
| 13368 | (int (*)(struct bpf_map *map, void *value, |
| 13369 | u64 flags))NULL)); |
| 13370 | BUILD_BUG_ON(!__same_type(ops->map_pop_elem, |
| 13371 | (int (*)(struct bpf_map *map, void *value))NULL)); |
| 13372 | BUILD_BUG_ON(!__same_type(ops->map_peek_elem, |
| 13373 | (int (*)(struct bpf_map *map, void *value))NULL)); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13374 | BUILD_BUG_ON(!__same_type(ops->map_redirect, |
| 13375 | (int (*)(struct bpf_map *map, u32 ifindex, u64 flags))NULL)); |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13376 | BUILD_BUG_ON(!__same_type(ops->map_for_each_callback, |
| 13377 | (int (*)(struct bpf_map *map, |
| 13378 | bpf_callback_t callback_fn, |
| 13379 | void *callback_ctx, |
| 13380 | u64 flags))NULL)); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13381 | |
Daniel Borkmann | 4a8f87e | 2020-10-11 01:40:03 +0200 | [diff] [blame] | 13382 | patch_map_ops_generic: |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13383 | switch (insn->imm) { |
| 13384 | case BPF_FUNC_map_lookup_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13385 | insn->imm = BPF_CALL_IMM(ops->map_lookup_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13386 | continue; |
| 13387 | case BPF_FUNC_map_update_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13388 | insn->imm = BPF_CALL_IMM(ops->map_update_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13389 | continue; |
| 13390 | case BPF_FUNC_map_delete_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13391 | insn->imm = BPF_CALL_IMM(ops->map_delete_elem); |
Daniel Borkmann | 09772d9 | 2018-06-02 23:06:35 +0200 | [diff] [blame] | 13392 | continue; |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13393 | case BPF_FUNC_map_push_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13394 | insn->imm = BPF_CALL_IMM(ops->map_push_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13395 | continue; |
| 13396 | case BPF_FUNC_map_pop_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13397 | insn->imm = BPF_CALL_IMM(ops->map_pop_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13398 | continue; |
| 13399 | case BPF_FUNC_map_peek_elem: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13400 | insn->imm = BPF_CALL_IMM(ops->map_peek_elem); |
Daniel Borkmann | 84430d4 | 2018-10-21 02:09:27 +0200 | [diff] [blame] | 13401 | continue; |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13402 | case BPF_FUNC_redirect_map: |
Kees Cook | 3d717fa | 2021-09-28 16:09:45 -0700 | [diff] [blame] | 13403 | insn->imm = BPF_CALL_IMM(ops->map_redirect); |
Björn Töpel | e6a4750 | 2021-03-08 12:29:06 +0100 | [diff] [blame] | 13404 | continue; |
Andrey Ignatov | 0640c77 | 2021-10-05 17:18:38 -0700 | [diff] [blame] | 13405 | case BPF_FUNC_for_each_map_elem: |
| 13406 | insn->imm = BPF_CALL_IMM(ops->map_for_each_callback); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13407 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13408 | } |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 13409 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13410 | goto patch_call_imm; |
| 13411 | } |
| 13412 | |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 13413 | /* Implement bpf_jiffies64 inline. */ |
Martin KaFai Lau | 5576b99 | 2020-01-22 15:36:46 -0800 | [diff] [blame] | 13414 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
| 13415 | insn->imm == BPF_FUNC_jiffies64) { |
| 13416 | struct bpf_insn ld_jiffies_addr[2] = { |
| 13417 | BPF_LD_IMM64(BPF_REG_0, |
| 13418 | (unsigned long)&jiffies), |
| 13419 | }; |
| 13420 | |
| 13421 | insn_buf[0] = ld_jiffies_addr[0]; |
| 13422 | insn_buf[1] = ld_jiffies_addr[1]; |
| 13423 | insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, |
| 13424 | BPF_REG_0, 0); |
| 13425 | cnt = 3; |
| 13426 | |
| 13427 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 13428 | cnt); |
| 13429 | if (!new_prog) |
| 13430 | return -ENOMEM; |
| 13431 | |
| 13432 | delta += cnt - 1; |
| 13433 | env->prog = prog = new_prog; |
| 13434 | insn = new_prog->insnsi + i + delta; |
| 13435 | continue; |
| 13436 | } |
| 13437 | |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13438 | /* Implement bpf_get_func_arg inline. */ |
| 13439 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13440 | insn->imm == BPF_FUNC_get_func_arg) { |
| 13441 | /* Load nr_args from ctx - 8 */ |
| 13442 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13443 | insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6); |
| 13444 | insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3); |
| 13445 | insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1); |
| 13446 | insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0); |
| 13447 | insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); |
| 13448 | insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0); |
| 13449 | insn_buf[7] = BPF_JMP_A(1); |
| 13450 | insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL); |
| 13451 | cnt = 9; |
| 13452 | |
| 13453 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13454 | if (!new_prog) |
| 13455 | return -ENOMEM; |
| 13456 | |
| 13457 | delta += cnt - 1; |
| 13458 | env->prog = prog = new_prog; |
| 13459 | insn = new_prog->insnsi + i + delta; |
| 13460 | continue; |
| 13461 | } |
| 13462 | |
| 13463 | /* Implement bpf_get_func_ret inline. */ |
| 13464 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13465 | insn->imm == BPF_FUNC_get_func_ret) { |
| 13466 | if (eatype == BPF_TRACE_FEXIT || |
| 13467 | eatype == BPF_MODIFY_RETURN) { |
| 13468 | /* Load nr_args from ctx - 8 */ |
| 13469 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13470 | insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3); |
| 13471 | insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1); |
| 13472 | insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0); |
| 13473 | insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0); |
| 13474 | insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0); |
| 13475 | cnt = 6; |
| 13476 | } else { |
| 13477 | insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP); |
| 13478 | cnt = 1; |
| 13479 | } |
| 13480 | |
| 13481 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 13482 | if (!new_prog) |
| 13483 | return -ENOMEM; |
| 13484 | |
| 13485 | delta += cnt - 1; |
| 13486 | env->prog = prog = new_prog; |
| 13487 | insn = new_prog->insnsi + i + delta; |
| 13488 | continue; |
| 13489 | } |
| 13490 | |
| 13491 | /* Implement get_func_arg_cnt inline. */ |
| 13492 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13493 | insn->imm == BPF_FUNC_get_func_arg_cnt) { |
| 13494 | /* Load nr_args from ctx - 8 */ |
| 13495 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8); |
| 13496 | |
| 13497 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); |
| 13498 | if (!new_prog) |
| 13499 | return -ENOMEM; |
| 13500 | |
| 13501 | env->prog = prog = new_prog; |
| 13502 | insn = new_prog->insnsi + i + delta; |
| 13503 | continue; |
| 13504 | } |
| 13505 | |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13506 | /* Implement bpf_get_func_ip inline. */ |
| 13507 | if (prog_type == BPF_PROG_TYPE_TRACING && |
| 13508 | insn->imm == BPF_FUNC_get_func_ip) { |
Jiri Olsa | f92c1e1 | 2021-12-08 20:32:44 +0100 | [diff] [blame] | 13509 | /* Load IP address from ctx - 16 */ |
| 13510 | insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16); |
Jiri Olsa | 9b99edc | 2021-07-14 11:43:55 +0200 | [diff] [blame] | 13511 | |
| 13512 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1); |
| 13513 | if (!new_prog) |
| 13514 | return -ENOMEM; |
| 13515 | |
| 13516 | env->prog = prog = new_prog; |
| 13517 | insn = new_prog->insnsi + i + delta; |
| 13518 | continue; |
| 13519 | } |
| 13520 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 13521 | patch_call_imm: |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13522 | fn = env->ops->get_func_proto(insn->imm, env->prog); |
| 13523 | /* all functions that have prototype and verifier allowed |
| 13524 | * programs to call them, must be real in-kernel functions |
| 13525 | */ |
| 13526 | if (!fn->func) { |
| 13527 | verbose(env, |
| 13528 | "kernel subsystem misconfigured func %s#%d\n", |
| 13529 | func_id_name(insn->imm), insn->imm); |
| 13530 | return -EFAULT; |
| 13531 | } |
| 13532 | insn->imm = fn->func - __bpf_call_base; |
| 13533 | } |
| 13534 | |
Daniel Borkmann | d2e4c1e | 2019-11-22 21:07:59 +0100 | [diff] [blame] | 13535 | /* Since poke tab is now finalized, publish aux to tracker. */ |
| 13536 | for (i = 0; i < prog->aux->size_poke_tab; i++) { |
| 13537 | map_ptr = prog->aux->poke_tab[i].tail_call.map; |
| 13538 | if (!map_ptr->ops->map_poke_track || |
| 13539 | !map_ptr->ops->map_poke_untrack || |
| 13540 | !map_ptr->ops->map_poke_run) { |
| 13541 | verbose(env, "bpf verifier is misconfigured\n"); |
| 13542 | return -EINVAL; |
| 13543 | } |
| 13544 | |
| 13545 | ret = map_ptr->ops->map_poke_track(map_ptr, prog->aux); |
| 13546 | if (ret < 0) { |
| 13547 | verbose(env, "tracking tail call prog failed\n"); |
| 13548 | return ret; |
| 13549 | } |
| 13550 | } |
| 13551 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 13552 | sort_kfunc_descs_by_imm(env->prog); |
| 13553 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13554 | return 0; |
| 13555 | } |
| 13556 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13557 | static void free_states(struct bpf_verifier_env *env) |
| 13558 | { |
| 13559 | struct bpf_verifier_state_list *sl, *sln; |
| 13560 | int i; |
| 13561 | |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 13562 | sl = env->free_list; |
| 13563 | while (sl) { |
| 13564 | sln = sl->next; |
| 13565 | free_verifier_state(&sl->state, false); |
| 13566 | kfree(sl); |
| 13567 | sl = sln; |
| 13568 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13569 | env->free_list = NULL; |
Alexei Starovoitov | 9f4686c | 2019-04-01 21:27:41 -0700 | [diff] [blame] | 13570 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13571 | if (!env->explored_states) |
| 13572 | return; |
| 13573 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 13574 | for (i = 0; i < state_htab_size(env); i++) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13575 | sl = env->explored_states[i]; |
| 13576 | |
Alexei Starovoitov | a8f500a | 2019-05-21 20:17:06 -0700 | [diff] [blame] | 13577 | while (sl) { |
| 13578 | sln = sl->next; |
| 13579 | free_verifier_state(&sl->state, false); |
| 13580 | kfree(sl); |
| 13581 | sl = sln; |
| 13582 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13583 | env->explored_states[i] = NULL; |
| 13584 | } |
| 13585 | } |
| 13586 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13587 | static int do_check_common(struct bpf_verifier_env *env, int subprog) |
| 13588 | { |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 13589 | bool pop_log = !(env->log.level & BPF_LOG_LEVEL2); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13590 | struct bpf_verifier_state *state; |
| 13591 | struct bpf_reg_state *regs; |
| 13592 | int ret, i; |
| 13593 | |
| 13594 | env->prev_linfo = NULL; |
| 13595 | env->pass_cnt++; |
| 13596 | |
| 13597 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 13598 | if (!state) |
| 13599 | return -ENOMEM; |
| 13600 | state->curframe = 0; |
| 13601 | state->speculative = false; |
| 13602 | state->branches = 1; |
| 13603 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 13604 | if (!state->frame[0]) { |
| 13605 | kfree(state); |
| 13606 | return -ENOMEM; |
| 13607 | } |
| 13608 | env->cur_state = state; |
| 13609 | init_func_state(env, state->frame[0], |
| 13610 | BPF_MAIN_FUNC /* callsite */, |
| 13611 | 0 /* frameno */, |
| 13612 | subprog); |
| 13613 | |
| 13614 | regs = state->frame[state->curframe]->regs; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13615 | if (subprog || env->prog->type == BPF_PROG_TYPE_EXT) { |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13616 | ret = btf_prepare_func_args(env, subprog, regs); |
| 13617 | if (ret) |
| 13618 | goto out; |
| 13619 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) { |
| 13620 | if (regs[i].type == PTR_TO_CTX) |
| 13621 | mark_reg_known_zero(env, regs, i); |
| 13622 | else if (regs[i].type == SCALAR_VALUE) |
| 13623 | mark_reg_unknown(env, regs, i); |
Dmitrii Banshchikov | e5069b9c | 2021-02-13 00:56:41 +0400 | [diff] [blame] | 13624 | else if (regs[i].type == PTR_TO_MEM_OR_NULL) { |
| 13625 | const u32 mem_size = regs[i].mem_size; |
| 13626 | |
| 13627 | mark_reg_known_zero(env, regs, i); |
| 13628 | regs[i].mem_size = mem_size; |
| 13629 | regs[i].id = ++env->id_gen; |
| 13630 | } |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13631 | } |
| 13632 | } else { |
| 13633 | /* 1st arg to a function */ |
| 13634 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 13635 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Martin KaFai Lau | 34747c4 | 2021-03-24 18:51:36 -0700 | [diff] [blame] | 13636 | ret = btf_check_subprog_arg_match(env, subprog, regs); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13637 | if (ret == -EFAULT) |
| 13638 | /* unlikely verifier bug. abort. |
| 13639 | * ret == 0 and ret < 0 are sadly acceptable for |
| 13640 | * main() function due to backward compatibility. |
| 13641 | * Like socket filter program may be written as: |
| 13642 | * int bpf_prog(struct pt_regs *ctx) |
| 13643 | * and never dereference that ctx in the program. |
| 13644 | * 'struct pt_regs' is a type mismatch for socket |
| 13645 | * filter that should be using 'struct __sk_buff'. |
| 13646 | */ |
| 13647 | goto out; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13648 | } |
| 13649 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13650 | ret = do_check(env); |
| 13651 | out: |
Alexei Starovoitov | f59bbfc | 2020-01-21 18:41:38 -0800 | [diff] [blame] | 13652 | /* check for NULL is necessary, since cur_state can be freed inside |
| 13653 | * do_check() under memory pressure. |
| 13654 | */ |
| 13655 | if (env->cur_state) { |
| 13656 | free_verifier_state(env->cur_state, true); |
| 13657 | env->cur_state = NULL; |
| 13658 | } |
Andrii Nakryiko | 6f8a57c | 2020-04-23 12:58:50 -0700 | [diff] [blame] | 13659 | while (!pop_stack(env, NULL, NULL, false)); |
| 13660 | if (!ret && pop_log) |
| 13661 | bpf_vlog_reset(&env->log, 0); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13662 | free_states(env); |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13663 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 13664 | } |
| 13665 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 13666 | /* Verify all global functions in a BPF program one by one based on their BTF. |
| 13667 | * All global functions must pass verification. Otherwise the whole program is rejected. |
| 13668 | * Consider: |
| 13669 | * int bar(int); |
| 13670 | * int foo(int f) |
| 13671 | * { |
| 13672 | * return bar(f); |
| 13673 | * } |
| 13674 | * int bar(int b) |
| 13675 | * { |
| 13676 | * ... |
| 13677 | * } |
| 13678 | * foo() will be verified first for R1=any_scalar_value. During verification it |
| 13679 | * will be assumed that bar() already verified successfully and call to bar() |
| 13680 | * from foo() will be checked for type match only. Later bar() will be verified |
| 13681 | * independently to check that it's safe for R1=any_scalar_value. |
| 13682 | */ |
| 13683 | static int do_check_subprogs(struct bpf_verifier_env *env) |
| 13684 | { |
| 13685 | struct bpf_prog_aux *aux = env->prog->aux; |
| 13686 | int i, ret; |
| 13687 | |
| 13688 | if (!aux->func_info) |
| 13689 | return 0; |
| 13690 | |
| 13691 | for (i = 1; i < env->subprog_cnt; i++) { |
| 13692 | if (aux->func_info_aux[i].linkage != BTF_FUNC_GLOBAL) |
| 13693 | continue; |
| 13694 | env->insn_idx = env->subprog_info[i].start; |
| 13695 | WARN_ON_ONCE(env->insn_idx == 0); |
| 13696 | ret = do_check_common(env, i); |
| 13697 | if (ret) { |
| 13698 | return ret; |
| 13699 | } else if (env->log.level & BPF_LOG_LEVEL) { |
| 13700 | verbose(env, |
| 13701 | "Func#%d is safe for any args that match its prototype\n", |
| 13702 | i); |
| 13703 | } |
| 13704 | } |
| 13705 | return 0; |
| 13706 | } |
| 13707 | |
| 13708 | static int do_check_main(struct bpf_verifier_env *env) |
| 13709 | { |
| 13710 | int ret; |
| 13711 | |
| 13712 | env->insn_idx = 0; |
| 13713 | ret = do_check_common(env, 0); |
| 13714 | if (!ret) |
| 13715 | env->prog->aux->stack_depth = env->subprog_info[0].stack_depth; |
| 13716 | return ret; |
| 13717 | } |
| 13718 | |
| 13719 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 13720 | static void print_verification_stats(struct bpf_verifier_env *env) |
| 13721 | { |
| 13722 | int i; |
| 13723 | |
| 13724 | if (env->log.level & BPF_LOG_STATS) { |
| 13725 | verbose(env, "verification time %lld usec\n", |
| 13726 | div_u64(env->verification_time, 1000)); |
| 13727 | verbose(env, "stack depth "); |
| 13728 | for (i = 0; i < env->subprog_cnt; i++) { |
| 13729 | u32 depth = env->subprog_info[i].stack_depth; |
| 13730 | |
| 13731 | verbose(env, "%d", depth); |
| 13732 | if (i + 1 < env->subprog_cnt) |
| 13733 | verbose(env, "+"); |
| 13734 | } |
| 13735 | verbose(env, "\n"); |
| 13736 | } |
| 13737 | verbose(env, "processed %d insns (limit %d) max_states_per_insn %d " |
| 13738 | "total_states %d peak_states %d mark_read %d\n", |
| 13739 | env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS, |
| 13740 | env->max_states_per_insn, env->total_states, |
| 13741 | env->peak_states, env->longest_mark_read_walk); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 13742 | } |
| 13743 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13744 | static int check_struct_ops_btf_id(struct bpf_verifier_env *env) |
| 13745 | { |
| 13746 | const struct btf_type *t, *func_proto; |
| 13747 | const struct bpf_struct_ops *st_ops; |
| 13748 | const struct btf_member *member; |
| 13749 | struct bpf_prog *prog = env->prog; |
| 13750 | u32 btf_id, member_idx; |
| 13751 | const char *mname; |
| 13752 | |
Toke Høiland-Jørgensen | 12aa8a9 | 2021-03-26 11:03:13 +0100 | [diff] [blame] | 13753 | if (!prog->gpl_compatible) { |
| 13754 | verbose(env, "struct ops programs must have a GPL compatible license\n"); |
| 13755 | return -EINVAL; |
| 13756 | } |
| 13757 | |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13758 | btf_id = prog->aux->attach_btf_id; |
| 13759 | st_ops = bpf_struct_ops_find(btf_id); |
| 13760 | if (!st_ops) { |
| 13761 | verbose(env, "attach_btf_id %u is not a supported struct\n", |
| 13762 | btf_id); |
| 13763 | return -ENOTSUPP; |
| 13764 | } |
| 13765 | |
| 13766 | t = st_ops->type; |
| 13767 | member_idx = prog->expected_attach_type; |
| 13768 | if (member_idx >= btf_type_vlen(t)) { |
| 13769 | verbose(env, "attach to invalid member idx %u of struct %s\n", |
| 13770 | member_idx, st_ops->name); |
| 13771 | return -EINVAL; |
| 13772 | } |
| 13773 | |
| 13774 | member = &btf_type_member(t)[member_idx]; |
| 13775 | mname = btf_name_by_offset(btf_vmlinux, member->name_off); |
| 13776 | func_proto = btf_type_resolve_func_ptr(btf_vmlinux, member->type, |
| 13777 | NULL); |
| 13778 | if (!func_proto) { |
| 13779 | verbose(env, "attach to invalid member %s(@idx %u) of struct %s\n", |
| 13780 | mname, member_idx, st_ops->name); |
| 13781 | return -EINVAL; |
| 13782 | } |
| 13783 | |
| 13784 | if (st_ops->check_member) { |
| 13785 | int err = st_ops->check_member(t, member); |
| 13786 | |
| 13787 | if (err) { |
| 13788 | verbose(env, "attach to unsupported member %s of struct %s\n", |
| 13789 | mname, st_ops->name); |
| 13790 | return err; |
| 13791 | } |
| 13792 | } |
| 13793 | |
| 13794 | prog->aux->attach_func_proto = func_proto; |
| 13795 | prog->aux->attach_func_name = mname; |
| 13796 | env->ops = st_ops->verifier_ops; |
| 13797 | |
| 13798 | return 0; |
| 13799 | } |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13800 | #define SECURITY_PREFIX "security_" |
| 13801 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13802 | static int check_attach_modify_return(unsigned long addr, const char *func_name) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13803 | { |
KP Singh | 6919175 | 2020-03-05 21:49:55 +0100 | [diff] [blame] | 13804 | if (within_error_injection_list(addr) || |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13805 | !strncmp(SECURITY_PREFIX, func_name, sizeof(SECURITY_PREFIX) - 1)) |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13806 | return 0; |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13807 | |
KP Singh | 6ba43b7 | 2020-03-04 20:18:50 +0100 | [diff] [blame] | 13808 | return -EINVAL; |
| 13809 | } |
Martin KaFai Lau | 27ae7997 | 2020-01-08 16:35:03 -0800 | [diff] [blame] | 13810 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 13811 | /* list of non-sleepable functions that are otherwise on |
| 13812 | * ALLOW_ERROR_INJECTION list |
| 13813 | */ |
| 13814 | BTF_SET_START(btf_non_sleepable_error_inject) |
| 13815 | /* Three functions below can be called from sleepable and non-sleepable context. |
| 13816 | * Assume non-sleepable from bpf safety point of view. |
| 13817 | */ |
Matthew Wilcox (Oracle) | 9dd3d06 | 2020-12-08 08:56:28 -0500 | [diff] [blame] | 13818 | BTF_ID(func, __filemap_add_folio) |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 13819 | BTF_ID(func, should_fail_alloc_page) |
| 13820 | BTF_ID(func, should_failslab) |
| 13821 | BTF_SET_END(btf_non_sleepable_error_inject) |
| 13822 | |
| 13823 | static int check_non_sleepable_error_inject(u32 btf_id) |
| 13824 | { |
| 13825 | return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); |
| 13826 | } |
| 13827 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13828 | int bpf_check_attach_target(struct bpf_verifier_log *log, |
| 13829 | const struct bpf_prog *prog, |
| 13830 | const struct bpf_prog *tgt_prog, |
| 13831 | u32 btf_id, |
| 13832 | struct bpf_attach_target_info *tgt_info) |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13833 | { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13834 | bool prog_extension = prog->type == BPF_PROG_TYPE_EXT; |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13835 | const char prefix[] = "btf_trace_"; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13836 | int ret = 0, subprog = -1, i; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13837 | const struct btf_type *t; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13838 | bool conservative = true; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13839 | const char *tname; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13840 | struct btf *btf; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13841 | long addr = 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13842 | |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13843 | if (!btf_id) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13844 | bpf_log(log, "Tracing programs must provide btf_id\n"); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13845 | return -EINVAL; |
| 13846 | } |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 13847 | btf = tgt_prog ? tgt_prog->aux->btf : prog->aux->attach_btf; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13848 | if (!btf) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13849 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13850 | "FENTRY/FEXIT program can only be attached to another program annotated with BTF\n"); |
| 13851 | return -EINVAL; |
| 13852 | } |
| 13853 | t = btf_type_by_id(btf, btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13854 | if (!t) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13855 | bpf_log(log, "attach_btf_id %u is invalid\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13856 | return -EINVAL; |
| 13857 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13858 | tname = btf_name_by_offset(btf, t->name_off); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13859 | if (!tname) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13860 | bpf_log(log, "attach_btf_id %u doesn't have a name\n", btf_id); |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13861 | return -EINVAL; |
| 13862 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13863 | if (tgt_prog) { |
| 13864 | struct bpf_prog_aux *aux = tgt_prog->aux; |
| 13865 | |
| 13866 | for (i = 0; i < aux->func_info_cnt; i++) |
| 13867 | if (aux->func_info[i].type_id == btf_id) { |
| 13868 | subprog = i; |
| 13869 | break; |
| 13870 | } |
| 13871 | if (subprog == -1) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13872 | bpf_log(log, "Subprog %s doesn't exist\n", tname); |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13873 | return -EINVAL; |
| 13874 | } |
| 13875 | conservative = aux->func_info_aux[subprog].unreliable; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13876 | if (prog_extension) { |
| 13877 | if (conservative) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13878 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13879 | "Cannot replace static functions\n"); |
| 13880 | return -EINVAL; |
| 13881 | } |
| 13882 | if (!prog->jit_requested) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13883 | bpf_log(log, |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13884 | "Extension programs should be JITed\n"); |
| 13885 | return -EINVAL; |
| 13886 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13887 | } |
| 13888 | if (!tgt_prog->jited) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13889 | bpf_log(log, "Can attach to only JITed progs\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13890 | return -EINVAL; |
| 13891 | } |
| 13892 | if (tgt_prog->type == prog->type) { |
| 13893 | /* Cannot fentry/fexit another fentry/fexit program. |
| 13894 | * Cannot attach program extension to another extension. |
| 13895 | * It's ok to attach fentry/fexit to extension program. |
| 13896 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13897 | bpf_log(log, "Cannot recursively attach\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13898 | return -EINVAL; |
| 13899 | } |
| 13900 | if (tgt_prog->type == BPF_PROG_TYPE_TRACING && |
| 13901 | prog_extension && |
| 13902 | (tgt_prog->expected_attach_type == BPF_TRACE_FENTRY || |
| 13903 | tgt_prog->expected_attach_type == BPF_TRACE_FEXIT)) { |
| 13904 | /* Program extensions can extend all program types |
| 13905 | * except fentry/fexit. The reason is the following. |
| 13906 | * The fentry/fexit programs are used for performance |
| 13907 | * analysis, stats and can be attached to any program |
| 13908 | * type except themselves. When extension program is |
| 13909 | * replacing XDP function it is necessary to allow |
| 13910 | * performance analysis of all functions. Both original |
| 13911 | * XDP program and its program extension. Hence |
| 13912 | * attaching fentry/fexit to BPF_PROG_TYPE_EXT is |
| 13913 | * allowed. If extending of fentry/fexit was allowed it |
| 13914 | * would be possible to create long call chain |
| 13915 | * fentry->extension->fentry->extension beyond |
| 13916 | * reasonable stack size. Hence extending fentry is not |
| 13917 | * allowed. |
| 13918 | */ |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13919 | bpf_log(log, "Cannot extend fentry/fexit\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13920 | return -EINVAL; |
| 13921 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13922 | } else { |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13923 | if (prog_extension) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13924 | bpf_log(log, "Cannot replace kernel functions\n"); |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13925 | return -EINVAL; |
| 13926 | } |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13927 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13928 | |
| 13929 | switch (prog->expected_attach_type) { |
| 13930 | case BPF_TRACE_RAW_TP: |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13931 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13932 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13933 | "Only FENTRY/FEXIT progs are attachable to another BPF prog\n"); |
| 13934 | return -EINVAL; |
| 13935 | } |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13936 | if (!btf_type_is_typedef(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13937 | bpf_log(log, "attach_btf_id %u is not a typedef\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13938 | btf_id); |
| 13939 | return -EINVAL; |
| 13940 | } |
Alexei Starovoitov | f1b9509 | 2019-10-30 15:32:11 -0700 | [diff] [blame] | 13941 | if (strncmp(prefix, tname, sizeof(prefix) - 1)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13942 | bpf_log(log, "attach_btf_id %u points to wrong type name %s\n", |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13943 | btf_id, tname); |
| 13944 | return -EINVAL; |
| 13945 | } |
| 13946 | tname += sizeof(prefix) - 1; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13947 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13948 | if (!btf_type_is_ptr(t)) |
| 13949 | /* should never happen in valid vmlinux build */ |
| 13950 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13951 | t = btf_type_by_id(btf, t->type); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 13952 | if (!btf_type_is_func_proto(t)) |
| 13953 | /* should never happen in valid vmlinux build */ |
| 13954 | return -EINVAL; |
| 13955 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13956 | break; |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 13957 | case BPF_TRACE_ITER: |
| 13958 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13959 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Yonghong Song | 15d83c4 | 2020-05-09 10:59:00 -0700 | [diff] [blame] | 13960 | btf_id); |
| 13961 | return -EINVAL; |
| 13962 | } |
| 13963 | t = btf_type_by_id(btf, t->type); |
| 13964 | if (!btf_type_is_func_proto(t)) |
| 13965 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13966 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
| 13967 | if (ret) |
| 13968 | return ret; |
| 13969 | break; |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13970 | default: |
| 13971 | if (!prog_extension) |
| 13972 | return -EINVAL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 13973 | fallthrough; |
KP Singh | ae24082 | 2020-03-04 20:18:49 +0100 | [diff] [blame] | 13974 | case BPF_MODIFY_RETURN: |
KP Singh | 9e4e01d | 2020-03-29 01:43:52 +0100 | [diff] [blame] | 13975 | case BPF_LSM_MAC: |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13976 | case BPF_TRACE_FENTRY: |
| 13977 | case BPF_TRACE_FEXIT: |
| 13978 | if (!btf_type_is_func(t)) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13979 | bpf_log(log, "attach_btf_id %u is not a function\n", |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13980 | btf_id); |
| 13981 | return -EINVAL; |
| 13982 | } |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13983 | if (prog_extension && |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 13984 | btf_check_type_match(log, prog, btf, t)) |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 13985 | return -EINVAL; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13986 | t = btf_type_by_id(btf, t->type); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13987 | if (!btf_type_is_func_proto(t)) |
| 13988 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13989 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 13990 | if ((prog->aux->saved_dst_prog_type || prog->aux->saved_dst_attach_type) && |
| 13991 | (!tgt_prog || prog->aux->saved_dst_prog_type != tgt_prog->type || |
| 13992 | prog->aux->saved_dst_attach_type != tgt_prog->expected_attach_type)) |
| 13993 | return -EINVAL; |
| 13994 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13995 | if (tgt_prog && conservative) |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 13996 | t = NULL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 13997 | |
| 13998 | ret = btf_distill_func_proto(log, btf, t, tname, &tgt_info->fmodel); |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 13999 | if (ret < 0) |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14000 | return ret; |
| 14001 | |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14002 | if (tgt_prog) { |
Yonghong Song | e9eeec5 | 2019-12-04 17:06:06 -0800 | [diff] [blame] | 14003 | if (subprog == 0) |
| 14004 | addr = (long) tgt_prog->bpf_func; |
| 14005 | else |
| 14006 | addr = (long) tgt_prog->aux->func[subprog]->bpf_func; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14007 | } else { |
| 14008 | addr = kallsyms_lookup_name(tname); |
| 14009 | if (!addr) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 14010 | bpf_log(log, |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14011 | "The address of function %s cannot be found\n", |
| 14012 | tname); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14013 | return -ENOENT; |
Alexei Starovoitov | 5b92a28 | 2019-11-14 10:57:17 -0800 | [diff] [blame] | 14014 | } |
Alexei Starovoitov | fec56f5 | 2019-11-14 10:57:04 -0800 | [diff] [blame] | 14015 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 14016 | |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14017 | if (prog->aux->sleepable) { |
| 14018 | ret = -EINVAL; |
| 14019 | switch (prog->type) { |
| 14020 | case BPF_PROG_TYPE_TRACING: |
| 14021 | /* fentry/fexit/fmod_ret progs can be sleepable only if they are |
| 14022 | * attached to ALLOW_ERROR_INJECTION and are not in denylist. |
| 14023 | */ |
| 14024 | if (!check_non_sleepable_error_inject(btf_id) && |
| 14025 | within_error_injection_list(addr)) |
| 14026 | ret = 0; |
| 14027 | break; |
| 14028 | case BPF_PROG_TYPE_LSM: |
| 14029 | /* LSM progs check that they are attached to bpf_lsm_*() funcs. |
| 14030 | * Only some of them are sleepable. |
| 14031 | */ |
KP Singh | 423f161 | 2020-11-13 00:59:29 +0000 | [diff] [blame] | 14032 | if (bpf_lsm_is_sleepable_hook(btf_id)) |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14033 | ret = 0; |
| 14034 | break; |
| 14035 | default: |
| 14036 | break; |
| 14037 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14038 | if (ret) { |
| 14039 | bpf_log(log, "%s is not sleepable\n", tname); |
| 14040 | return ret; |
| 14041 | } |
Alexei Starovoitov | 1e6c62a | 2020-08-27 15:01:11 -0700 | [diff] [blame] | 14042 | } else if (prog->expected_attach_type == BPF_MODIFY_RETURN) { |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 14043 | if (tgt_prog) { |
Toke Høiland-Jørgensen | efc6815 | 2020-09-25 23:25:01 +0200 | [diff] [blame] | 14044 | bpf_log(log, "can't modify return codes of BPF programs\n"); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14045 | return -EINVAL; |
Toke Høiland-Jørgensen | 1af9270 | 2020-09-25 23:25:00 +0200 | [diff] [blame] | 14046 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14047 | ret = check_attach_modify_return(addr, tname); |
| 14048 | if (ret) { |
| 14049 | bpf_log(log, "%s() is not modifiable\n", tname); |
| 14050 | return ret; |
| 14051 | } |
Alexei Starovoitov | 18644ce | 2020-05-28 21:38:36 -0700 | [diff] [blame] | 14052 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14053 | |
| 14054 | break; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14055 | } |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14056 | tgt_info->tgt_addr = addr; |
| 14057 | tgt_info->tgt_name = tname; |
| 14058 | tgt_info->tgt_type = t; |
| 14059 | return 0; |
| 14060 | } |
| 14061 | |
Jiri Olsa | 35e3815 | 2021-04-29 13:47:12 +0200 | [diff] [blame] | 14062 | BTF_SET_START(btf_id_deny) |
| 14063 | BTF_ID_UNUSED |
| 14064 | #ifdef CONFIG_SMP |
| 14065 | BTF_ID(func, migrate_disable) |
| 14066 | BTF_ID(func, migrate_enable) |
| 14067 | #endif |
| 14068 | #if !defined CONFIG_PREEMPT_RCU && !defined CONFIG_TINY_RCU |
| 14069 | BTF_ID(func, rcu_read_unlock_strict) |
| 14070 | #endif |
| 14071 | BTF_SET_END(btf_id_deny) |
| 14072 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14073 | static int check_attach_btf_id(struct bpf_verifier_env *env) |
| 14074 | { |
| 14075 | struct bpf_prog *prog = env->prog; |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14076 | struct bpf_prog *tgt_prog = prog->aux->dst_prog; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14077 | struct bpf_attach_target_info tgt_info = {}; |
| 14078 | u32 btf_id = prog->aux->attach_btf_id; |
| 14079 | struct bpf_trampoline *tr; |
| 14080 | int ret; |
| 14081 | u64 key; |
| 14082 | |
Alexei Starovoitov | 79a7f8b | 2021-05-13 17:36:03 -0700 | [diff] [blame] | 14083 | if (prog->type == BPF_PROG_TYPE_SYSCALL) { |
| 14084 | if (prog->aux->sleepable) |
| 14085 | /* attach_btf_id checked to be zero already */ |
| 14086 | return 0; |
| 14087 | verbose(env, "Syscall programs can only be sleepable\n"); |
| 14088 | return -EINVAL; |
| 14089 | } |
| 14090 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14091 | if (prog->aux->sleepable && prog->type != BPF_PROG_TYPE_TRACING && |
| 14092 | prog->type != BPF_PROG_TYPE_LSM) { |
| 14093 | verbose(env, "Only fentry/fexit/fmod_ret and lsm programs can be sleepable\n"); |
| 14094 | return -EINVAL; |
| 14095 | } |
| 14096 | |
| 14097 | if (prog->type == BPF_PROG_TYPE_STRUCT_OPS) |
| 14098 | return check_struct_ops_btf_id(env); |
| 14099 | |
| 14100 | if (prog->type != BPF_PROG_TYPE_TRACING && |
| 14101 | prog->type != BPF_PROG_TYPE_LSM && |
| 14102 | prog->type != BPF_PROG_TYPE_EXT) |
| 14103 | return 0; |
| 14104 | |
| 14105 | ret = bpf_check_attach_target(&env->log, prog, tgt_prog, btf_id, &tgt_info); |
| 14106 | if (ret) |
| 14107 | return ret; |
| 14108 | |
| 14109 | if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) { |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14110 | /* to make freplace equivalent to their targets, they need to |
| 14111 | * inherit env->ops and expected_attach_type for the rest of the |
| 14112 | * verification |
| 14113 | */ |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14114 | env->ops = bpf_verifier_ops[tgt_prog->type]; |
| 14115 | prog->expected_attach_type = tgt_prog->expected_attach_type; |
| 14116 | } |
| 14117 | |
| 14118 | /* store info about the attachment target that will be used later */ |
| 14119 | prog->aux->attach_func_proto = tgt_info.tgt_type; |
| 14120 | prog->aux->attach_func_name = tgt_info.tgt_name; |
| 14121 | |
Toke Høiland-Jørgensen | 4a1e7c0 | 2020-09-29 14:45:51 +0200 | [diff] [blame] | 14122 | if (tgt_prog) { |
| 14123 | prog->aux->saved_dst_prog_type = tgt_prog->type; |
| 14124 | prog->aux->saved_dst_attach_type = tgt_prog->expected_attach_type; |
| 14125 | } |
| 14126 | |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14127 | if (prog->expected_attach_type == BPF_TRACE_RAW_TP) { |
| 14128 | prog->aux->attach_btf_trace = true; |
| 14129 | return 0; |
| 14130 | } else if (prog->expected_attach_type == BPF_TRACE_ITER) { |
| 14131 | if (!bpf_iter_prog_supported(prog)) |
| 14132 | return -EINVAL; |
| 14133 | return 0; |
| 14134 | } |
| 14135 | |
| 14136 | if (prog->type == BPF_PROG_TYPE_LSM) { |
| 14137 | ret = bpf_lsm_verify_prog(&env->log, prog); |
| 14138 | if (ret < 0) |
| 14139 | return ret; |
Jiri Olsa | 35e3815 | 2021-04-29 13:47:12 +0200 | [diff] [blame] | 14140 | } else if (prog->type == BPF_PROG_TYPE_TRACING && |
| 14141 | btf_id_set_contains(&btf_id_deny, btf_id)) { |
| 14142 | return -EINVAL; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14143 | } |
| 14144 | |
Andrii Nakryiko | 22dc4a0 | 2020-12-03 12:46:29 -0800 | [diff] [blame] | 14145 | key = bpf_trampoline_compute_key(tgt_prog, prog->aux->attach_btf, btf_id); |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14146 | tr = bpf_trampoline_get(key, &tgt_info); |
| 14147 | if (!tr) |
| 14148 | return -ENOMEM; |
| 14149 | |
Toke Høiland-Jørgensen | 3aac1ea | 2020-09-29 14:45:50 +0200 | [diff] [blame] | 14150 | prog->aux->dst_trampoline = tr; |
Toke Høiland-Jørgensen | f7b12b6 | 2020-09-25 23:25:02 +0200 | [diff] [blame] | 14151 | return 0; |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14152 | } |
| 14153 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 14154 | struct btf *bpf_get_btf_vmlinux(void) |
| 14155 | { |
| 14156 | if (!btf_vmlinux && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) { |
| 14157 | mutex_lock(&bpf_verifier_lock); |
| 14158 | if (!btf_vmlinux) |
| 14159 | btf_vmlinux = btf_parse_vmlinux(); |
| 14160 | mutex_unlock(&bpf_verifier_lock); |
| 14161 | } |
| 14162 | return btf_vmlinux; |
| 14163 | } |
| 14164 | |
Alexei Starovoitov | af2ac3e | 2021-05-13 17:36:05 -0700 | [diff] [blame] | 14165 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14166 | { |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14167 | u64 start_time = ktime_get_ns(); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14168 | struct bpf_verifier_env *env; |
Martin KaFai Lau | b9193c1 | 2018-03-24 11:44:22 -0700 | [diff] [blame] | 14169 | struct bpf_verifier_log *log; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14170 | int i, len, ret = -EINVAL; |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14171 | bool is_priv; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14172 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 14173 | /* no program is valid */ |
| 14174 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 14175 | return -EINVAL; |
| 14176 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14177 | /* 'struct bpf_verifier_env' can be global, but since it's not small, |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14178 | * allocate/free it every time bpf_check() is called |
| 14179 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14180 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14181 | if (!env) |
| 14182 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 14183 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14184 | |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14185 | len = (*prog)->len; |
Kees Cook | fad953c | 2018-06-12 14:27:37 -0700 | [diff] [blame] | 14186 | env->insn_aux_data = |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14187 | vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len)); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14188 | ret = -ENOMEM; |
| 14189 | if (!env->insn_aux_data) |
| 14190 | goto err_free_env; |
Jakub Kicinski | 9e4c24e | 2019-01-22 22:45:23 -0800 | [diff] [blame] | 14191 | for (i = 0; i < len; i++) |
| 14192 | env->insn_aux_data[i].orig_idx = i; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14193 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 14194 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 387544b | 2021-05-13 17:36:10 -0700 | [diff] [blame] | 14195 | env->fd_array = make_bpfptr(attr->fd_array, uattr.is_kernel); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14196 | is_priv = bpf_capable(); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14197 | |
Alan Maguire | 76654e6 | 2020-09-28 12:31:03 +0100 | [diff] [blame] | 14198 | bpf_get_btf_vmlinux(); |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14199 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14200 | /* grab the mutex to protect few globals used by verifier */ |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 14201 | if (!is_priv) |
| 14202 | mutex_lock(&bpf_verifier_lock); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14203 | |
| 14204 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 14205 | /* user requested verbose verifier output |
| 14206 | * and supplied buffer to store the verification trace |
| 14207 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 14208 | log->level = attr->log_level; |
| 14209 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 14210 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14211 | |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 14212 | /* log attributes have to be sane */ |
Hou Tao | 866de40 | 2021-12-03 13:30:01 +0800 | [diff] [blame] | 14213 | if (!bpf_verifier_log_attr_valid(log)) { |
| 14214 | ret = -EINVAL; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14215 | goto err_unlock; |
Hou Tao | 866de40 | 2021-12-03 13:30:01 +0800 | [diff] [blame] | 14216 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14217 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 14218 | |
Christy Lee | 0f55f9e | 2021-12-16 13:33:56 -0800 | [diff] [blame] | 14219 | mark_verifier_state_clean(env); |
| 14220 | |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14221 | if (IS_ERR(btf_vmlinux)) { |
| 14222 | /* Either gcc or pahole or kernel are broken. */ |
| 14223 | verbose(env, "in-kernel BTF is malformed\n"); |
| 14224 | ret = PTR_ERR(btf_vmlinux); |
Martin KaFai Lau | 3820729 | 2019-10-24 17:18:11 -0700 | [diff] [blame] | 14225 | goto skip_full_check; |
Alexei Starovoitov | 8580ac9 | 2019-10-15 20:24:57 -0700 | [diff] [blame] | 14226 | } |
| 14227 | |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 14228 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 14229 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 14230 | env->strict_alignment = true; |
David Miller | e9ee9ef | 2018-11-30 21:08:14 -0800 | [diff] [blame] | 14231 | if (attr->prog_flags & BPF_F_ANY_ALIGNMENT) |
| 14232 | env->strict_alignment = false; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14233 | |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14234 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
Andrei Matei | 01f810a | 2021-02-06 20:10:24 -0500 | [diff] [blame] | 14235 | env->allow_uninit_stack = bpf_allow_uninit_stack(); |
Andrey Ignatov | 41c48f3 | 2020-06-19 14:11:43 -0700 | [diff] [blame] | 14236 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
Alexei Starovoitov | 2c78ee8 | 2020-05-13 16:03:54 -0700 | [diff] [blame] | 14237 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 14238 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |
| 14239 | env->bpf_capable = bpf_capable(); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14240 | |
Alexei Starovoitov | 10d274e | 2019-08-22 22:52:12 -0700 | [diff] [blame] | 14241 | if (is_priv) |
| 14242 | env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ; |
| 14243 | |
Alexei Starovoitov | dc2a4eb | 2019-05-21 20:17:07 -0700 | [diff] [blame] | 14244 | env->explored_states = kvcalloc(state_htab_size(env), |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 14245 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 14246 | GFP_USER); |
| 14247 | ret = -ENOMEM; |
| 14248 | if (!env->explored_states) |
| 14249 | goto skip_full_check; |
| 14250 | |
Martin KaFai Lau | e6ac245 | 2021-03-24 18:51:42 -0700 | [diff] [blame] | 14251 | ret = add_subprog_and_kfunc(env); |
| 14252 | if (ret < 0) |
| 14253 | goto skip_full_check; |
| 14254 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 14255 | ret = check_subprogs(env); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 14256 | if (ret < 0) |
| 14257 | goto skip_full_check; |
| 14258 | |
Martin KaFai Lau | c454a46 | 2018-12-07 16:42:25 -0800 | [diff] [blame] | 14259 | ret = check_btf_info(env, attr, uattr); |
Yonghong Song | 838e969 | 2018-11-19 15:29:11 -0800 | [diff] [blame] | 14260 | if (ret < 0) |
| 14261 | goto skip_full_check; |
| 14262 | |
Alexei Starovoitov | be8704f | 2020-01-20 16:53:46 -0800 | [diff] [blame] | 14263 | ret = check_attach_btf_id(env); |
| 14264 | if (ret) |
| 14265 | goto skip_full_check; |
| 14266 | |
Hao Luo | 4976b71 | 2020-09-29 16:50:44 -0700 | [diff] [blame] | 14267 | ret = resolve_pseudo_ldimm64(env); |
| 14268 | if (ret < 0) |
| 14269 | goto skip_full_check; |
| 14270 | |
Yinjun Zhang | ceb1167 | 2021-05-20 10:58:34 +0200 | [diff] [blame] | 14271 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 14272 | ret = bpf_prog_offload_verifier_prep(env->prog); |
| 14273 | if (ret) |
| 14274 | goto skip_full_check; |
| 14275 | } |
| 14276 | |
Martin KaFai Lau | d9762e8 | 2018-12-13 10:41:48 -0800 | [diff] [blame] | 14277 | ret = check_cfg(env); |
| 14278 | if (ret < 0) |
| 14279 | goto skip_full_check; |
| 14280 | |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 14281 | ret = do_check_subprogs(env); |
| 14282 | ret = ret ?: do_check_main(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14283 | |
Quentin Monnet | c941ce9 | 2018-10-07 12:56:47 +0100 | [diff] [blame] | 14284 | if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux)) |
| 14285 | ret = bpf_prog_offload_finalize(env); |
| 14286 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14287 | skip_full_check: |
Alexei Starovoitov | 51c39bb | 2020-01-09 22:41:20 -0800 | [diff] [blame] | 14288 | kvfree(env->explored_states); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14289 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14290 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 14291 | ret = check_max_stack_depth(env); |
| 14292 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 14293 | /* instruction rewrites happen after this point */ |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14294 | if (is_priv) { |
| 14295 | if (ret == 0) |
| 14296 | opt_hard_wire_dead_code_branches(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 14297 | if (ret == 0) |
| 14298 | ret = opt_remove_dead_code(env); |
Jakub Kicinski | a1b14ab | 2019-01-22 22:45:21 -0800 | [diff] [blame] | 14299 | if (ret == 0) |
| 14300 | ret = opt_remove_nops(env); |
Jakub Kicinski | 52875a0 | 2019-01-22 22:45:20 -0800 | [diff] [blame] | 14301 | } else { |
| 14302 | if (ret == 0) |
| 14303 | sanitize_dead_code(env); |
Jakub Kicinski | e2ae4ca | 2019-01-22 22:45:19 -0800 | [diff] [blame] | 14304 | } |
| 14305 | |
Jakub Kicinski | 9b38c40 | 2018-12-19 22:13:06 -0800 | [diff] [blame] | 14306 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14307 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 14308 | ret = convert_ctx_accesses(env); |
| 14309 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 14310 | if (ret == 0) |
Brendan Jackman | e6ac593 | 2021-02-17 10:45:09 +0000 | [diff] [blame] | 14311 | ret = do_misc_fixups(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 14312 | |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 14313 | /* do 32-bit optimization after insn patching has done so those patched |
| 14314 | * insns could be handled correctly. |
| 14315 | */ |
Jiong Wang | d6c2308 | 2019-05-24 23:25:18 +0100 | [diff] [blame] | 14316 | if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) { |
| 14317 | ret = opt_subreg_zext_lo32_rnd_hi32(env, attr); |
| 14318 | env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret |
| 14319 | : false; |
Jiong Wang | a4b1d3c | 2019-05-24 23:25:15 +0100 | [diff] [blame] | 14320 | } |
| 14321 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 14322 | if (ret == 0) |
| 14323 | ret = fixup_call_args(env); |
| 14324 | |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14325 | env->verification_time = ktime_get_ns() - start_time; |
| 14326 | print_verification_stats(env); |
Dave Marchevsky | aba64c7 | 2021-10-20 00:48:17 -0700 | [diff] [blame] | 14327 | env->prog->aux->verified_insns = env->insn_processed; |
Alexei Starovoitov | 06ee711 | 2019-04-01 21:27:40 -0700 | [diff] [blame] | 14328 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14329 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14330 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14331 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14332 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14333 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14334 | } |
| 14335 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14336 | if (ret) |
| 14337 | goto err_release_maps; |
| 14338 | |
| 14339 | if (env->used_map_cnt) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14340 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14341 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 14342 | sizeof(env->used_maps[0]), |
| 14343 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14344 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14345 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14346 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14347 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14348 | } |
| 14349 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14350 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14351 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14352 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14353 | } |
| 14354 | if (env->used_btf_cnt) { |
| 14355 | /* if program passed verifier, update used_btfs in bpf_prog_aux */ |
| 14356 | env->prog->aux->used_btfs = kmalloc_array(env->used_btf_cnt, |
| 14357 | sizeof(env->used_btfs[0]), |
| 14358 | GFP_KERNEL); |
| 14359 | if (!env->prog->aux->used_btfs) { |
| 14360 | ret = -ENOMEM; |
| 14361 | goto err_release_maps; |
| 14362 | } |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14363 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14364 | memcpy(env->prog->aux->used_btfs, env->used_btfs, |
| 14365 | sizeof(env->used_btfs[0]) * env->used_btf_cnt); |
| 14366 | env->prog->aux->used_btf_cnt = env->used_btf_cnt; |
| 14367 | } |
| 14368 | if (env->used_map_cnt || env->used_btf_cnt) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14369 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 14370 | * bpf_ld_imm64 instructions |
| 14371 | */ |
| 14372 | convert_pseudo_ld_imm64(env); |
| 14373 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 14374 | |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14375 | adjust_btf_func(env); |
Yonghong Song | ba64e7d | 2018-11-24 23:20:44 -0800 | [diff] [blame] | 14376 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 14377 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14378 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14379 | /* if we didn't copy map pointers into bpf_prog_info, release |
Jakub Kicinski | ab7f5bf | 2018-05-03 18:37:17 -0700 | [diff] [blame] | 14380 | * them now. Otherwise free_used_maps() will release them. |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 14381 | */ |
| 14382 | release_maps(env); |
Andrii Nakryiko | 541c3ba | 2021-01-11 23:55:18 -0800 | [diff] [blame] | 14383 | if (!env->prog->aux->used_btfs) |
| 14384 | release_btfs(env); |
Toke Høiland-Jørgensen | 03f87c0 | 2020-04-24 15:34:27 +0200 | [diff] [blame] | 14385 | |
| 14386 | /* extension progs temporarily inherit the attach_type of their targets |
| 14387 | for verification purposes, so set it back to zero before returning |
| 14388 | */ |
| 14389 | if (env->prog->type == BPF_PROG_TYPE_EXT) |
| 14390 | env->prog->expected_attach_type = 0; |
| 14391 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 14392 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14393 | err_unlock: |
Alexei Starovoitov | 45a73c1 | 2019-04-19 07:44:55 -0700 | [diff] [blame] | 14394 | if (!is_priv) |
| 14395 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 14396 | vfree(env->insn_aux_data); |
| 14397 | err_free_env: |
| 14398 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 14399 | return ret; |
| 14400 | } |