This source file includes following definitions.
- bpf_map_ptr_poisoned
- bpf_map_ptr_unpriv
- bpf_map_ptr_store
- find_linfo
- bpf_verifier_vlog
- __printf
- __printf
- ltrim
- __printf
- type_is_pkt_pointer
- type_is_sk_pointer
- reg_type_may_be_null
- reg_may_point_to_spin_lock
- reg_type_may_be_refcounted_or_null
- arg_type_may_be_refcounted
- is_release_function
- is_acquire_function
- is_ptr_cast_function
- print_liveness
- print_verifier_state
- REALLOC_STATE_FN
- acquire_reference_state
- release_reference_state
- transfer_reference_state
- free_func_state
- clear_jmp_history
- free_verifier_state
- copy_func_state
- copy_verifier_state
- update_branch_counts
- pop_stack
- push_stack
- __mark_reg_known
- __mark_reg_known_zero
- __mark_reg_const_zero
- mark_reg_known_zero
- reg_is_pkt_pointer
- reg_is_pkt_pointer_any
- reg_is_init_pkt_pointer
- __update_reg_bounds
- __reg_deduce_bounds
- __reg_bound_offset
- __mark_reg_unbounded
- __mark_reg_unknown
- mark_reg_unknown
- __mark_reg_not_init
- mark_reg_not_init
- init_reg_state
- init_func_state
- cmp_subprogs
- find_subprog
- add_subprog
- check_subprogs
- mark_reg_read
- is_reg64
- insn_no_def
- insn_has_def32
- mark_insn_zext
- check_reg_arg
- push_jmp_history
- get_prev_insn_idx
- backtrack_insn
- mark_all_scalars_precise
- __mark_chain_precision
- mark_chain_precision
- mark_chain_precision_stack
- is_spillable_regtype
- register_is_null
- register_is_const
- __is_pointer_value
- save_register_state
- check_stack_write
- check_stack_read
- check_stack_access
- check_map_access_type
- __check_map_access
- check_map_access
- may_access_direct_pkt_data
- __check_packet_access
- check_packet_access
- check_ctx_access
- check_flow_keys_access
- check_sock_access
- is_pointer_value
- is_ctx_reg
- is_sk_reg
- is_pkt_reg
- is_flow_key_reg
- check_pkt_ptr_alignment
- check_generic_ptr_alignment
- check_ptr_alignment
- update_stack_depth
- check_max_stack_depth
- get_callee_stack_depth
- check_ctx_reg
- check_tp_buffer_access
- coerce_reg_to_size
- check_mem_access
- check_xadd
- __check_stack_boundary
- check_stack_boundary
- check_helper_mem_access
- process_spin_lock
- arg_type_is_mem_ptr
- arg_type_is_mem_size
- arg_type_is_int_ptr
- int_ptr_type_to_size
- check_func_arg
- check_map_func_compatibility
- check_raw_mode_ok
- check_args_pair_invalid
- check_arg_pair_ok
- check_refcount_ok
- check_func_proto
- __clear_all_pkt_pointers
- clear_all_pkt_pointers
- release_reg_references
- release_reference
- check_func_call
- prepare_func_exit
- do_refine_retval_range
- record_func_map
- check_reference_leak
- check_helper_call
- signed_add_overflows
- signed_sub_overflows
- check_reg_sane_offset
- cur_aux
- retrieve_ptr_limit
- can_skip_alu_sanitation
- update_alu_sanitation_state
- sanitize_val_alu
- sanitize_ptr_alu
- adjust_ptr_min_max_vals
- adjust_scalar_min_max_vals
- adjust_reg_min_max_vals
- check_alu_op
- __find_good_pkt_pointers
- find_good_pkt_pointers
- is_branch_taken
- gen_hi_min
- gen_hi_max
- cmp_val_with_extended_s64
- set_upper_bound
- set_lower_bound
- reg_set_min_max
- reg_set_min_max_inv
- __reg_combine_min_max
- reg_combine_min_max
- mark_ptr_or_null_reg
- __mark_ptr_or_null_regs
- mark_ptr_or_null_regs
- try_match_pkt_pointers
- check_cond_jmp_op
- check_ld_imm
- may_access_skb
- check_ld_abs
- check_return_code
- state_htab_size
- explored_state
- init_explored_state
- push_insn
- check_cfg
- check_btf_func
- adjust_btf_func
- check_btf_line
- check_btf_info
- range_within
- check_ids
- clean_func_state
- clean_verifier_state
- clean_live_states
- regsafe
- stacksafe
- refsafe
- func_states_equal
- states_equal
- propagate_liveness_reg
- propagate_liveness
- propagate_precision
- states_maybe_looping
- is_state_visited
- reg_type_mismatch_ok
- reg_type_mismatch
- do_check
- check_map_prealloc
- is_tracing_prog_type
- check_map_prog_compatibility
- bpf_map_is_cgroup_storage
- replace_map_fd_with_map_ptr
- release_maps
- convert_pseudo_ld_imm64
- adjust_insn_aux_data
- adjust_subprog_starts
- bpf_patch_insn_data
- adjust_subprog_starts_after_remove
- bpf_adj_linfo_after_remove
- verifier_remove_insns
- sanitize_dead_code
- insn_is_cond_jump
- opt_hard_wire_dead_code_branches
- opt_remove_dead_code
- opt_remove_nops
- opt_subreg_zext_lo32_rnd_hi32
- convert_ctx_accesses
- jit_subprogs
- fixup_call_args
- fixup_bpf_calls
- free_states
- print_verification_stats
- bpf_check
1
2
3
4
5
6 #include <uapi/linux/btf.h>
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/bpf.h>
11 #include <linux/btf.h>
12 #include <linux/bpf_verifier.h>
13 #include <linux/filter.h>
14 #include <net/netlink.h>
15 #include <linux/file.h>
16 #include <linux/vmalloc.h>
17 #include <linux/stringify.h>
18 #include <linux/bsearch.h>
19 #include <linux/sort.h>
20 #include <linux/perf_event.h>
21 #include <linux/ctype.h>
22
23 #include "disasm.h"
24
25 static const struct bpf_verifier_ops * const bpf_verifier_ops[] = {
26 #define BPF_PROG_TYPE(_id, _name) \
27 [_id] = & _name ## _verifier_ops,
28 #define BPF_MAP_TYPE(_id, _ops)
29 #include <linux/bpf_types.h>
30 #undef BPF_PROG_TYPE
31 #undef BPF_MAP_TYPE
32 };
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 struct bpf_verifier_stack_elem {
161
162
163
164
165 struct bpf_verifier_state st;
166 int insn_idx;
167 int prev_insn_idx;
168 struct bpf_verifier_stack_elem *next;
169 };
170
171 #define BPF_COMPLEXITY_LIMIT_JMP_SEQ 8192
172 #define BPF_COMPLEXITY_LIMIT_STATES 64
173
174 #define BPF_MAP_PTR_UNPRIV 1UL
175 #define BPF_MAP_PTR_POISON ((void *)((0xeB9FUL << 1) + \
176 POISON_POINTER_DELTA))
177 #define BPF_MAP_PTR(X) ((struct bpf_map *)((X) & ~BPF_MAP_PTR_UNPRIV))
178
179 static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
180 {
181 return BPF_MAP_PTR(aux->map_state) == BPF_MAP_PTR_POISON;
182 }
183
184 static bool bpf_map_ptr_unpriv(const struct bpf_insn_aux_data *aux)
185 {
186 return aux->map_state & BPF_MAP_PTR_UNPRIV;
187 }
188
189 static void bpf_map_ptr_store(struct bpf_insn_aux_data *aux,
190 const struct bpf_map *map, bool unpriv)
191 {
192 BUILD_BUG_ON((unsigned long)BPF_MAP_PTR_POISON & BPF_MAP_PTR_UNPRIV);
193 unpriv |= bpf_map_ptr_unpriv(aux);
194 aux->map_state = (unsigned long)map |
195 (unpriv ? BPF_MAP_PTR_UNPRIV : 0UL);
196 }
197
198 struct bpf_call_arg_meta {
199 struct bpf_map *map_ptr;
200 bool raw_mode;
201 bool pkt_access;
202 int regno;
203 int access_size;
204 u64 msize_max_value;
205 int ref_obj_id;
206 int func_id;
207 };
208
209 static DEFINE_MUTEX(bpf_verifier_lock);
210
211 static const struct bpf_line_info *
212 find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
213 {
214 const struct bpf_line_info *linfo;
215 const struct bpf_prog *prog;
216 u32 i, nr_linfo;
217
218 prog = env->prog;
219 nr_linfo = prog->aux->nr_linfo;
220
221 if (!nr_linfo || insn_off >= prog->len)
222 return NULL;
223
224 linfo = prog->aux->linfo;
225 for (i = 1; i < nr_linfo; i++)
226 if (insn_off < linfo[i].insn_off)
227 break;
228
229 return &linfo[i - 1];
230 }
231
232 void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
233 va_list args)
234 {
235 unsigned int n;
236
237 n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args);
238
239 WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1,
240 "verifier log line truncated - local buffer too short\n");
241
242 n = min(log->len_total - log->len_used - 1, n);
243 log->kbuf[n] = '\0';
244
245 if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1))
246 log->len_used += n;
247 else
248 log->ubuf = NULL;
249 }
250
251
252
253
254
255 __printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
256 const char *fmt, ...)
257 {
258 va_list args;
259
260 if (!bpf_verifier_log_needed(&env->log))
261 return;
262
263 va_start(args, fmt);
264 bpf_verifier_vlog(&env->log, fmt, args);
265 va_end(args);
266 }
267 EXPORT_SYMBOL_GPL(bpf_verifier_log_write);
268
269 __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
270 {
271 struct bpf_verifier_env *env = private_data;
272 va_list args;
273
274 if (!bpf_verifier_log_needed(&env->log))
275 return;
276
277 va_start(args, fmt);
278 bpf_verifier_vlog(&env->log, fmt, args);
279 va_end(args);
280 }
281
282 static const char *ltrim(const char *s)
283 {
284 while (isspace(*s))
285 s++;
286
287 return s;
288 }
289
290 __printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
291 u32 insn_off,
292 const char *prefix_fmt, ...)
293 {
294 const struct bpf_line_info *linfo;
295
296 if (!bpf_verifier_log_needed(&env->log))
297 return;
298
299 linfo = find_linfo(env, insn_off);
300 if (!linfo || linfo == env->prev_linfo)
301 return;
302
303 if (prefix_fmt) {
304 va_list args;
305
306 va_start(args, prefix_fmt);
307 bpf_verifier_vlog(&env->log, prefix_fmt, args);
308 va_end(args);
309 }
310
311 verbose(env, "%s\n",
312 ltrim(btf_name_by_offset(env->prog->aux->btf,
313 linfo->line_off)));
314
315 env->prev_linfo = linfo;
316 }
317
318 static bool type_is_pkt_pointer(enum bpf_reg_type type)
319 {
320 return type == PTR_TO_PACKET ||
321 type == PTR_TO_PACKET_META;
322 }
323
324 static bool type_is_sk_pointer(enum bpf_reg_type type)
325 {
326 return type == PTR_TO_SOCKET ||
327 type == PTR_TO_SOCK_COMMON ||
328 type == PTR_TO_TCP_SOCK ||
329 type == PTR_TO_XDP_SOCK;
330 }
331
332 static bool reg_type_may_be_null(enum bpf_reg_type type)
333 {
334 return type == PTR_TO_MAP_VALUE_OR_NULL ||
335 type == PTR_TO_SOCKET_OR_NULL ||
336 type == PTR_TO_SOCK_COMMON_OR_NULL ||
337 type == PTR_TO_TCP_SOCK_OR_NULL;
338 }
339
340 static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
341 {
342 return reg->type == PTR_TO_MAP_VALUE &&
343 map_value_has_spin_lock(reg->map_ptr);
344 }
345
346 static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
347 {
348 return type == PTR_TO_SOCKET ||
349 type == PTR_TO_SOCKET_OR_NULL ||
350 type == PTR_TO_TCP_SOCK ||
351 type == PTR_TO_TCP_SOCK_OR_NULL;
352 }
353
354 static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
355 {
356 return type == ARG_PTR_TO_SOCK_COMMON;
357 }
358
359
360
361
362
363 static bool is_release_function(enum bpf_func_id func_id)
364 {
365 return func_id == BPF_FUNC_sk_release;
366 }
367
368 static bool is_acquire_function(enum bpf_func_id func_id)
369 {
370 return func_id == BPF_FUNC_sk_lookup_tcp ||
371 func_id == BPF_FUNC_sk_lookup_udp ||
372 func_id == BPF_FUNC_skc_lookup_tcp;
373 }
374
375 static bool is_ptr_cast_function(enum bpf_func_id func_id)
376 {
377 return func_id == BPF_FUNC_tcp_sock ||
378 func_id == BPF_FUNC_sk_fullsock;
379 }
380
381
382 static const char * const reg_type_str[] = {
383 [NOT_INIT] = "?",
384 [SCALAR_VALUE] = "inv",
385 [PTR_TO_CTX] = "ctx",
386 [CONST_PTR_TO_MAP] = "map_ptr",
387 [PTR_TO_MAP_VALUE] = "map_value",
388 [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
389 [PTR_TO_STACK] = "fp",
390 [PTR_TO_PACKET] = "pkt",
391 [PTR_TO_PACKET_META] = "pkt_meta",
392 [PTR_TO_PACKET_END] = "pkt_end",
393 [PTR_TO_FLOW_KEYS] = "flow_keys",
394 [PTR_TO_SOCKET] = "sock",
395 [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
396 [PTR_TO_SOCK_COMMON] = "sock_common",
397 [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
398 [PTR_TO_TCP_SOCK] = "tcp_sock",
399 [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
400 [PTR_TO_TP_BUFFER] = "tp_buffer",
401 [PTR_TO_XDP_SOCK] = "xdp_sock",
402 };
403
404 static char slot_type_char[] = {
405 [STACK_INVALID] = '?',
406 [STACK_SPILL] = 'r',
407 [STACK_MISC] = 'm',
408 [STACK_ZERO] = '0',
409 };
410
411 static void print_liveness(struct bpf_verifier_env *env,
412 enum bpf_reg_liveness live)
413 {
414 if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN | REG_LIVE_DONE))
415 verbose(env, "_");
416 if (live & REG_LIVE_READ)
417 verbose(env, "r");
418 if (live & REG_LIVE_WRITTEN)
419 verbose(env, "w");
420 if (live & REG_LIVE_DONE)
421 verbose(env, "D");
422 }
423
424 static struct bpf_func_state *func(struct bpf_verifier_env *env,
425 const struct bpf_reg_state *reg)
426 {
427 struct bpf_verifier_state *cur = env->cur_state;
428
429 return cur->frame[reg->frameno];
430 }
431
432 static void print_verifier_state(struct bpf_verifier_env *env,
433 const struct bpf_func_state *state)
434 {
435 const struct bpf_reg_state *reg;
436 enum bpf_reg_type t;
437 int i;
438
439 if (state->frameno)
440 verbose(env, " frame%d:", state->frameno);
441 for (i = 0; i < MAX_BPF_REG; i++) {
442 reg = &state->regs[i];
443 t = reg->type;
444 if (t == NOT_INIT)
445 continue;
446 verbose(env, " R%d", i);
447 print_liveness(env, reg->live);
448 verbose(env, "=%s", reg_type_str[t]);
449 if (t == SCALAR_VALUE && reg->precise)
450 verbose(env, "P");
451 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
452 tnum_is_const(reg->var_off)) {
453
454 verbose(env, "%lld", reg->var_off.value + reg->off);
455 } else {
456 verbose(env, "(id=%d", reg->id);
457 if (reg_type_may_be_refcounted_or_null(t))
458 verbose(env, ",ref_obj_id=%d", reg->ref_obj_id);
459 if (t != SCALAR_VALUE)
460 verbose(env, ",off=%d", reg->off);
461 if (type_is_pkt_pointer(t))
462 verbose(env, ",r=%d", reg->range);
463 else if (t == CONST_PTR_TO_MAP ||
464 t == PTR_TO_MAP_VALUE ||
465 t == PTR_TO_MAP_VALUE_OR_NULL)
466 verbose(env, ",ks=%d,vs=%d",
467 reg->map_ptr->key_size,
468 reg->map_ptr->value_size);
469 if (tnum_is_const(reg->var_off)) {
470
471
472
473
474 verbose(env, ",imm=%llx", reg->var_off.value);
475 } else {
476 if (reg->smin_value != reg->umin_value &&
477 reg->smin_value != S64_MIN)
478 verbose(env, ",smin_value=%lld",
479 (long long)reg->smin_value);
480 if (reg->smax_value != reg->umax_value &&
481 reg->smax_value != S64_MAX)
482 verbose(env, ",smax_value=%lld",
483 (long long)reg->smax_value);
484 if (reg->umin_value != 0)
485 verbose(env, ",umin_value=%llu",
486 (unsigned long long)reg->umin_value);
487 if (reg->umax_value != U64_MAX)
488 verbose(env, ",umax_value=%llu",
489 (unsigned long long)reg->umax_value);
490 if (!tnum_is_unknown(reg->var_off)) {
491 char tn_buf[48];
492
493 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
494 verbose(env, ",var_off=%s", tn_buf);
495 }
496 }
497 verbose(env, ")");
498 }
499 }
500 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
501 char types_buf[BPF_REG_SIZE + 1];
502 bool valid = false;
503 int j;
504
505 for (j = 0; j < BPF_REG_SIZE; j++) {
506 if (state->stack[i].slot_type[j] != STACK_INVALID)
507 valid = true;
508 types_buf[j] = slot_type_char[
509 state->stack[i].slot_type[j]];
510 }
511 types_buf[BPF_REG_SIZE] = 0;
512 if (!valid)
513 continue;
514 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
515 print_liveness(env, state->stack[i].spilled_ptr.live);
516 if (state->stack[i].slot_type[0] == STACK_SPILL) {
517 reg = &state->stack[i].spilled_ptr;
518 t = reg->type;
519 verbose(env, "=%s", reg_type_str[t]);
520 if (t == SCALAR_VALUE && reg->precise)
521 verbose(env, "P");
522 if (t == SCALAR_VALUE && tnum_is_const(reg->var_off))
523 verbose(env, "%lld", reg->var_off.value + reg->off);
524 } else {
525 verbose(env, "=%s", types_buf);
526 }
527 }
528 if (state->acquired_refs && state->refs[0].id) {
529 verbose(env, " refs=%d", state->refs[0].id);
530 for (i = 1; i < state->acquired_refs; i++)
531 if (state->refs[i].id)
532 verbose(env, ",%d", state->refs[i].id);
533 }
534 verbose(env, "\n");
535 }
536
537 #define COPY_STATE_FN(NAME, COUNT, FIELD, SIZE) \
538 static int copy_##NAME##_state(struct bpf_func_state *dst, \
539 const struct bpf_func_state *src) \
540 { \
541 if (!src->FIELD) \
542 return 0; \
543 if (WARN_ON_ONCE(dst->COUNT < src->COUNT)) { \
544 \
545 memset(dst, 0, sizeof(*dst)); \
546 return -EFAULT; \
547 } \
548 memcpy(dst->FIELD, src->FIELD, \
549 sizeof(*src->FIELD) * (src->COUNT / SIZE)); \
550 return 0; \
551 }
552
553 COPY_STATE_FN(reference, acquired_refs, refs, 1)
554
555 COPY_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
556 #undef COPY_STATE_FN
557
558 #define REALLOC_STATE_FN(NAME, COUNT, FIELD, SIZE) \
559 static int realloc_##NAME##_state(struct bpf_func_state *state, int size, \
560 bool copy_old) \
561 { \
562 u32 old_size = state->COUNT; \
563 struct bpf_##NAME##_state *new_##FIELD; \
564 int slot = size / SIZE; \
565 \
566 if (size <= old_size || !size) { \
567 if (copy_old) \
568 return 0; \
569 state->COUNT = slot * SIZE; \
570 if (!size && old_size) { \
571 kfree(state->FIELD); \
572 state->FIELD = NULL; \
573 } \
574 return 0; \
575 } \
576 new_##FIELD = kmalloc_array(slot, sizeof(struct bpf_##NAME##_state), \
577 GFP_KERNEL); \
578 if (!new_##FIELD) \
579 return -ENOMEM; \
580 if (copy_old) { \
581 if (state->FIELD) \
582 memcpy(new_##FIELD, state->FIELD, \
583 sizeof(*new_##FIELD) * (old_size / SIZE)); \
584 memset(new_##FIELD + old_size / SIZE, 0, \
585 sizeof(*new_##FIELD) * (size - old_size) / SIZE); \
586 } \
587 state->COUNT = slot * SIZE; \
588 kfree(state->FIELD); \
589 state->FIELD = new_##FIELD; \
590 return 0; \
591 }
592
593 REALLOC_STATE_FN(reference, acquired_refs, refs, 1)
594
595 REALLOC_STATE_FN(stack, allocated_stack, stack, BPF_REG_SIZE)
596 #undef REALLOC_STATE_FN
597
598
599
600
601
602
603
604
605 static int realloc_func_state(struct bpf_func_state *state, int stack_size,
606 int refs_size, bool copy_old)
607 {
608 int err = realloc_reference_state(state, refs_size, copy_old);
609 if (err)
610 return err;
611 return realloc_stack_state(state, stack_size, copy_old);
612 }
613
614
615
616
617
618
619 static int acquire_reference_state(struct bpf_verifier_env *env, int insn_idx)
620 {
621 struct bpf_func_state *state = cur_func(env);
622 int new_ofs = state->acquired_refs;
623 int id, err;
624
625 err = realloc_reference_state(state, state->acquired_refs + 1, true);
626 if (err)
627 return err;
628 id = ++env->id_gen;
629 state->refs[new_ofs].id = id;
630 state->refs[new_ofs].insn_idx = insn_idx;
631
632 return id;
633 }
634
635
636 static int release_reference_state(struct bpf_func_state *state, int ptr_id)
637 {
638 int i, last_idx;
639
640 last_idx = state->acquired_refs - 1;
641 for (i = 0; i < state->acquired_refs; i++) {
642 if (state->refs[i].id == ptr_id) {
643 if (last_idx && i != last_idx)
644 memcpy(&state->refs[i], &state->refs[last_idx],
645 sizeof(*state->refs));
646 memset(&state->refs[last_idx], 0, sizeof(*state->refs));
647 state->acquired_refs--;
648 return 0;
649 }
650 }
651 return -EINVAL;
652 }
653
654 static int transfer_reference_state(struct bpf_func_state *dst,
655 struct bpf_func_state *src)
656 {
657 int err = realloc_reference_state(dst, src->acquired_refs, false);
658 if (err)
659 return err;
660 err = copy_reference_state(dst, src);
661 if (err)
662 return err;
663 return 0;
664 }
665
666 static void free_func_state(struct bpf_func_state *state)
667 {
668 if (!state)
669 return;
670 kfree(state->refs);
671 kfree(state->stack);
672 kfree(state);
673 }
674
675 static void clear_jmp_history(struct bpf_verifier_state *state)
676 {
677 kfree(state->jmp_history);
678 state->jmp_history = NULL;
679 state->jmp_history_cnt = 0;
680 }
681
682 static void free_verifier_state(struct bpf_verifier_state *state,
683 bool free_self)
684 {
685 int i;
686
687 for (i = 0; i <= state->curframe; i++) {
688 free_func_state(state->frame[i]);
689 state->frame[i] = NULL;
690 }
691 clear_jmp_history(state);
692 if (free_self)
693 kfree(state);
694 }
695
696
697
698
699 static int copy_func_state(struct bpf_func_state *dst,
700 const struct bpf_func_state *src)
701 {
702 int err;
703
704 err = realloc_func_state(dst, src->allocated_stack, src->acquired_refs,
705 false);
706 if (err)
707 return err;
708 memcpy(dst, src, offsetof(struct bpf_func_state, acquired_refs));
709 err = copy_reference_state(dst, src);
710 if (err)
711 return err;
712 return copy_stack_state(dst, src);
713 }
714
715 static int copy_verifier_state(struct bpf_verifier_state *dst_state,
716 const struct bpf_verifier_state *src)
717 {
718 struct bpf_func_state *dst;
719 u32 jmp_sz = sizeof(struct bpf_idx_pair) * src->jmp_history_cnt;
720 int i, err;
721
722 if (dst_state->jmp_history_cnt < src->jmp_history_cnt) {
723 kfree(dst_state->jmp_history);
724 dst_state->jmp_history = kmalloc(jmp_sz, GFP_USER);
725 if (!dst_state->jmp_history)
726 return -ENOMEM;
727 }
728 memcpy(dst_state->jmp_history, src->jmp_history, jmp_sz);
729 dst_state->jmp_history_cnt = src->jmp_history_cnt;
730
731
732 for (i = src->curframe + 1; i <= dst_state->curframe; i++) {
733 free_func_state(dst_state->frame[i]);
734 dst_state->frame[i] = NULL;
735 }
736 dst_state->speculative = src->speculative;
737 dst_state->curframe = src->curframe;
738 dst_state->active_spin_lock = src->active_spin_lock;
739 dst_state->branches = src->branches;
740 dst_state->parent = src->parent;
741 dst_state->first_insn_idx = src->first_insn_idx;
742 dst_state->last_insn_idx = src->last_insn_idx;
743 for (i = 0; i <= src->curframe; i++) {
744 dst = dst_state->frame[i];
745 if (!dst) {
746 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
747 if (!dst)
748 return -ENOMEM;
749 dst_state->frame[i] = dst;
750 }
751 err = copy_func_state(dst, src->frame[i]);
752 if (err)
753 return err;
754 }
755 return 0;
756 }
757
758 static void update_branch_counts(struct bpf_verifier_env *env, struct bpf_verifier_state *st)
759 {
760 while (st) {
761 u32 br = --st->branches;
762
763
764
765
766 WARN_ONCE((int)br < 0,
767 "BUG update_branch_counts:branches_to_explore=%d\n",
768 br);
769 if (br)
770 break;
771 st = st->parent;
772 }
773 }
774
775 static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx,
776 int *insn_idx)
777 {
778 struct bpf_verifier_state *cur = env->cur_state;
779 struct bpf_verifier_stack_elem *elem, *head = env->head;
780 int err;
781
782 if (env->head == NULL)
783 return -ENOENT;
784
785 if (cur) {
786 err = copy_verifier_state(cur, &head->st);
787 if (err)
788 return err;
789 }
790 if (insn_idx)
791 *insn_idx = head->insn_idx;
792 if (prev_insn_idx)
793 *prev_insn_idx = head->prev_insn_idx;
794 elem = head->next;
795 free_verifier_state(&head->st, false);
796 kfree(head);
797 env->head = elem;
798 env->stack_size--;
799 return 0;
800 }
801
802 static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,
803 int insn_idx, int prev_insn_idx,
804 bool speculative)
805 {
806 struct bpf_verifier_state *cur = env->cur_state;
807 struct bpf_verifier_stack_elem *elem;
808 int err;
809
810 elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL);
811 if (!elem)
812 goto err;
813
814 elem->insn_idx = insn_idx;
815 elem->prev_insn_idx = prev_insn_idx;
816 elem->next = env->head;
817 env->head = elem;
818 env->stack_size++;
819 err = copy_verifier_state(&elem->st, cur);
820 if (err)
821 goto err;
822 elem->st.speculative |= speculative;
823 if (env->stack_size > BPF_COMPLEXITY_LIMIT_JMP_SEQ) {
824 verbose(env, "The sequence of %d jumps is too complex.\n",
825 env->stack_size);
826 goto err;
827 }
828 if (elem->st.parent) {
829 ++elem->st.parent->branches;
830
831
832
833
834
835
836
837
838
839 }
840 return &elem->st;
841 err:
842 free_verifier_state(env->cur_state, true);
843 env->cur_state = NULL;
844
845 while (!pop_stack(env, NULL, NULL));
846 return NULL;
847 }
848
849 #define CALLER_SAVED_REGS 6
850 static const int caller_saved[CALLER_SAVED_REGS] = {
851 BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5
852 };
853
854 static void __mark_reg_not_init(const struct bpf_verifier_env *env,
855 struct bpf_reg_state *reg);
856
857
858
859
860 static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
861 {
862
863 memset(((u8 *)reg) + sizeof(reg->type), 0,
864 offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
865 reg->var_off = tnum_const(imm);
866 reg->smin_value = (s64)imm;
867 reg->smax_value = (s64)imm;
868 reg->umin_value = imm;
869 reg->umax_value = imm;
870 }
871
872
873
874
875 static void __mark_reg_known_zero(struct bpf_reg_state *reg)
876 {
877 __mark_reg_known(reg, 0);
878 }
879
880 static void __mark_reg_const_zero(struct bpf_reg_state *reg)
881 {
882 __mark_reg_known(reg, 0);
883 reg->type = SCALAR_VALUE;
884 }
885
886 static void mark_reg_known_zero(struct bpf_verifier_env *env,
887 struct bpf_reg_state *regs, u32 regno)
888 {
889 if (WARN_ON(regno >= MAX_BPF_REG)) {
890 verbose(env, "mark_reg_known_zero(regs, %u)\n", regno);
891
892 for (regno = 0; regno < MAX_BPF_REG; regno++)
893 __mark_reg_not_init(env, regs + regno);
894 return;
895 }
896 __mark_reg_known_zero(regs + regno);
897 }
898
899 static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
900 {
901 return type_is_pkt_pointer(reg->type);
902 }
903
904 static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg)
905 {
906 return reg_is_pkt_pointer(reg) ||
907 reg->type == PTR_TO_PACKET_END;
908 }
909
910
911 static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg,
912 enum bpf_reg_type which)
913 {
914
915
916
917
918 return reg->type == which &&
919 reg->id == 0 &&
920 reg->off == 0 &&
921 tnum_equals_const(reg->var_off, 0);
922 }
923
924
925 static void __update_reg_bounds(struct bpf_reg_state *reg)
926 {
927
928 reg->smin_value = max_t(s64, reg->smin_value,
929 reg->var_off.value | (reg->var_off.mask & S64_MIN));
930
931 reg->smax_value = min_t(s64, reg->smax_value,
932 reg->var_off.value | (reg->var_off.mask & S64_MAX));
933 reg->umin_value = max(reg->umin_value, reg->var_off.value);
934 reg->umax_value = min(reg->umax_value,
935 reg->var_off.value | reg->var_off.mask);
936 }
937
938
939 static void __reg_deduce_bounds(struct bpf_reg_state *reg)
940 {
941
942
943
944
945
946 if (reg->smin_value >= 0 || reg->smax_value < 0) {
947 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
948 reg->umin_value);
949 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
950 reg->umax_value);
951 return;
952 }
953
954
955
956 if ((s64)reg->umax_value >= 0) {
957
958
959
960 reg->smin_value = reg->umin_value;
961 reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
962 reg->umax_value);
963 } else if ((s64)reg->umin_value < 0) {
964
965
966
967 reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
968 reg->umin_value);
969 reg->smax_value = reg->umax_value;
970 }
971 }
972
973
974 static void __reg_bound_offset(struct bpf_reg_state *reg)
975 {
976 reg->var_off = tnum_intersect(reg->var_off,
977 tnum_range(reg->umin_value,
978 reg->umax_value));
979 }
980
981
982 static void __mark_reg_unbounded(struct bpf_reg_state *reg)
983 {
984 reg->smin_value = S64_MIN;
985 reg->smax_value = S64_MAX;
986 reg->umin_value = 0;
987 reg->umax_value = U64_MAX;
988 }
989
990
991 static void __mark_reg_unknown(const struct bpf_verifier_env *env,
992 struct bpf_reg_state *reg)
993 {
994
995
996
997
998 memset(reg, 0, offsetof(struct bpf_reg_state, var_off));
999 reg->type = SCALAR_VALUE;
1000 reg->var_off = tnum_unknown;
1001 reg->frameno = 0;
1002 reg->precise = env->subprog_cnt > 1 || !env->allow_ptr_leaks ?
1003 true : false;
1004 __mark_reg_unbounded(reg);
1005 }
1006
1007 static void mark_reg_unknown(struct bpf_verifier_env *env,
1008 struct bpf_reg_state *regs, u32 regno)
1009 {
1010 if (WARN_ON(regno >= MAX_BPF_REG)) {
1011 verbose(env, "mark_reg_unknown(regs, %u)\n", regno);
1012
1013 for (regno = 0; regno < BPF_REG_FP; regno++)
1014 __mark_reg_not_init(env, regs + regno);
1015 return;
1016 }
1017 __mark_reg_unknown(env, regs + regno);
1018 }
1019
1020 static void __mark_reg_not_init(const struct bpf_verifier_env *env,
1021 struct bpf_reg_state *reg)
1022 {
1023 __mark_reg_unknown(env, reg);
1024 reg->type = NOT_INIT;
1025 }
1026
1027 static void mark_reg_not_init(struct bpf_verifier_env *env,
1028 struct bpf_reg_state *regs, u32 regno)
1029 {
1030 if (WARN_ON(regno >= MAX_BPF_REG)) {
1031 verbose(env, "mark_reg_not_init(regs, %u)\n", regno);
1032
1033 for (regno = 0; regno < BPF_REG_FP; regno++)
1034 __mark_reg_not_init(env, regs + regno);
1035 return;
1036 }
1037 __mark_reg_not_init(env, regs + regno);
1038 }
1039
1040 #define DEF_NOT_SUBREG (0)
1041 static void init_reg_state(struct bpf_verifier_env *env,
1042 struct bpf_func_state *state)
1043 {
1044 struct bpf_reg_state *regs = state->regs;
1045 int i;
1046
1047 for (i = 0; i < MAX_BPF_REG; i++) {
1048 mark_reg_not_init(env, regs, i);
1049 regs[i].live = REG_LIVE_NONE;
1050 regs[i].parent = NULL;
1051 regs[i].subreg_def = DEF_NOT_SUBREG;
1052 }
1053
1054
1055 regs[BPF_REG_FP].type = PTR_TO_STACK;
1056 mark_reg_known_zero(env, regs, BPF_REG_FP);
1057 regs[BPF_REG_FP].frameno = state->frameno;
1058
1059
1060 regs[BPF_REG_1].type = PTR_TO_CTX;
1061 mark_reg_known_zero(env, regs, BPF_REG_1);
1062 }
1063
1064 #define BPF_MAIN_FUNC (-1)
1065 static void init_func_state(struct bpf_verifier_env *env,
1066 struct bpf_func_state *state,
1067 int callsite, int frameno, int subprogno)
1068 {
1069 state->callsite = callsite;
1070 state->frameno = frameno;
1071 state->subprogno = subprogno;
1072 init_reg_state(env, state);
1073 }
1074
1075 enum reg_arg_type {
1076 SRC_OP,
1077 DST_OP,
1078 DST_OP_NO_MARK
1079 };
1080
1081 static int cmp_subprogs(const void *a, const void *b)
1082 {
1083 return ((struct bpf_subprog_info *)a)->start -
1084 ((struct bpf_subprog_info *)b)->start;
1085 }
1086
1087 static int find_subprog(struct bpf_verifier_env *env, int off)
1088 {
1089 struct bpf_subprog_info *p;
1090
1091 p = bsearch(&off, env->subprog_info, env->subprog_cnt,
1092 sizeof(env->subprog_info[0]), cmp_subprogs);
1093 if (!p)
1094 return -ENOENT;
1095 return p - env->subprog_info;
1096
1097 }
1098
1099 static int add_subprog(struct bpf_verifier_env *env, int off)
1100 {
1101 int insn_cnt = env->prog->len;
1102 int ret;
1103
1104 if (off >= insn_cnt || off < 0) {
1105 verbose(env, "call to invalid destination\n");
1106 return -EINVAL;
1107 }
1108 ret = find_subprog(env, off);
1109 if (ret >= 0)
1110 return 0;
1111 if (env->subprog_cnt >= BPF_MAX_SUBPROGS) {
1112 verbose(env, "too many subprograms\n");
1113 return -E2BIG;
1114 }
1115 env->subprog_info[env->subprog_cnt++].start = off;
1116 sort(env->subprog_info, env->subprog_cnt,
1117 sizeof(env->subprog_info[0]), cmp_subprogs, NULL);
1118 return 0;
1119 }
1120
1121 static int check_subprogs(struct bpf_verifier_env *env)
1122 {
1123 int i, ret, subprog_start, subprog_end, off, cur_subprog = 0;
1124 struct bpf_subprog_info *subprog = env->subprog_info;
1125 struct bpf_insn *insn = env->prog->insnsi;
1126 int insn_cnt = env->prog->len;
1127
1128
1129 ret = add_subprog(env, 0);
1130 if (ret < 0)
1131 return ret;
1132
1133
1134 for (i = 0; i < insn_cnt; i++) {
1135 if (insn[i].code != (BPF_JMP | BPF_CALL))
1136 continue;
1137 if (insn[i].src_reg != BPF_PSEUDO_CALL)
1138 continue;
1139 if (!env->allow_ptr_leaks) {
1140 verbose(env, "function calls to other bpf functions are allowed for root only\n");
1141 return -EPERM;
1142 }
1143 ret = add_subprog(env, i + insn[i].imm + 1);
1144 if (ret < 0)
1145 return ret;
1146 }
1147
1148
1149
1150
1151 subprog[env->subprog_cnt].start = insn_cnt;
1152
1153 if (env->log.level & BPF_LOG_LEVEL2)
1154 for (i = 0; i < env->subprog_cnt; i++)
1155 verbose(env, "func#%d @%d\n", i, subprog[i].start);
1156
1157
1158 subprog_start = subprog[cur_subprog].start;
1159 subprog_end = subprog[cur_subprog + 1].start;
1160 for (i = 0; i < insn_cnt; i++) {
1161 u8 code = insn[i].code;
1162
1163 if (BPF_CLASS(code) != BPF_JMP && BPF_CLASS(code) != BPF_JMP32)
1164 goto next;
1165 if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL)
1166 goto next;
1167 off = i + insn[i].off + 1;
1168 if (off < subprog_start || off >= subprog_end) {
1169 verbose(env, "jump out of range from insn %d to %d\n", i, off);
1170 return -EINVAL;
1171 }
1172 next:
1173 if (i == subprog_end - 1) {
1174
1175
1176
1177
1178 if (code != (BPF_JMP | BPF_EXIT) &&
1179 code != (BPF_JMP | BPF_JA)) {
1180 verbose(env, "last insn is not an exit or jmp\n");
1181 return -EINVAL;
1182 }
1183 subprog_start = subprog_end;
1184 cur_subprog++;
1185 if (cur_subprog < env->subprog_cnt)
1186 subprog_end = subprog[cur_subprog + 1].start;
1187 }
1188 }
1189 return 0;
1190 }
1191
1192
1193
1194
1195 static int mark_reg_read(struct bpf_verifier_env *env,
1196 const struct bpf_reg_state *state,
1197 struct bpf_reg_state *parent, u8 flag)
1198 {
1199 bool writes = parent == state->parent;
1200 int cnt = 0;
1201
1202 while (parent) {
1203
1204 if (writes && state->live & REG_LIVE_WRITTEN)
1205 break;
1206 if (parent->live & REG_LIVE_DONE) {
1207 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
1208 reg_type_str[parent->type],
1209 parent->var_off.value, parent->off);
1210 return -EFAULT;
1211 }
1212
1213
1214
1215 if ((parent->live & REG_LIVE_READ) == flag ||
1216 parent->live & REG_LIVE_READ64)
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 break;
1227
1228 parent->live |= flag;
1229
1230 if (flag == REG_LIVE_READ64)
1231 parent->live &= ~REG_LIVE_READ32;
1232 state = parent;
1233 parent = state->parent;
1234 writes = true;
1235 cnt++;
1236 }
1237
1238 if (env->longest_mark_read_walk < cnt)
1239 env->longest_mark_read_walk = cnt;
1240 return 0;
1241 }
1242
1243
1244
1245
1246
1247 static bool is_reg64(struct bpf_verifier_env *env, struct bpf_insn *insn,
1248 u32 regno, struct bpf_reg_state *reg, enum reg_arg_type t)
1249 {
1250 u8 code, class, op;
1251
1252 code = insn->code;
1253 class = BPF_CLASS(code);
1254 op = BPF_OP(code);
1255 if (class == BPF_JMP) {
1256
1257
1258
1259 if (op == BPF_EXIT)
1260 return true;
1261 if (op == BPF_CALL) {
1262
1263
1264
1265
1266
1267 if (insn->src_reg == BPF_PSEUDO_CALL)
1268 return false;
1269
1270
1271
1272 if (t == SRC_OP)
1273 return true;
1274
1275 return false;
1276 }
1277 }
1278
1279 if (class == BPF_ALU64 || class == BPF_JMP ||
1280
1281 (class == BPF_ALU && op == BPF_END && insn->imm == 64))
1282 return true;
1283
1284 if (class == BPF_ALU || class == BPF_JMP32)
1285 return false;
1286
1287 if (class == BPF_LDX) {
1288 if (t != SRC_OP)
1289 return BPF_SIZE(code) == BPF_DW;
1290
1291 return true;
1292 }
1293
1294 if (class == BPF_STX) {
1295 if (reg->type != SCALAR_VALUE)
1296 return true;
1297 return BPF_SIZE(code) == BPF_DW;
1298 }
1299
1300 if (class == BPF_LD) {
1301 u8 mode = BPF_MODE(code);
1302
1303
1304 if (mode == BPF_IMM)
1305 return true;
1306
1307
1308 if (t != SRC_OP)
1309 return false;
1310
1311
1312 if (regno == BPF_REG_6)
1313 return true;
1314
1315
1316 return true;
1317 }
1318
1319 if (class == BPF_ST)
1320
1321 return true;
1322
1323
1324 return true;
1325 }
1326
1327
1328 static bool insn_no_def(struct bpf_insn *insn)
1329 {
1330 u8 class = BPF_CLASS(insn->code);
1331
1332 return (class == BPF_JMP || class == BPF_JMP32 ||
1333 class == BPF_STX || class == BPF_ST);
1334 }
1335
1336
1337 static bool insn_has_def32(struct bpf_verifier_env *env, struct bpf_insn *insn)
1338 {
1339 if (insn_no_def(insn))
1340 return false;
1341
1342 return !is_reg64(env, insn, insn->dst_reg, NULL, DST_OP);
1343 }
1344
1345 static void mark_insn_zext(struct bpf_verifier_env *env,
1346 struct bpf_reg_state *reg)
1347 {
1348 s32 def_idx = reg->subreg_def;
1349
1350 if (def_idx == DEF_NOT_SUBREG)
1351 return;
1352
1353 env->insn_aux_data[def_idx - 1].zext_dst = true;
1354
1355 reg->subreg_def = DEF_NOT_SUBREG;
1356 }
1357
1358 static int check_reg_arg(struct bpf_verifier_env *env, u32 regno,
1359 enum reg_arg_type t)
1360 {
1361 struct bpf_verifier_state *vstate = env->cur_state;
1362 struct bpf_func_state *state = vstate->frame[vstate->curframe];
1363 struct bpf_insn *insn = env->prog->insnsi + env->insn_idx;
1364 struct bpf_reg_state *reg, *regs = state->regs;
1365 bool rw64;
1366
1367 if (regno >= MAX_BPF_REG) {
1368 verbose(env, "R%d is invalid\n", regno);
1369 return -EINVAL;
1370 }
1371
1372 reg = ®s[regno];
1373 rw64 = is_reg64(env, insn, regno, reg, t);
1374 if (t == SRC_OP) {
1375
1376 if (reg->type == NOT_INIT) {
1377 verbose(env, "R%d !read_ok\n", regno);
1378 return -EACCES;
1379 }
1380
1381 if (regno == BPF_REG_FP)
1382 return 0;
1383
1384 if (rw64)
1385 mark_insn_zext(env, reg);
1386
1387 return mark_reg_read(env, reg, reg->parent,
1388 rw64 ? REG_LIVE_READ64 : REG_LIVE_READ32);
1389 } else {
1390
1391 if (regno == BPF_REG_FP) {
1392 verbose(env, "frame pointer is read only\n");
1393 return -EACCES;
1394 }
1395 reg->live |= REG_LIVE_WRITTEN;
1396 reg->subreg_def = rw64 ? DEF_NOT_SUBREG : env->insn_idx + 1;
1397 if (t == DST_OP)
1398 mark_reg_unknown(env, regs, regno);
1399 }
1400 return 0;
1401 }
1402
1403
1404 static int push_jmp_history(struct bpf_verifier_env *env,
1405 struct bpf_verifier_state *cur)
1406 {
1407 u32 cnt = cur->jmp_history_cnt;
1408 struct bpf_idx_pair *p;
1409
1410 cnt++;
1411 p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER);
1412 if (!p)
1413 return -ENOMEM;
1414 p[cnt - 1].idx = env->insn_idx;
1415 p[cnt - 1].prev_idx = env->prev_insn_idx;
1416 cur->jmp_history = p;
1417 cur->jmp_history_cnt = cnt;
1418 return 0;
1419 }
1420
1421
1422
1423
1424 static int get_prev_insn_idx(struct bpf_verifier_state *st, int i,
1425 u32 *history)
1426 {
1427 u32 cnt = *history;
1428
1429 if (cnt && st->jmp_history[cnt - 1].idx == i) {
1430 i = st->jmp_history[cnt - 1].prev_idx;
1431 (*history)--;
1432 } else {
1433 i--;
1434 }
1435 return i;
1436 }
1437
1438
1439
1440
1441
1442 static int backtrack_insn(struct bpf_verifier_env *env, int idx,
1443 u32 *reg_mask, u64 *stack_mask)
1444 {
1445 const struct bpf_insn_cbs cbs = {
1446 .cb_print = verbose,
1447 .private_data = env,
1448 };
1449 struct bpf_insn *insn = env->prog->insnsi + idx;
1450 u8 class = BPF_CLASS(insn->code);
1451 u8 opcode = BPF_OP(insn->code);
1452 u8 mode = BPF_MODE(insn->code);
1453 u32 dreg = 1u << insn->dst_reg;
1454 u32 sreg = 1u << insn->src_reg;
1455 u32 spi;
1456
1457 if (insn->code == 0)
1458 return 0;
1459 if (env->log.level & BPF_LOG_LEVEL) {
1460 verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask);
1461 verbose(env, "%d: ", idx);
1462 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
1463 }
1464
1465 if (class == BPF_ALU || class == BPF_ALU64) {
1466 if (!(*reg_mask & dreg))
1467 return 0;
1468 if (opcode == BPF_MOV) {
1469 if (BPF_SRC(insn->code) == BPF_X) {
1470
1471
1472
1473
1474 *reg_mask &= ~dreg;
1475 *reg_mask |= sreg;
1476 } else {
1477
1478
1479
1480
1481
1482
1483 *reg_mask &= ~dreg;
1484 }
1485 } else {
1486 if (BPF_SRC(insn->code) == BPF_X) {
1487
1488
1489
1490
1491 *reg_mask |= sreg;
1492 }
1493
1494
1495 }
1496 } else if (class == BPF_LDX) {
1497 if (!(*reg_mask & dreg))
1498 return 0;
1499 *reg_mask &= ~dreg;
1500
1501
1502
1503
1504
1505
1506
1507 if (insn->src_reg != BPF_REG_FP)
1508 return 0;
1509 if (BPF_SIZE(insn->code) != BPF_DW)
1510 return 0;
1511
1512
1513
1514
1515
1516 spi = (-insn->off - 1) / BPF_REG_SIZE;
1517 if (spi >= 64) {
1518 verbose(env, "BUG spi %d\n", spi);
1519 WARN_ONCE(1, "verifier backtracking bug");
1520 return -EFAULT;
1521 }
1522 *stack_mask |= 1ull << spi;
1523 } else if (class == BPF_STX || class == BPF_ST) {
1524 if (*reg_mask & dreg)
1525
1526
1527
1528
1529 return -ENOTSUPP;
1530
1531 if (insn->dst_reg != BPF_REG_FP)
1532 return 0;
1533 if (BPF_SIZE(insn->code) != BPF_DW)
1534 return 0;
1535 spi = (-insn->off - 1) / BPF_REG_SIZE;
1536 if (spi >= 64) {
1537 verbose(env, "BUG spi %d\n", spi);
1538 WARN_ONCE(1, "verifier backtracking bug");
1539 return -EFAULT;
1540 }
1541 if (!(*stack_mask & (1ull << spi)))
1542 return 0;
1543 *stack_mask &= ~(1ull << spi);
1544 if (class == BPF_STX)
1545 *reg_mask |= sreg;
1546 } else if (class == BPF_JMP || class == BPF_JMP32) {
1547 if (opcode == BPF_CALL) {
1548 if (insn->src_reg == BPF_PSEUDO_CALL)
1549 return -ENOTSUPP;
1550
1551 *reg_mask &= ~1;
1552 if (*reg_mask & 0x3f) {
1553
1554
1555
1556 verbose(env, "BUG regs %x\n", *reg_mask);
1557 WARN_ONCE(1, "verifier backtracking bug");
1558 return -EFAULT;
1559 }
1560 } else if (opcode == BPF_EXIT) {
1561 return -ENOTSUPP;
1562 }
1563 } else if (class == BPF_LD) {
1564 if (!(*reg_mask & dreg))
1565 return 0;
1566 *reg_mask &= ~dreg;
1567
1568
1569
1570
1571 if (mode == BPF_IND || mode == BPF_ABS)
1572
1573 return -ENOTSUPP;
1574 }
1575 return 0;
1576 }
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630 static void mark_all_scalars_precise(struct bpf_verifier_env *env,
1631 struct bpf_verifier_state *st)
1632 {
1633 struct bpf_func_state *func;
1634 struct bpf_reg_state *reg;
1635 int i, j;
1636
1637
1638
1639
1640 for (; st; st = st->parent)
1641 for (i = 0; i <= st->curframe; i++) {
1642 func = st->frame[i];
1643 for (j = 0; j < BPF_REG_FP; j++) {
1644 reg = &func->regs[j];
1645 if (reg->type != SCALAR_VALUE)
1646 continue;
1647 reg->precise = true;
1648 }
1649 for (j = 0; j < func->allocated_stack / BPF_REG_SIZE; j++) {
1650 if (func->stack[j].slot_type[0] != STACK_SPILL)
1651 continue;
1652 reg = &func->stack[j].spilled_ptr;
1653 if (reg->type != SCALAR_VALUE)
1654 continue;
1655 reg->precise = true;
1656 }
1657 }
1658 }
1659
1660 static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
1661 int spi)
1662 {
1663 struct bpf_verifier_state *st = env->cur_state;
1664 int first_idx = st->first_insn_idx;
1665 int last_idx = env->insn_idx;
1666 struct bpf_func_state *func;
1667 struct bpf_reg_state *reg;
1668 u32 reg_mask = regno >= 0 ? 1u << regno : 0;
1669 u64 stack_mask = spi >= 0 ? 1ull << spi : 0;
1670 bool skip_first = true;
1671 bool new_marks = false;
1672 int i, err;
1673
1674 if (!env->allow_ptr_leaks)
1675
1676 return 0;
1677
1678 func = st->frame[st->curframe];
1679 if (regno >= 0) {
1680 reg = &func->regs[regno];
1681 if (reg->type != SCALAR_VALUE) {
1682 WARN_ONCE(1, "backtracing misuse");
1683 return -EFAULT;
1684 }
1685 if (!reg->precise)
1686 new_marks = true;
1687 else
1688 reg_mask = 0;
1689 reg->precise = true;
1690 }
1691
1692 while (spi >= 0) {
1693 if (func->stack[spi].slot_type[0] != STACK_SPILL) {
1694 stack_mask = 0;
1695 break;
1696 }
1697 reg = &func->stack[spi].spilled_ptr;
1698 if (reg->type != SCALAR_VALUE) {
1699 stack_mask = 0;
1700 break;
1701 }
1702 if (!reg->precise)
1703 new_marks = true;
1704 else
1705 stack_mask = 0;
1706 reg->precise = true;
1707 break;
1708 }
1709
1710 if (!new_marks)
1711 return 0;
1712 if (!reg_mask && !stack_mask)
1713 return 0;
1714 for (;;) {
1715 DECLARE_BITMAP(mask, 64);
1716 u32 history = st->jmp_history_cnt;
1717
1718 if (env->log.level & BPF_LOG_LEVEL)
1719 verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx);
1720 for (i = last_idx;;) {
1721 if (skip_first) {
1722 err = 0;
1723 skip_first = false;
1724 } else {
1725 err = backtrack_insn(env, i, ®_mask, &stack_mask);
1726 }
1727 if (err == -ENOTSUPP) {
1728 mark_all_scalars_precise(env, st);
1729 return 0;
1730 } else if (err) {
1731 return err;
1732 }
1733 if (!reg_mask && !stack_mask)
1734
1735
1736
1737
1738 return 0;
1739 if (i == first_idx)
1740 break;
1741 i = get_prev_insn_idx(st, i, &history);
1742 if (i >= env->prog->len) {
1743
1744
1745
1746
1747
1748
1749 verbose(env, "BUG backtracking idx %d\n", i);
1750 WARN_ONCE(1, "verifier backtracking bug");
1751 return -EFAULT;
1752 }
1753 }
1754 st = st->parent;
1755 if (!st)
1756 break;
1757
1758 new_marks = false;
1759 func = st->frame[st->curframe];
1760 bitmap_from_u64(mask, reg_mask);
1761 for_each_set_bit(i, mask, 32) {
1762 reg = &func->regs[i];
1763 if (reg->type != SCALAR_VALUE) {
1764 reg_mask &= ~(1u << i);
1765 continue;
1766 }
1767 if (!reg->precise)
1768 new_marks = true;
1769 reg->precise = true;
1770 }
1771
1772 bitmap_from_u64(mask, stack_mask);
1773 for_each_set_bit(i, mask, 64) {
1774 if (i >= func->allocated_stack / BPF_REG_SIZE) {
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788 mark_all_scalars_precise(env, st);
1789 return 0;
1790 }
1791
1792 if (func->stack[i].slot_type[0] != STACK_SPILL) {
1793 stack_mask &= ~(1ull << i);
1794 continue;
1795 }
1796 reg = &func->stack[i].spilled_ptr;
1797 if (reg->type != SCALAR_VALUE) {
1798 stack_mask &= ~(1ull << i);
1799 continue;
1800 }
1801 if (!reg->precise)
1802 new_marks = true;
1803 reg->precise = true;
1804 }
1805 if (env->log.level & BPF_LOG_LEVEL) {
1806 print_verifier_state(env, func);
1807 verbose(env, "parent %s regs=%x stack=%llx marks\n",
1808 new_marks ? "didn't have" : "already had",
1809 reg_mask, stack_mask);
1810 }
1811
1812 if (!reg_mask && !stack_mask)
1813 break;
1814 if (!new_marks)
1815 break;
1816
1817 last_idx = st->last_insn_idx;
1818 first_idx = st->first_insn_idx;
1819 }
1820 return 0;
1821 }
1822
1823 static int mark_chain_precision(struct bpf_verifier_env *env, int regno)
1824 {
1825 return __mark_chain_precision(env, regno, -1);
1826 }
1827
1828 static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi)
1829 {
1830 return __mark_chain_precision(env, -1, spi);
1831 }
1832
1833 static bool is_spillable_regtype(enum bpf_reg_type type)
1834 {
1835 switch (type) {
1836 case PTR_TO_MAP_VALUE:
1837 case PTR_TO_MAP_VALUE_OR_NULL:
1838 case PTR_TO_STACK:
1839 case PTR_TO_CTX:
1840 case PTR_TO_PACKET:
1841 case PTR_TO_PACKET_META:
1842 case PTR_TO_PACKET_END:
1843 case PTR_TO_FLOW_KEYS:
1844 case CONST_PTR_TO_MAP:
1845 case PTR_TO_SOCKET:
1846 case PTR_TO_SOCKET_OR_NULL:
1847 case PTR_TO_SOCK_COMMON:
1848 case PTR_TO_SOCK_COMMON_OR_NULL:
1849 case PTR_TO_TCP_SOCK:
1850 case PTR_TO_TCP_SOCK_OR_NULL:
1851 case PTR_TO_XDP_SOCK:
1852 return true;
1853 default:
1854 return false;
1855 }
1856 }
1857
1858
1859 static bool register_is_null(struct bpf_reg_state *reg)
1860 {
1861 return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0);
1862 }
1863
1864 static bool register_is_const(struct bpf_reg_state *reg)
1865 {
1866 return reg->type == SCALAR_VALUE && tnum_is_const(reg->var_off);
1867 }
1868
1869 static bool __is_pointer_value(bool allow_ptr_leaks,
1870 const struct bpf_reg_state *reg)
1871 {
1872 if (allow_ptr_leaks)
1873 return false;
1874
1875 return reg->type != SCALAR_VALUE;
1876 }
1877
1878 static void save_register_state(struct bpf_func_state *state,
1879 int spi, struct bpf_reg_state *reg)
1880 {
1881 int i;
1882
1883 state->stack[spi].spilled_ptr = *reg;
1884 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
1885
1886 for (i = 0; i < BPF_REG_SIZE; i++)
1887 state->stack[spi].slot_type[i] = STACK_SPILL;
1888 }
1889
1890
1891
1892
1893 static int check_stack_write(struct bpf_verifier_env *env,
1894 struct bpf_func_state *state,
1895 int off, int size, int value_regno, int insn_idx)
1896 {
1897 struct bpf_func_state *cur;
1898 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err;
1899 u32 dst_reg = env->prog->insnsi[insn_idx].dst_reg;
1900 struct bpf_reg_state *reg = NULL;
1901
1902 err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE),
1903 state->acquired_refs, true);
1904 if (err)
1905 return err;
1906
1907
1908
1909 if (!env->allow_ptr_leaks &&
1910 state->stack[spi].slot_type[0] == STACK_SPILL &&
1911 size != BPF_REG_SIZE) {
1912 verbose(env, "attempt to corrupt spilled pointer on stack\n");
1913 return -EACCES;
1914 }
1915
1916 cur = env->cur_state->frame[env->cur_state->curframe];
1917 if (value_regno >= 0)
1918 reg = &cur->regs[value_regno];
1919
1920 if (reg && size == BPF_REG_SIZE && register_is_const(reg) &&
1921 !register_is_null(reg) && env->allow_ptr_leaks) {
1922 if (dst_reg != BPF_REG_FP) {
1923
1924
1925
1926
1927
1928
1929 err = mark_chain_precision(env, value_regno);
1930 if (err)
1931 return err;
1932 }
1933 save_register_state(state, spi, reg);
1934 } else if (reg && is_spillable_regtype(reg->type)) {
1935
1936 if (size != BPF_REG_SIZE) {
1937 verbose_linfo(env, insn_idx, "; ");
1938 verbose(env, "invalid size of register spill\n");
1939 return -EACCES;
1940 }
1941
1942 if (state != cur && reg->type == PTR_TO_STACK) {
1943 verbose(env, "cannot spill pointers to stack into stack frame of the caller\n");
1944 return -EINVAL;
1945 }
1946
1947 if (!env->allow_ptr_leaks) {
1948 bool sanitize = false;
1949
1950 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
1951 register_is_const(&state->stack[spi].spilled_ptr))
1952 sanitize = true;
1953 for (i = 0; i < BPF_REG_SIZE; i++)
1954 if (state->stack[spi].slot_type[i] == STACK_MISC) {
1955 sanitize = true;
1956 break;
1957 }
1958 if (sanitize) {
1959 int *poff = &env->insn_aux_data[insn_idx].sanitize_stack_off;
1960 int soff = (-spi - 1) * BPF_REG_SIZE;
1961
1962
1963
1964
1965
1966
1967
1968
1969 if (*poff && *poff != soff) {
1970
1971
1972
1973
1974 verbose(env,
1975 "insn %d cannot access two stack slots fp%d and fp%d",
1976 insn_idx, *poff, soff);
1977 return -EINVAL;
1978 }
1979 *poff = soff;
1980 }
1981 }
1982 save_register_state(state, spi, reg);
1983 } else {
1984 u8 type = STACK_MISC;
1985
1986
1987 state->stack[spi].spilled_ptr.type = NOT_INIT;
1988
1989 if (state->stack[spi].slot_type[0] == STACK_SPILL)
1990 for (i = 0; i < BPF_REG_SIZE; i++)
1991 state->stack[spi].slot_type[i] = STACK_MISC;
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001 if (size == BPF_REG_SIZE)
2002 state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
2003
2004
2005 if (reg && register_is_null(reg)) {
2006
2007 err = mark_chain_precision(env, value_regno);
2008 if (err)
2009 return err;
2010 type = STACK_ZERO;
2011 }
2012
2013
2014 for (i = 0; i < size; i++)
2015 state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] =
2016 type;
2017 }
2018 return 0;
2019 }
2020
2021 static int check_stack_read(struct bpf_verifier_env *env,
2022 struct bpf_func_state *reg_state ,
2023 int off, int size, int value_regno)
2024 {
2025 struct bpf_verifier_state *vstate = env->cur_state;
2026 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2027 int i, slot = -off - 1, spi = slot / BPF_REG_SIZE;
2028 struct bpf_reg_state *reg;
2029 u8 *stype;
2030
2031 if (reg_state->allocated_stack <= slot) {
2032 verbose(env, "invalid read from stack off %d+0 size %d\n",
2033 off, size);
2034 return -EACCES;
2035 }
2036 stype = reg_state->stack[spi].slot_type;
2037 reg = ®_state->stack[spi].spilled_ptr;
2038
2039 if (stype[0] == STACK_SPILL) {
2040 if (size != BPF_REG_SIZE) {
2041 if (reg->type != SCALAR_VALUE) {
2042 verbose_linfo(env, env->insn_idx, "; ");
2043 verbose(env, "invalid size of register fill\n");
2044 return -EACCES;
2045 }
2046 if (value_regno >= 0) {
2047 mark_reg_unknown(env, state->regs, value_regno);
2048 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2049 }
2050 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2051 return 0;
2052 }
2053 for (i = 1; i < BPF_REG_SIZE; i++) {
2054 if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) {
2055 verbose(env, "corrupted spill memory\n");
2056 return -EACCES;
2057 }
2058 }
2059
2060 if (value_regno >= 0) {
2061
2062 state->regs[value_regno] = *reg;
2063
2064
2065
2066
2067 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2068 } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) {
2069
2070
2071
2072
2073
2074
2075 verbose(env, "leaking pointer from stack off %d\n",
2076 off);
2077 return -EACCES;
2078 }
2079 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2080 } else {
2081 int zeros = 0;
2082
2083 for (i = 0; i < size; i++) {
2084 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC)
2085 continue;
2086 if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) {
2087 zeros++;
2088 continue;
2089 }
2090 verbose(env, "invalid read from stack off %d+%d size %d\n",
2091 off, i, size);
2092 return -EACCES;
2093 }
2094 mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64);
2095 if (value_regno >= 0) {
2096 if (zeros == size) {
2097
2098
2099
2100 __mark_reg_const_zero(&state->regs[value_regno]);
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111 state->regs[value_regno].precise = true;
2112 } else {
2113
2114 mark_reg_unknown(env, state->regs, value_regno);
2115 }
2116 state->regs[value_regno].live |= REG_LIVE_WRITTEN;
2117 }
2118 }
2119 return 0;
2120 }
2121
2122 static int check_stack_access(struct bpf_verifier_env *env,
2123 const struct bpf_reg_state *reg,
2124 int off, int size)
2125 {
2126
2127
2128
2129
2130 if (!tnum_is_const(reg->var_off)) {
2131 char tn_buf[48];
2132
2133 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2134 verbose(env, "variable stack access var_off=%s off=%d size=%d\n",
2135 tn_buf, off, size);
2136 return -EACCES;
2137 }
2138
2139 if (off >= 0 || off < -MAX_BPF_STACK) {
2140 verbose(env, "invalid stack off=%d size=%d\n", off, size);
2141 return -EACCES;
2142 }
2143
2144 return 0;
2145 }
2146
2147 static int check_map_access_type(struct bpf_verifier_env *env, u32 regno,
2148 int off, int size, enum bpf_access_type type)
2149 {
2150 struct bpf_reg_state *regs = cur_regs(env);
2151 struct bpf_map *map = regs[regno].map_ptr;
2152 u32 cap = bpf_map_flags_to_cap(map);
2153
2154 if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) {
2155 verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n",
2156 map->value_size, off, size);
2157 return -EACCES;
2158 }
2159
2160 if (type == BPF_READ && !(cap & BPF_MAP_CAN_READ)) {
2161 verbose(env, "read from map forbidden, value_size=%d off=%d size=%d\n",
2162 map->value_size, off, size);
2163 return -EACCES;
2164 }
2165
2166 return 0;
2167 }
2168
2169
2170 static int __check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
2171 int size, bool zero_size_allowed)
2172 {
2173 struct bpf_reg_state *regs = cur_regs(env);
2174 struct bpf_map *map = regs[regno].map_ptr;
2175
2176 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
2177 off + size > map->value_size) {
2178 verbose(env, "invalid access to map value, value_size=%d off=%d size=%d\n",
2179 map->value_size, off, size);
2180 return -EACCES;
2181 }
2182 return 0;
2183 }
2184
2185
2186 static int check_map_access(struct bpf_verifier_env *env, u32 regno,
2187 int off, int size, bool zero_size_allowed)
2188 {
2189 struct bpf_verifier_state *vstate = env->cur_state;
2190 struct bpf_func_state *state = vstate->frame[vstate->curframe];
2191 struct bpf_reg_state *reg = &state->regs[regno];
2192 int err;
2193
2194
2195
2196
2197
2198 if (env->log.level & BPF_LOG_LEVEL)
2199 print_verifier_state(env, state);
2200
2201
2202
2203
2204
2205
2206
2207 if (reg->smin_value < 0 &&
2208 (reg->smin_value == S64_MIN ||
2209 (off + reg->smin_value != (s64)(s32)(off + reg->smin_value)) ||
2210 reg->smin_value + off < 0)) {
2211 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2212 regno);
2213 return -EACCES;
2214 }
2215 err = __check_map_access(env, regno, reg->smin_value + off, size,
2216 zero_size_allowed);
2217 if (err) {
2218 verbose(env, "R%d min value is outside of the array range\n",
2219 regno);
2220 return err;
2221 }
2222
2223
2224
2225
2226
2227 if (reg->umax_value >= BPF_MAX_VAR_OFF) {
2228 verbose(env, "R%d unbounded memory access, make sure to bounds check any array access into a map\n",
2229 regno);
2230 return -EACCES;
2231 }
2232 err = __check_map_access(env, regno, reg->umax_value + off, size,
2233 zero_size_allowed);
2234 if (err)
2235 verbose(env, "R%d max value is outside of the array range\n",
2236 regno);
2237
2238 if (map_value_has_spin_lock(reg->map_ptr)) {
2239 u32 lock = reg->map_ptr->spin_lock_off;
2240
2241
2242
2243
2244
2245
2246 if (reg->smin_value + off < lock + sizeof(struct bpf_spin_lock) &&
2247 lock < reg->umax_value + off + size) {
2248 verbose(env, "bpf_spin_lock cannot be accessed directly by load/store\n");
2249 return -EACCES;
2250 }
2251 }
2252 return err;
2253 }
2254
2255 #define MAX_PACKET_OFF 0xffff
2256
2257 static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
2258 const struct bpf_call_arg_meta *meta,
2259 enum bpf_access_type t)
2260 {
2261 switch (env->prog->type) {
2262
2263 case BPF_PROG_TYPE_LWT_IN:
2264 case BPF_PROG_TYPE_LWT_OUT:
2265 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2266 case BPF_PROG_TYPE_SK_REUSEPORT:
2267 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2268 case BPF_PROG_TYPE_CGROUP_SKB:
2269 if (t == BPF_WRITE)
2270 return false;
2271
2272
2273
2274 case BPF_PROG_TYPE_SCHED_CLS:
2275 case BPF_PROG_TYPE_SCHED_ACT:
2276 case BPF_PROG_TYPE_XDP:
2277 case BPF_PROG_TYPE_LWT_XMIT:
2278 case BPF_PROG_TYPE_SK_SKB:
2279 case BPF_PROG_TYPE_SK_MSG:
2280 if (meta)
2281 return meta->pkt_access;
2282
2283 env->seen_direct_write = true;
2284 return true;
2285
2286 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2287 if (t == BPF_WRITE)
2288 env->seen_direct_write = true;
2289
2290 return true;
2291
2292 default:
2293 return false;
2294 }
2295 }
2296
2297 static int __check_packet_access(struct bpf_verifier_env *env, u32 regno,
2298 int off, int size, bool zero_size_allowed)
2299 {
2300 struct bpf_reg_state *regs = cur_regs(env);
2301 struct bpf_reg_state *reg = ®s[regno];
2302
2303 if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) ||
2304 (u64)off + size > reg->range) {
2305 verbose(env, "invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n",
2306 off, size, regno, reg->id, reg->off, reg->range);
2307 return -EACCES;
2308 }
2309 return 0;
2310 }
2311
2312 static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,
2313 int size, bool zero_size_allowed)
2314 {
2315 struct bpf_reg_state *regs = cur_regs(env);
2316 struct bpf_reg_state *reg = ®s[regno];
2317 int err;
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327 if (reg->smin_value < 0) {
2328 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2329 regno);
2330 return -EACCES;
2331 }
2332 err = __check_packet_access(env, regno, off, size, zero_size_allowed);
2333 if (err) {
2334 verbose(env, "R%d offset is outside of the packet\n", regno);
2335 return err;
2336 }
2337
2338
2339
2340
2341
2342
2343
2344 env->prog->aux->max_pkt_offset =
2345 max_t(u32, env->prog->aux->max_pkt_offset,
2346 off + reg->umax_value + size - 1);
2347
2348 return err;
2349 }
2350
2351
2352 static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, int size,
2353 enum bpf_access_type t, enum bpf_reg_type *reg_type)
2354 {
2355 struct bpf_insn_access_aux info = {
2356 .reg_type = *reg_type,
2357 };
2358
2359 if (env->ops->is_valid_access &&
2360 env->ops->is_valid_access(off, size, t, env->prog, &info)) {
2361
2362
2363
2364
2365
2366
2367
2368 *reg_type = info.reg_type;
2369
2370 env->insn_aux_data[insn_idx].ctx_field_size = info.ctx_field_size;
2371
2372 if (env->prog->aux->max_ctx_offset < off + size)
2373 env->prog->aux->max_ctx_offset = off + size;
2374 return 0;
2375 }
2376
2377 verbose(env, "invalid bpf_context access off=%d size=%d\n", off, size);
2378 return -EACCES;
2379 }
2380
2381 static int check_flow_keys_access(struct bpf_verifier_env *env, int off,
2382 int size)
2383 {
2384 if (size < 0 || off < 0 ||
2385 (u64)off + size > sizeof(struct bpf_flow_keys)) {
2386 verbose(env, "invalid access to flow keys off=%d size=%d\n",
2387 off, size);
2388 return -EACCES;
2389 }
2390 return 0;
2391 }
2392
2393 static int check_sock_access(struct bpf_verifier_env *env, int insn_idx,
2394 u32 regno, int off, int size,
2395 enum bpf_access_type t)
2396 {
2397 struct bpf_reg_state *regs = cur_regs(env);
2398 struct bpf_reg_state *reg = ®s[regno];
2399 struct bpf_insn_access_aux info = {};
2400 bool valid;
2401
2402 if (reg->smin_value < 0) {
2403 verbose(env, "R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n",
2404 regno);
2405 return -EACCES;
2406 }
2407
2408 switch (reg->type) {
2409 case PTR_TO_SOCK_COMMON:
2410 valid = bpf_sock_common_is_valid_access(off, size, t, &info);
2411 break;
2412 case PTR_TO_SOCKET:
2413 valid = bpf_sock_is_valid_access(off, size, t, &info);
2414 break;
2415 case PTR_TO_TCP_SOCK:
2416 valid = bpf_tcp_sock_is_valid_access(off, size, t, &info);
2417 break;
2418 case PTR_TO_XDP_SOCK:
2419 valid = bpf_xdp_sock_is_valid_access(off, size, t, &info);
2420 break;
2421 default:
2422 valid = false;
2423 }
2424
2425
2426 if (valid) {
2427 env->insn_aux_data[insn_idx].ctx_field_size =
2428 info.ctx_field_size;
2429 return 0;
2430 }
2431
2432 verbose(env, "R%d invalid %s access off=%d size=%d\n",
2433 regno, reg_type_str[reg->type], off, size);
2434
2435 return -EACCES;
2436 }
2437
2438 static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)
2439 {
2440 return cur_regs(env) + regno;
2441 }
2442
2443 static bool is_pointer_value(struct bpf_verifier_env *env, int regno)
2444 {
2445 return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno));
2446 }
2447
2448 static bool is_ctx_reg(struct bpf_verifier_env *env, int regno)
2449 {
2450 const struct bpf_reg_state *reg = reg_state(env, regno);
2451
2452 return reg->type == PTR_TO_CTX;
2453 }
2454
2455 static bool is_sk_reg(struct bpf_verifier_env *env, int regno)
2456 {
2457 const struct bpf_reg_state *reg = reg_state(env, regno);
2458
2459 return type_is_sk_pointer(reg->type);
2460 }
2461
2462 static bool is_pkt_reg(struct bpf_verifier_env *env, int regno)
2463 {
2464 const struct bpf_reg_state *reg = reg_state(env, regno);
2465
2466 return type_is_pkt_pointer(reg->type);
2467 }
2468
2469 static bool is_flow_key_reg(struct bpf_verifier_env *env, int regno)
2470 {
2471 const struct bpf_reg_state *reg = reg_state(env, regno);
2472
2473
2474 return reg->type == PTR_TO_FLOW_KEYS;
2475 }
2476
2477 static int check_pkt_ptr_alignment(struct bpf_verifier_env *env,
2478 const struct bpf_reg_state *reg,
2479 int off, int size, bool strict)
2480 {
2481 struct tnum reg_off;
2482 int ip_align;
2483
2484
2485 if (!strict || size == 1)
2486 return 0;
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496 ip_align = 2;
2497
2498 reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off));
2499 if (!tnum_is_aligned(reg_off, size)) {
2500 char tn_buf[48];
2501
2502 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2503 verbose(env,
2504 "misaligned packet access off %d+%s+%d+%d size %d\n",
2505 ip_align, tn_buf, reg->off, off, size);
2506 return -EACCES;
2507 }
2508
2509 return 0;
2510 }
2511
2512 static int check_generic_ptr_alignment(struct bpf_verifier_env *env,
2513 const struct bpf_reg_state *reg,
2514 const char *pointer_desc,
2515 int off, int size, bool strict)
2516 {
2517 struct tnum reg_off;
2518
2519
2520 if (!strict || size == 1)
2521 return 0;
2522
2523 reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off));
2524 if (!tnum_is_aligned(reg_off, size)) {
2525 char tn_buf[48];
2526
2527 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2528 verbose(env, "misaligned %saccess off %s+%d+%d size %d\n",
2529 pointer_desc, tn_buf, reg->off, off, size);
2530 return -EACCES;
2531 }
2532
2533 return 0;
2534 }
2535
2536 static int check_ptr_alignment(struct bpf_verifier_env *env,
2537 const struct bpf_reg_state *reg, int off,
2538 int size, bool strict_alignment_once)
2539 {
2540 bool strict = env->strict_alignment || strict_alignment_once;
2541 const char *pointer_desc = "";
2542
2543 switch (reg->type) {
2544 case PTR_TO_PACKET:
2545 case PTR_TO_PACKET_META:
2546
2547
2548
2549 return check_pkt_ptr_alignment(env, reg, off, size, strict);
2550 case PTR_TO_FLOW_KEYS:
2551 pointer_desc = "flow keys ";
2552 break;
2553 case PTR_TO_MAP_VALUE:
2554 pointer_desc = "value ";
2555 break;
2556 case PTR_TO_CTX:
2557 pointer_desc = "context ";
2558 break;
2559 case PTR_TO_STACK:
2560 pointer_desc = "stack ";
2561
2562
2563
2564
2565 strict = true;
2566 break;
2567 case PTR_TO_SOCKET:
2568 pointer_desc = "sock ";
2569 break;
2570 case PTR_TO_SOCK_COMMON:
2571 pointer_desc = "sock_common ";
2572 break;
2573 case PTR_TO_TCP_SOCK:
2574 pointer_desc = "tcp_sock ";
2575 break;
2576 case PTR_TO_XDP_SOCK:
2577 pointer_desc = "xdp_sock ";
2578 break;
2579 default:
2580 break;
2581 }
2582 return check_generic_ptr_alignment(env, reg, pointer_desc, off, size,
2583 strict);
2584 }
2585
2586 static int update_stack_depth(struct bpf_verifier_env *env,
2587 const struct bpf_func_state *func,
2588 int off)
2589 {
2590 u16 stack = env->subprog_info[func->subprogno].stack_depth;
2591
2592 if (stack >= -off)
2593 return 0;
2594
2595
2596 env->subprog_info[func->subprogno].stack_depth = -off;
2597 return 0;
2598 }
2599
2600
2601
2602
2603
2604
2605
2606 static int check_max_stack_depth(struct bpf_verifier_env *env)
2607 {
2608 int depth = 0, frame = 0, idx = 0, i = 0, subprog_end;
2609 struct bpf_subprog_info *subprog = env->subprog_info;
2610 struct bpf_insn *insn = env->prog->insnsi;
2611 int ret_insn[MAX_CALL_FRAMES];
2612 int ret_prog[MAX_CALL_FRAMES];
2613
2614 process_func:
2615
2616
2617
2618 depth += round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
2619 if (depth > MAX_BPF_STACK) {
2620 verbose(env, "combined stack size of %d calls is %d. Too large\n",
2621 frame + 1, depth);
2622 return -EACCES;
2623 }
2624 continue_func:
2625 subprog_end = subprog[idx + 1].start;
2626 for (; i < subprog_end; i++) {
2627 if (insn[i].code != (BPF_JMP | BPF_CALL))
2628 continue;
2629 if (insn[i].src_reg != BPF_PSEUDO_CALL)
2630 continue;
2631
2632 ret_insn[frame] = i + 1;
2633 ret_prog[frame] = idx;
2634
2635
2636 i = i + insn[i].imm + 1;
2637 idx = find_subprog(env, i);
2638 if (idx < 0) {
2639 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2640 i);
2641 return -EFAULT;
2642 }
2643 frame++;
2644 if (frame >= MAX_CALL_FRAMES) {
2645 verbose(env, "the call stack of %d frames is too deep !\n",
2646 frame);
2647 return -E2BIG;
2648 }
2649 goto process_func;
2650 }
2651
2652
2653
2654 if (frame == 0)
2655 return 0;
2656 depth -= round_up(max_t(u32, subprog[idx].stack_depth, 1), 32);
2657 frame--;
2658 i = ret_insn[frame];
2659 idx = ret_prog[frame];
2660 goto continue_func;
2661 }
2662
2663 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
2664 static int get_callee_stack_depth(struct bpf_verifier_env *env,
2665 const struct bpf_insn *insn, int idx)
2666 {
2667 int start = idx + insn->imm + 1, subprog;
2668
2669 subprog = find_subprog(env, start);
2670 if (subprog < 0) {
2671 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
2672 start);
2673 return -EFAULT;
2674 }
2675 return env->subprog_info[subprog].stack_depth;
2676 }
2677 #endif
2678
2679 static int check_ctx_reg(struct bpf_verifier_env *env,
2680 const struct bpf_reg_state *reg, int regno)
2681 {
2682
2683
2684
2685
2686 if (reg->off) {
2687 verbose(env, "dereference of modified ctx ptr R%d off=%d disallowed\n",
2688 regno, reg->off);
2689 return -EACCES;
2690 }
2691
2692 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2693 char tn_buf[48];
2694
2695 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2696 verbose(env, "variable ctx access var_off=%s disallowed\n", tn_buf);
2697 return -EACCES;
2698 }
2699
2700 return 0;
2701 }
2702
2703 static int check_tp_buffer_access(struct bpf_verifier_env *env,
2704 const struct bpf_reg_state *reg,
2705 int regno, int off, int size)
2706 {
2707 if (off < 0) {
2708 verbose(env,
2709 "R%d invalid tracepoint buffer access: off=%d, size=%d",
2710 regno, off, size);
2711 return -EACCES;
2712 }
2713 if (!tnum_is_const(reg->var_off) || reg->var_off.value) {
2714 char tn_buf[48];
2715
2716 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2717 verbose(env,
2718 "R%d invalid variable buffer offset: off=%d, var_off=%s",
2719 regno, off, tn_buf);
2720 return -EACCES;
2721 }
2722 if (off + size > env->prog->aux->max_tp_access)
2723 env->prog->aux->max_tp_access = off + size;
2724
2725 return 0;
2726 }
2727
2728
2729
2730
2731
2732 static void coerce_reg_to_size(struct bpf_reg_state *reg, int size)
2733 {
2734 u64 mask;
2735
2736
2737 reg->var_off = tnum_cast(reg->var_off, size);
2738
2739
2740 mask = ((u64)1 << (size * 8)) - 1;
2741 if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) {
2742 reg->umin_value &= mask;
2743 reg->umax_value &= mask;
2744 } else {
2745 reg->umin_value = 0;
2746 reg->umax_value = mask;
2747 }
2748 reg->smin_value = reg->umin_value;
2749 reg->smax_value = reg->umax_value;
2750 }
2751
2752
2753
2754
2755
2756
2757
2758 static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno,
2759 int off, int bpf_size, enum bpf_access_type t,
2760 int value_regno, bool strict_alignment_once)
2761 {
2762 struct bpf_reg_state *regs = cur_regs(env);
2763 struct bpf_reg_state *reg = regs + regno;
2764 struct bpf_func_state *state;
2765 int size, err = 0;
2766
2767 size = bpf_size_to_bytes(bpf_size);
2768 if (size < 0)
2769 return size;
2770
2771
2772 err = check_ptr_alignment(env, reg, off, size, strict_alignment_once);
2773 if (err)
2774 return err;
2775
2776
2777 off += reg->off;
2778
2779 if (reg->type == PTR_TO_MAP_VALUE) {
2780 if (t == BPF_WRITE && value_regno >= 0 &&
2781 is_pointer_value(env, value_regno)) {
2782 verbose(env, "R%d leaks addr into map\n", value_regno);
2783 return -EACCES;
2784 }
2785 err = check_map_access_type(env, regno, off, size, t);
2786 if (err)
2787 return err;
2788 err = check_map_access(env, regno, off, size, false);
2789 if (!err && t == BPF_READ && value_regno >= 0)
2790 mark_reg_unknown(env, regs, value_regno);
2791
2792 } else if (reg->type == PTR_TO_CTX) {
2793 enum bpf_reg_type reg_type = SCALAR_VALUE;
2794
2795 if (t == BPF_WRITE && value_regno >= 0 &&
2796 is_pointer_value(env, value_regno)) {
2797 verbose(env, "R%d leaks addr into ctx\n", value_regno);
2798 return -EACCES;
2799 }
2800
2801 err = check_ctx_reg(env, reg, regno);
2802 if (err < 0)
2803 return err;
2804
2805 err = check_ctx_access(env, insn_idx, off, size, t, ®_type);
2806 if (!err && t == BPF_READ && value_regno >= 0) {
2807
2808
2809
2810
2811 if (reg_type == SCALAR_VALUE) {
2812 mark_reg_unknown(env, regs, value_regno);
2813 } else {
2814 mark_reg_known_zero(env, regs,
2815 value_regno);
2816 if (reg_type_may_be_null(reg_type))
2817 regs[value_regno].id = ++env->id_gen;
2818
2819
2820
2821
2822
2823 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
2824 }
2825 regs[value_regno].type = reg_type;
2826 }
2827
2828 } else if (reg->type == PTR_TO_STACK) {
2829 off += reg->var_off.value;
2830 err = check_stack_access(env, reg, off, size);
2831 if (err)
2832 return err;
2833
2834 state = func(env, reg);
2835 err = update_stack_depth(env, state, off);
2836 if (err)
2837 return err;
2838
2839 if (t == BPF_WRITE)
2840 err = check_stack_write(env, state, off, size,
2841 value_regno, insn_idx);
2842 else
2843 err = check_stack_read(env, state, off, size,
2844 value_regno);
2845 } else if (reg_is_pkt_pointer(reg)) {
2846 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
2847 verbose(env, "cannot write into packet\n");
2848 return -EACCES;
2849 }
2850 if (t == BPF_WRITE && value_regno >= 0 &&
2851 is_pointer_value(env, value_regno)) {
2852 verbose(env, "R%d leaks addr into packet\n",
2853 value_regno);
2854 return -EACCES;
2855 }
2856 err = check_packet_access(env, regno, off, size, false);
2857 if (!err && t == BPF_READ && value_regno >= 0)
2858 mark_reg_unknown(env, regs, value_regno);
2859 } else if (reg->type == PTR_TO_FLOW_KEYS) {
2860 if (t == BPF_WRITE && value_regno >= 0 &&
2861 is_pointer_value(env, value_regno)) {
2862 verbose(env, "R%d leaks addr into flow keys\n",
2863 value_regno);
2864 return -EACCES;
2865 }
2866
2867 err = check_flow_keys_access(env, off, size);
2868 if (!err && t == BPF_READ && value_regno >= 0)
2869 mark_reg_unknown(env, regs, value_regno);
2870 } else if (type_is_sk_pointer(reg->type)) {
2871 if (t == BPF_WRITE) {
2872 verbose(env, "R%d cannot write into %s\n",
2873 regno, reg_type_str[reg->type]);
2874 return -EACCES;
2875 }
2876 err = check_sock_access(env, insn_idx, regno, off, size, t);
2877 if (!err && value_regno >= 0)
2878 mark_reg_unknown(env, regs, value_regno);
2879 } else if (reg->type == PTR_TO_TP_BUFFER) {
2880 err = check_tp_buffer_access(env, reg, regno, off, size);
2881 if (!err && t == BPF_READ && value_regno >= 0)
2882 mark_reg_unknown(env, regs, value_regno);
2883 } else {
2884 verbose(env, "R%d invalid mem access '%s'\n", regno,
2885 reg_type_str[reg->type]);
2886 return -EACCES;
2887 }
2888
2889 if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ &&
2890 regs[value_regno].type == SCALAR_VALUE) {
2891
2892 coerce_reg_to_size(®s[value_regno], size);
2893 }
2894 return err;
2895 }
2896
2897 static int check_xadd(struct bpf_verifier_env *env, int insn_idx, struct bpf_insn *insn)
2898 {
2899 int err;
2900
2901 if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) ||
2902 insn->imm != 0) {
2903 verbose(env, "BPF_XADD uses reserved fields\n");
2904 return -EINVAL;
2905 }
2906
2907
2908 err = check_reg_arg(env, insn->src_reg, SRC_OP);
2909 if (err)
2910 return err;
2911
2912
2913 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
2914 if (err)
2915 return err;
2916
2917 if (is_pointer_value(env, insn->src_reg)) {
2918 verbose(env, "R%d leaks addr into mem\n", insn->src_reg);
2919 return -EACCES;
2920 }
2921
2922 if (is_ctx_reg(env, insn->dst_reg) ||
2923 is_pkt_reg(env, insn->dst_reg) ||
2924 is_flow_key_reg(env, insn->dst_reg) ||
2925 is_sk_reg(env, insn->dst_reg)) {
2926 verbose(env, "BPF_XADD stores into R%d %s is not allowed\n",
2927 insn->dst_reg,
2928 reg_type_str[reg_state(env, insn->dst_reg)->type]);
2929 return -EACCES;
2930 }
2931
2932
2933 err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
2934 BPF_SIZE(insn->code), BPF_READ, -1, true);
2935 if (err)
2936 return err;
2937
2938
2939 return check_mem_access(env, insn_idx, insn->dst_reg, insn->off,
2940 BPF_SIZE(insn->code), BPF_WRITE, -1, true);
2941 }
2942
2943 static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno,
2944 int off, int access_size,
2945 bool zero_size_allowed)
2946 {
2947 struct bpf_reg_state *reg = reg_state(env, regno);
2948
2949 if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 ||
2950 access_size < 0 || (access_size == 0 && !zero_size_allowed)) {
2951 if (tnum_is_const(reg->var_off)) {
2952 verbose(env, "invalid stack type R%d off=%d access_size=%d\n",
2953 regno, off, access_size);
2954 } else {
2955 char tn_buf[48];
2956
2957 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
2958 verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n",
2959 regno, tn_buf, access_size);
2960 }
2961 return -EACCES;
2962 }
2963 return 0;
2964 }
2965
2966
2967
2968
2969
2970
2971
2972 static int check_stack_boundary(struct bpf_verifier_env *env, int regno,
2973 int access_size, bool zero_size_allowed,
2974 struct bpf_call_arg_meta *meta)
2975 {
2976 struct bpf_reg_state *reg = reg_state(env, regno);
2977 struct bpf_func_state *state = func(env, reg);
2978 int err, min_off, max_off, i, j, slot, spi;
2979
2980 if (reg->type != PTR_TO_STACK) {
2981
2982 if (zero_size_allowed && access_size == 0 &&
2983 register_is_null(reg))
2984 return 0;
2985
2986 verbose(env, "R%d type=%s expected=%s\n", regno,
2987 reg_type_str[reg->type],
2988 reg_type_str[PTR_TO_STACK]);
2989 return -EACCES;
2990 }
2991
2992 if (tnum_is_const(reg->var_off)) {
2993 min_off = max_off = reg->var_off.value + reg->off;
2994 err = __check_stack_boundary(env, regno, min_off, access_size,
2995 zero_size_allowed);
2996 if (err)
2997 return err;
2998 } else {
2999
3000
3001
3002
3003
3004 if (!env->allow_ptr_leaks) {
3005 char tn_buf[48];
3006
3007 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3008 verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n",
3009 regno, tn_buf);
3010 return -EACCES;
3011 }
3012
3013
3014
3015
3016
3017
3018 if (meta && meta->raw_mode)
3019 meta = NULL;
3020
3021 if (reg->smax_value >= BPF_MAX_VAR_OFF ||
3022 reg->smax_value <= -BPF_MAX_VAR_OFF) {
3023 verbose(env, "R%d unbounded indirect variable offset stack access\n",
3024 regno);
3025 return -EACCES;
3026 }
3027 min_off = reg->smin_value + reg->off;
3028 max_off = reg->smax_value + reg->off;
3029 err = __check_stack_boundary(env, regno, min_off, access_size,
3030 zero_size_allowed);
3031 if (err) {
3032 verbose(env, "R%d min value is outside of stack bound\n",
3033 regno);
3034 return err;
3035 }
3036 err = __check_stack_boundary(env, regno, max_off, access_size,
3037 zero_size_allowed);
3038 if (err) {
3039 verbose(env, "R%d max value is outside of stack bound\n",
3040 regno);
3041 return err;
3042 }
3043 }
3044
3045 if (meta && meta->raw_mode) {
3046 meta->access_size = access_size;
3047 meta->regno = regno;
3048 return 0;
3049 }
3050
3051 for (i = min_off; i < max_off + access_size; i++) {
3052 u8 *stype;
3053
3054 slot = -i - 1;
3055 spi = slot / BPF_REG_SIZE;
3056 if (state->allocated_stack <= slot)
3057 goto err;
3058 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
3059 if (*stype == STACK_MISC)
3060 goto mark;
3061 if (*stype == STACK_ZERO) {
3062
3063 *stype = STACK_MISC;
3064 goto mark;
3065 }
3066 if (state->stack[spi].slot_type[0] == STACK_SPILL &&
3067 state->stack[spi].spilled_ptr.type == SCALAR_VALUE) {
3068 __mark_reg_unknown(env, &state->stack[spi].spilled_ptr);
3069 for (j = 0; j < BPF_REG_SIZE; j++)
3070 state->stack[spi].slot_type[j] = STACK_MISC;
3071 goto mark;
3072 }
3073
3074 err:
3075 if (tnum_is_const(reg->var_off)) {
3076 verbose(env, "invalid indirect read from stack off %d+%d size %d\n",
3077 min_off, i - min_off, access_size);
3078 } else {
3079 char tn_buf[48];
3080
3081 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
3082 verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n",
3083 tn_buf, i - min_off, access_size);
3084 }
3085 return -EACCES;
3086 mark:
3087
3088
3089
3090 mark_reg_read(env, &state->stack[spi].spilled_ptr,
3091 state->stack[spi].spilled_ptr.parent,
3092 REG_LIVE_READ64);
3093 }
3094 return update_stack_depth(env, state, min_off);
3095 }
3096
3097 static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,
3098 int access_size, bool zero_size_allowed,
3099 struct bpf_call_arg_meta *meta)
3100 {
3101 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3102
3103 switch (reg->type) {
3104 case PTR_TO_PACKET:
3105 case PTR_TO_PACKET_META:
3106 return check_packet_access(env, regno, reg->off, access_size,
3107 zero_size_allowed);
3108 case PTR_TO_MAP_VALUE:
3109 if (check_map_access_type(env, regno, reg->off, access_size,
3110 meta && meta->raw_mode ? BPF_WRITE :
3111 BPF_READ))
3112 return -EACCES;
3113 return check_map_access(env, regno, reg->off, access_size,
3114 zero_size_allowed);
3115 default:
3116 return check_stack_boundary(env, regno, access_size,
3117 zero_size_allowed, meta);
3118 }
3119 }
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140 static int process_spin_lock(struct bpf_verifier_env *env, int regno,
3141 bool is_lock)
3142 {
3143 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3144 struct bpf_verifier_state *cur = env->cur_state;
3145 bool is_const = tnum_is_const(reg->var_off);
3146 struct bpf_map *map = reg->map_ptr;
3147 u64 val = reg->var_off.value;
3148
3149 if (reg->type != PTR_TO_MAP_VALUE) {
3150 verbose(env, "R%d is not a pointer to map_value\n", regno);
3151 return -EINVAL;
3152 }
3153 if (!is_const) {
3154 verbose(env,
3155 "R%d doesn't have constant offset. bpf_spin_lock has to be at the constant offset\n",
3156 regno);
3157 return -EINVAL;
3158 }
3159 if (!map->btf) {
3160 verbose(env,
3161 "map '%s' has to have BTF in order to use bpf_spin_lock\n",
3162 map->name);
3163 return -EINVAL;
3164 }
3165 if (!map_value_has_spin_lock(map)) {
3166 if (map->spin_lock_off == -E2BIG)
3167 verbose(env,
3168 "map '%s' has more than one 'struct bpf_spin_lock'\n",
3169 map->name);
3170 else if (map->spin_lock_off == -ENOENT)
3171 verbose(env,
3172 "map '%s' doesn't have 'struct bpf_spin_lock'\n",
3173 map->name);
3174 else
3175 verbose(env,
3176 "map '%s' is not a struct type or bpf_spin_lock is mangled\n",
3177 map->name);
3178 return -EINVAL;
3179 }
3180 if (map->spin_lock_off != val + reg->off) {
3181 verbose(env, "off %lld doesn't point to 'struct bpf_spin_lock'\n",
3182 val + reg->off);
3183 return -EINVAL;
3184 }
3185 if (is_lock) {
3186 if (cur->active_spin_lock) {
3187 verbose(env,
3188 "Locking two bpf_spin_locks are not allowed\n");
3189 return -EINVAL;
3190 }
3191 cur->active_spin_lock = reg->id;
3192 } else {
3193 if (!cur->active_spin_lock) {
3194 verbose(env, "bpf_spin_unlock without taking a lock\n");
3195 return -EINVAL;
3196 }
3197 if (cur->active_spin_lock != reg->id) {
3198 verbose(env, "bpf_spin_unlock of different lock\n");
3199 return -EINVAL;
3200 }
3201 cur->active_spin_lock = 0;
3202 }
3203 return 0;
3204 }
3205
3206 static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
3207 {
3208 return type == ARG_PTR_TO_MEM ||
3209 type == ARG_PTR_TO_MEM_OR_NULL ||
3210 type == ARG_PTR_TO_UNINIT_MEM;
3211 }
3212
3213 static bool arg_type_is_mem_size(enum bpf_arg_type type)
3214 {
3215 return type == ARG_CONST_SIZE ||
3216 type == ARG_CONST_SIZE_OR_ZERO;
3217 }
3218
3219 static bool arg_type_is_int_ptr(enum bpf_arg_type type)
3220 {
3221 return type == ARG_PTR_TO_INT ||
3222 type == ARG_PTR_TO_LONG;
3223 }
3224
3225 static int int_ptr_type_to_size(enum bpf_arg_type type)
3226 {
3227 if (type == ARG_PTR_TO_INT)
3228 return sizeof(u32);
3229 else if (type == ARG_PTR_TO_LONG)
3230 return sizeof(u64);
3231
3232 return -EINVAL;
3233 }
3234
3235 static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
3236 enum bpf_arg_type arg_type,
3237 struct bpf_call_arg_meta *meta)
3238 {
3239 struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno];
3240 enum bpf_reg_type expected_type, type = reg->type;
3241 int err = 0;
3242
3243 if (arg_type == ARG_DONTCARE)
3244 return 0;
3245
3246 err = check_reg_arg(env, regno, SRC_OP);
3247 if (err)
3248 return err;
3249
3250 if (arg_type == ARG_ANYTHING) {
3251 if (is_pointer_value(env, regno)) {
3252 verbose(env, "R%d leaks addr into helper function\n",
3253 regno);
3254 return -EACCES;
3255 }
3256 return 0;
3257 }
3258
3259 if (type_is_pkt_pointer(type) &&
3260 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
3261 verbose(env, "helper access to the packet is not allowed\n");
3262 return -EACCES;
3263 }
3264
3265 if (arg_type == ARG_PTR_TO_MAP_KEY ||
3266 arg_type == ARG_PTR_TO_MAP_VALUE ||
3267 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
3268 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
3269 expected_type = PTR_TO_STACK;
3270 if (register_is_null(reg) &&
3271 arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL)
3272 ;
3273 else if (!type_is_pkt_pointer(type) &&
3274 type != PTR_TO_MAP_VALUE &&
3275 type != expected_type)
3276 goto err_type;
3277 } else if (arg_type == ARG_CONST_SIZE ||
3278 arg_type == ARG_CONST_SIZE_OR_ZERO) {
3279 expected_type = SCALAR_VALUE;
3280 if (type != expected_type)
3281 goto err_type;
3282 } else if (arg_type == ARG_CONST_MAP_PTR) {
3283 expected_type = CONST_PTR_TO_MAP;
3284 if (type != expected_type)
3285 goto err_type;
3286 } else if (arg_type == ARG_PTR_TO_CTX) {
3287 expected_type = PTR_TO_CTX;
3288 if (type != expected_type)
3289 goto err_type;
3290 err = check_ctx_reg(env, reg, regno);
3291 if (err < 0)
3292 return err;
3293 } else if (arg_type == ARG_PTR_TO_SOCK_COMMON) {
3294 expected_type = PTR_TO_SOCK_COMMON;
3295
3296 if (!type_is_sk_pointer(type))
3297 goto err_type;
3298 if (reg->ref_obj_id) {
3299 if (meta->ref_obj_id) {
3300 verbose(env, "verifier internal error: more than one arg with ref_obj_id R%d %u %u\n",
3301 regno, reg->ref_obj_id,
3302 meta->ref_obj_id);
3303 return -EFAULT;
3304 }
3305 meta->ref_obj_id = reg->ref_obj_id;
3306 }
3307 } else if (arg_type == ARG_PTR_TO_SOCKET) {
3308 expected_type = PTR_TO_SOCKET;
3309 if (type != expected_type)
3310 goto err_type;
3311 } else if (arg_type == ARG_PTR_TO_SPIN_LOCK) {
3312 if (meta->func_id == BPF_FUNC_spin_lock) {
3313 if (process_spin_lock(env, regno, true))
3314 return -EACCES;
3315 } else if (meta->func_id == BPF_FUNC_spin_unlock) {
3316 if (process_spin_lock(env, regno, false))
3317 return -EACCES;
3318 } else {
3319 verbose(env, "verifier internal error\n");
3320 return -EFAULT;
3321 }
3322 } else if (arg_type_is_mem_ptr(arg_type)) {
3323 expected_type = PTR_TO_STACK;
3324
3325
3326
3327
3328 if (register_is_null(reg) &&
3329 arg_type == ARG_PTR_TO_MEM_OR_NULL)
3330 ;
3331 else if (!type_is_pkt_pointer(type) &&
3332 type != PTR_TO_MAP_VALUE &&
3333 type != expected_type)
3334 goto err_type;
3335 meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM;
3336 } else if (arg_type_is_int_ptr(arg_type)) {
3337 expected_type = PTR_TO_STACK;
3338 if (!type_is_pkt_pointer(type) &&
3339 type != PTR_TO_MAP_VALUE &&
3340 type != expected_type)
3341 goto err_type;
3342 } else {
3343 verbose(env, "unsupported arg_type %d\n", arg_type);
3344 return -EFAULT;
3345 }
3346
3347 if (arg_type == ARG_CONST_MAP_PTR) {
3348
3349 meta->map_ptr = reg->map_ptr;
3350 } else if (arg_type == ARG_PTR_TO_MAP_KEY) {
3351
3352
3353
3354
3355 if (!meta->map_ptr) {
3356
3357
3358
3359
3360
3361 verbose(env, "invalid map_ptr to access map->key\n");
3362 return -EACCES;
3363 }
3364 err = check_helper_mem_access(env, regno,
3365 meta->map_ptr->key_size, false,
3366 NULL);
3367 } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
3368 (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
3369 !register_is_null(reg)) ||
3370 arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
3371
3372
3373
3374 if (!meta->map_ptr) {
3375
3376 verbose(env, "invalid map_ptr to access map->value\n");
3377 return -EACCES;
3378 }
3379 meta->raw_mode = (arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE);
3380 err = check_helper_mem_access(env, regno,
3381 meta->map_ptr->value_size, false,
3382 meta);
3383 } else if (arg_type_is_mem_size(arg_type)) {
3384 bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO);
3385
3386
3387
3388
3389 meta->msize_max_value = reg->umax_value;
3390
3391
3392
3393
3394 if (!tnum_is_const(reg->var_off))
3395
3396
3397
3398
3399
3400 meta = NULL;
3401
3402 if (reg->smin_value < 0) {
3403 verbose(env, "R%d min value is negative, either use unsigned or 'var &= const'\n",
3404 regno);
3405 return -EACCES;
3406 }
3407
3408 if (reg->umin_value == 0) {
3409 err = check_helper_mem_access(env, regno - 1, 0,
3410 zero_size_allowed,
3411 meta);
3412 if (err)
3413 return err;
3414 }
3415
3416 if (reg->umax_value >= BPF_MAX_VAR_SIZ) {
3417 verbose(env, "R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n",
3418 regno);
3419 return -EACCES;
3420 }
3421 err = check_helper_mem_access(env, regno - 1,
3422 reg->umax_value,
3423 zero_size_allowed, meta);
3424 if (!err)
3425 err = mark_chain_precision(env, regno);
3426 } else if (arg_type_is_int_ptr(arg_type)) {
3427 int size = int_ptr_type_to_size(arg_type);
3428
3429 err = check_helper_mem_access(env, regno, size, false, meta);
3430 if (err)
3431 return err;
3432 err = check_ptr_alignment(env, reg, 0, size, true);
3433 }
3434
3435 return err;
3436 err_type:
3437 verbose(env, "R%d type=%s expected=%s\n", regno,
3438 reg_type_str[type], reg_type_str[expected_type]);
3439 return -EACCES;
3440 }
3441
3442 static int check_map_func_compatibility(struct bpf_verifier_env *env,
3443 struct bpf_map *map, int func_id)
3444 {
3445 if (!map)
3446 return 0;
3447
3448
3449 switch (map->map_type) {
3450 case BPF_MAP_TYPE_PROG_ARRAY:
3451 if (func_id != BPF_FUNC_tail_call)
3452 goto error;
3453 break;
3454 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
3455 if (func_id != BPF_FUNC_perf_event_read &&
3456 func_id != BPF_FUNC_perf_event_output &&
3457 func_id != BPF_FUNC_perf_event_read_value)
3458 goto error;
3459 break;
3460 case BPF_MAP_TYPE_STACK_TRACE:
3461 if (func_id != BPF_FUNC_get_stackid)
3462 goto error;
3463 break;
3464 case BPF_MAP_TYPE_CGROUP_ARRAY:
3465 if (func_id != BPF_FUNC_skb_under_cgroup &&
3466 func_id != BPF_FUNC_current_task_under_cgroup)
3467 goto error;
3468 break;
3469 case BPF_MAP_TYPE_CGROUP_STORAGE:
3470 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
3471 if (func_id != BPF_FUNC_get_local_storage)
3472 goto error;
3473 break;
3474 case BPF_MAP_TYPE_DEVMAP:
3475 case BPF_MAP_TYPE_DEVMAP_HASH:
3476 if (func_id != BPF_FUNC_redirect_map &&
3477 func_id != BPF_FUNC_map_lookup_elem)
3478 goto error;
3479 break;
3480
3481
3482
3483 case BPF_MAP_TYPE_CPUMAP:
3484 if (func_id != BPF_FUNC_redirect_map)
3485 goto error;
3486 break;
3487 case BPF_MAP_TYPE_XSKMAP:
3488 if (func_id != BPF_FUNC_redirect_map &&
3489 func_id != BPF_FUNC_map_lookup_elem)
3490 goto error;
3491 break;
3492 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
3493 case BPF_MAP_TYPE_HASH_OF_MAPS:
3494 if (func_id != BPF_FUNC_map_lookup_elem)
3495 goto error;
3496 break;
3497 case BPF_MAP_TYPE_SOCKMAP:
3498 if (func_id != BPF_FUNC_sk_redirect_map &&
3499 func_id != BPF_FUNC_sock_map_update &&
3500 func_id != BPF_FUNC_map_delete_elem &&
3501 func_id != BPF_FUNC_msg_redirect_map)
3502 goto error;
3503 break;
3504 case BPF_MAP_TYPE_SOCKHASH:
3505 if (func_id != BPF_FUNC_sk_redirect_hash &&
3506 func_id != BPF_FUNC_sock_hash_update &&
3507 func_id != BPF_FUNC_map_delete_elem &&
3508 func_id != BPF_FUNC_msg_redirect_hash)
3509 goto error;
3510 break;
3511 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
3512 if (func_id != BPF_FUNC_sk_select_reuseport)
3513 goto error;
3514 break;
3515 case BPF_MAP_TYPE_QUEUE:
3516 case BPF_MAP_TYPE_STACK:
3517 if (func_id != BPF_FUNC_map_peek_elem &&
3518 func_id != BPF_FUNC_map_pop_elem &&
3519 func_id != BPF_FUNC_map_push_elem)
3520 goto error;
3521 break;
3522 case BPF_MAP_TYPE_SK_STORAGE:
3523 if (func_id != BPF_FUNC_sk_storage_get &&
3524 func_id != BPF_FUNC_sk_storage_delete)
3525 goto error;
3526 break;
3527 default:
3528 break;
3529 }
3530
3531
3532 switch (func_id) {
3533 case BPF_FUNC_tail_call:
3534 if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY)
3535 goto error;
3536 if (env->subprog_cnt > 1) {
3537 verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n");
3538 return -EINVAL;
3539 }
3540 break;
3541 case BPF_FUNC_perf_event_read:
3542 case BPF_FUNC_perf_event_output:
3543 case BPF_FUNC_perf_event_read_value:
3544 if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
3545 goto error;
3546 break;
3547 case BPF_FUNC_get_stackid:
3548 if (map->map_type != BPF_MAP_TYPE_STACK_TRACE)
3549 goto error;
3550 break;
3551 case BPF_FUNC_current_task_under_cgroup:
3552 case BPF_FUNC_skb_under_cgroup:
3553 if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY)
3554 goto error;
3555 break;
3556 case BPF_FUNC_redirect_map:
3557 if (map->map_type != BPF_MAP_TYPE_DEVMAP &&
3558 map->map_type != BPF_MAP_TYPE_DEVMAP_HASH &&
3559 map->map_type != BPF_MAP_TYPE_CPUMAP &&
3560 map->map_type != BPF_MAP_TYPE_XSKMAP)
3561 goto error;
3562 break;
3563 case BPF_FUNC_sk_redirect_map:
3564 case BPF_FUNC_msg_redirect_map:
3565 case BPF_FUNC_sock_map_update:
3566 if (map->map_type != BPF_MAP_TYPE_SOCKMAP)
3567 goto error;
3568 break;
3569 case BPF_FUNC_sk_redirect_hash:
3570 case BPF_FUNC_msg_redirect_hash:
3571 case BPF_FUNC_sock_hash_update:
3572 if (map->map_type != BPF_MAP_TYPE_SOCKHASH)
3573 goto error;
3574 break;
3575 case BPF_FUNC_get_local_storage:
3576 if (map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
3577 map->map_type != BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
3578 goto error;
3579 break;
3580 case BPF_FUNC_sk_select_reuseport:
3581 if (map->map_type != BPF_MAP_TYPE_REUSEPORT_SOCKARRAY)
3582 goto error;
3583 break;
3584 case BPF_FUNC_map_peek_elem:
3585 case BPF_FUNC_map_pop_elem:
3586 case BPF_FUNC_map_push_elem:
3587 if (map->map_type != BPF_MAP_TYPE_QUEUE &&
3588 map->map_type != BPF_MAP_TYPE_STACK)
3589 goto error;
3590 break;
3591 case BPF_FUNC_sk_storage_get:
3592 case BPF_FUNC_sk_storage_delete:
3593 if (map->map_type != BPF_MAP_TYPE_SK_STORAGE)
3594 goto error;
3595 break;
3596 default:
3597 break;
3598 }
3599
3600 return 0;
3601 error:
3602 verbose(env, "cannot pass map_type %d into func %s#%d\n",
3603 map->map_type, func_id_name(func_id), func_id);
3604 return -EINVAL;
3605 }
3606
3607 static bool check_raw_mode_ok(const struct bpf_func_proto *fn)
3608 {
3609 int count = 0;
3610
3611 if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM)
3612 count++;
3613 if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM)
3614 count++;
3615 if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM)
3616 count++;
3617 if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM)
3618 count++;
3619 if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM)
3620 count++;
3621
3622
3623
3624
3625
3626 return count <= 1;
3627 }
3628
3629 static bool check_args_pair_invalid(enum bpf_arg_type arg_curr,
3630 enum bpf_arg_type arg_next)
3631 {
3632 return (arg_type_is_mem_ptr(arg_curr) &&
3633 !arg_type_is_mem_size(arg_next)) ||
3634 (!arg_type_is_mem_ptr(arg_curr) &&
3635 arg_type_is_mem_size(arg_next));
3636 }
3637
3638 static bool check_arg_pair_ok(const struct bpf_func_proto *fn)
3639 {
3640
3641
3642
3643
3644
3645 if (arg_type_is_mem_size(fn->arg1_type) ||
3646 arg_type_is_mem_ptr(fn->arg5_type) ||
3647 check_args_pair_invalid(fn->arg1_type, fn->arg2_type) ||
3648 check_args_pair_invalid(fn->arg2_type, fn->arg3_type) ||
3649 check_args_pair_invalid(fn->arg3_type, fn->arg4_type) ||
3650 check_args_pair_invalid(fn->arg4_type, fn->arg5_type))
3651 return false;
3652
3653 return true;
3654 }
3655
3656 static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)
3657 {
3658 int count = 0;
3659
3660 if (arg_type_may_be_refcounted(fn->arg1_type))
3661 count++;
3662 if (arg_type_may_be_refcounted(fn->arg2_type))
3663 count++;
3664 if (arg_type_may_be_refcounted(fn->arg3_type))
3665 count++;
3666 if (arg_type_may_be_refcounted(fn->arg4_type))
3667 count++;
3668 if (arg_type_may_be_refcounted(fn->arg5_type))
3669 count++;
3670
3671
3672
3673
3674 if (is_acquire_function(func_id) && count)
3675 return false;
3676
3677
3678
3679
3680 return count <= 1;
3681 }
3682
3683 static int check_func_proto(const struct bpf_func_proto *fn, int func_id)
3684 {
3685 return check_raw_mode_ok(fn) &&
3686 check_arg_pair_ok(fn) &&
3687 check_refcount_ok(fn, func_id) ? 0 : -EINVAL;
3688 }
3689
3690
3691
3692
3693 static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
3694 struct bpf_func_state *state)
3695 {
3696 struct bpf_reg_state *regs = state->regs, *reg;
3697 int i;
3698
3699 for (i = 0; i < MAX_BPF_REG; i++)
3700 if (reg_is_pkt_pointer_any(®s[i]))
3701 mark_reg_unknown(env, regs, i);
3702
3703 bpf_for_each_spilled_reg(i, state, reg) {
3704 if (!reg)
3705 continue;
3706 if (reg_is_pkt_pointer_any(reg))
3707 __mark_reg_unknown(env, reg);
3708 }
3709 }
3710
3711 static void clear_all_pkt_pointers(struct bpf_verifier_env *env)
3712 {
3713 struct bpf_verifier_state *vstate = env->cur_state;
3714 int i;
3715
3716 for (i = 0; i <= vstate->curframe; i++)
3717 __clear_all_pkt_pointers(env, vstate->frame[i]);
3718 }
3719
3720 static void release_reg_references(struct bpf_verifier_env *env,
3721 struct bpf_func_state *state,
3722 int ref_obj_id)
3723 {
3724 struct bpf_reg_state *regs = state->regs, *reg;
3725 int i;
3726
3727 for (i = 0; i < MAX_BPF_REG; i++)
3728 if (regs[i].ref_obj_id == ref_obj_id)
3729 mark_reg_unknown(env, regs, i);
3730
3731 bpf_for_each_spilled_reg(i, state, reg) {
3732 if (!reg)
3733 continue;
3734 if (reg->ref_obj_id == ref_obj_id)
3735 __mark_reg_unknown(env, reg);
3736 }
3737 }
3738
3739
3740
3741
3742 static int release_reference(struct bpf_verifier_env *env,
3743 int ref_obj_id)
3744 {
3745 struct bpf_verifier_state *vstate = env->cur_state;
3746 int err;
3747 int i;
3748
3749 err = release_reference_state(cur_func(env), ref_obj_id);
3750 if (err)
3751 return err;
3752
3753 for (i = 0; i <= vstate->curframe; i++)
3754 release_reg_references(env, vstate->frame[i], ref_obj_id);
3755
3756 return 0;
3757 }
3758
3759 static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
3760 int *insn_idx)
3761 {
3762 struct bpf_verifier_state *state = env->cur_state;
3763 struct bpf_func_state *caller, *callee;
3764 int i, err, subprog, target_insn;
3765
3766 if (state->curframe + 1 >= MAX_CALL_FRAMES) {
3767 verbose(env, "the call stack of %d frames is too deep\n",
3768 state->curframe + 2);
3769 return -E2BIG;
3770 }
3771
3772 target_insn = *insn_idx + insn->imm;
3773 subprog = find_subprog(env, target_insn + 1);
3774 if (subprog < 0) {
3775 verbose(env, "verifier bug. No program starts at insn %d\n",
3776 target_insn + 1);
3777 return -EFAULT;
3778 }
3779
3780 caller = state->frame[state->curframe];
3781 if (state->frame[state->curframe + 1]) {
3782 verbose(env, "verifier bug. Frame %d already allocated\n",
3783 state->curframe + 1);
3784 return -EFAULT;
3785 }
3786
3787 callee = kzalloc(sizeof(*callee), GFP_KERNEL);
3788 if (!callee)
3789 return -ENOMEM;
3790 state->frame[state->curframe + 1] = callee;
3791
3792
3793
3794
3795
3796 init_func_state(env, callee,
3797
3798 *insn_idx ,
3799 state->curframe + 1 ,
3800 subprog );
3801
3802
3803 err = transfer_reference_state(callee, caller);
3804 if (err)
3805 return err;
3806
3807
3808
3809
3810 for (i = BPF_REG_1; i <= BPF_REG_5; i++)
3811 callee->regs[i] = caller->regs[i];
3812
3813
3814 for (i = 0; i < CALLER_SAVED_REGS; i++) {
3815 mark_reg_not_init(env, caller->regs, caller_saved[i]);
3816 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
3817 }
3818
3819
3820 state->curframe++;
3821
3822
3823 *insn_idx = target_insn;
3824
3825 if (env->log.level & BPF_LOG_LEVEL) {
3826 verbose(env, "caller:\n");
3827 print_verifier_state(env, caller);
3828 verbose(env, "callee:\n");
3829 print_verifier_state(env, callee);
3830 }
3831 return 0;
3832 }
3833
3834 static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
3835 {
3836 struct bpf_verifier_state *state = env->cur_state;
3837 struct bpf_func_state *caller, *callee;
3838 struct bpf_reg_state *r0;
3839 int err;
3840
3841 callee = state->frame[state->curframe];
3842 r0 = &callee->regs[BPF_REG_0];
3843 if (r0->type == PTR_TO_STACK) {
3844
3845
3846
3847
3848
3849
3850 verbose(env, "cannot return stack pointer to the caller\n");
3851 return -EINVAL;
3852 }
3853
3854 state->curframe--;
3855 caller = state->frame[state->curframe];
3856
3857 caller->regs[BPF_REG_0] = *r0;
3858
3859
3860 err = transfer_reference_state(caller, callee);
3861 if (err)
3862 return err;
3863
3864 *insn_idx = callee->callsite + 1;
3865 if (env->log.level & BPF_LOG_LEVEL) {
3866 verbose(env, "returning from callee:\n");
3867 print_verifier_state(env, callee);
3868 verbose(env, "to caller at %d:\n", *insn_idx);
3869 print_verifier_state(env, caller);
3870 }
3871
3872 free_func_state(callee);
3873 state->frame[state->curframe + 1] = NULL;
3874 return 0;
3875 }
3876
3877 static int do_refine_retval_range(struct bpf_verifier_env *env,
3878 struct bpf_reg_state *regs, int ret_type,
3879 int func_id, struct bpf_call_arg_meta *meta)
3880 {
3881 struct bpf_reg_state *ret_reg = ®s[BPF_REG_0];
3882 struct bpf_reg_state tmp_reg = *ret_reg;
3883 bool ret;
3884
3885 if (ret_type != RET_INTEGER ||
3886 (func_id != BPF_FUNC_get_stack &&
3887 func_id != BPF_FUNC_probe_read_str))
3888 return 0;
3889
3890
3891 ret_reg->smin_value = S32_MIN;
3892 ret_reg->smax_value = -1;
3893
3894 __reg_deduce_bounds(ret_reg);
3895 __reg_bound_offset(ret_reg);
3896 __update_reg_bounds(ret_reg);
3897
3898 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, false);
3899 if (!ret)
3900 return -EFAULT;
3901
3902 *ret_reg = tmp_reg;
3903
3904
3905 ret_reg->smin_value = 0;
3906 ret_reg->smax_value = meta->msize_max_value;
3907 ret_reg->umin_value = ret_reg->smin_value;
3908 ret_reg->umax_value = ret_reg->smax_value;
3909
3910 __reg_deduce_bounds(ret_reg);
3911 __reg_bound_offset(ret_reg);
3912 __update_reg_bounds(ret_reg);
3913
3914 return 0;
3915 }
3916
3917 static int
3918 record_func_map(struct bpf_verifier_env *env, struct bpf_call_arg_meta *meta,
3919 int func_id, int insn_idx)
3920 {
3921 struct bpf_insn_aux_data *aux = &env->insn_aux_data[insn_idx];
3922 struct bpf_map *map = meta->map_ptr;
3923
3924 if (func_id != BPF_FUNC_tail_call &&
3925 func_id != BPF_FUNC_map_lookup_elem &&
3926 func_id != BPF_FUNC_map_update_elem &&
3927 func_id != BPF_FUNC_map_delete_elem &&
3928 func_id != BPF_FUNC_map_push_elem &&
3929 func_id != BPF_FUNC_map_pop_elem &&
3930 func_id != BPF_FUNC_map_peek_elem)
3931 return 0;
3932
3933 if (map == NULL) {
3934 verbose(env, "kernel subsystem misconfigured verifier\n");
3935 return -EINVAL;
3936 }
3937
3938
3939
3940
3941
3942 if ((map->map_flags & BPF_F_RDONLY_PROG) &&
3943 (func_id == BPF_FUNC_map_delete_elem ||
3944 func_id == BPF_FUNC_map_update_elem ||
3945 func_id == BPF_FUNC_map_push_elem ||
3946 func_id == BPF_FUNC_map_pop_elem)) {
3947 verbose(env, "write into map forbidden\n");
3948 return -EACCES;
3949 }
3950
3951 if (!BPF_MAP_PTR(aux->map_state))
3952 bpf_map_ptr_store(aux, meta->map_ptr,
3953 meta->map_ptr->unpriv_array);
3954 else if (BPF_MAP_PTR(aux->map_state) != meta->map_ptr)
3955 bpf_map_ptr_store(aux, BPF_MAP_PTR_POISON,
3956 meta->map_ptr->unpriv_array);
3957 return 0;
3958 }
3959
3960 static int check_reference_leak(struct bpf_verifier_env *env)
3961 {
3962 struct bpf_func_state *state = cur_func(env);
3963 int i;
3964
3965 for (i = 0; i < state->acquired_refs; i++) {
3966 verbose(env, "Unreleased reference id=%d alloc_insn=%d\n",
3967 state->refs[i].id, state->refs[i].insn_idx);
3968 }
3969 return state->acquired_refs ? -EINVAL : 0;
3970 }
3971
3972 static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn_idx)
3973 {
3974 const struct bpf_func_proto *fn = NULL;
3975 struct bpf_reg_state *regs;
3976 struct bpf_call_arg_meta meta;
3977 bool changes_data;
3978 int i, err;
3979
3980
3981 if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) {
3982 verbose(env, "invalid func %s#%d\n", func_id_name(func_id),
3983 func_id);
3984 return -EINVAL;
3985 }
3986
3987 if (env->ops->get_func_proto)
3988 fn = env->ops->get_func_proto(func_id, env->prog);
3989 if (!fn) {
3990 verbose(env, "unknown func %s#%d\n", func_id_name(func_id),
3991 func_id);
3992 return -EINVAL;
3993 }
3994
3995
3996 if (!env->prog->gpl_compatible && fn->gpl_only) {
3997 verbose(env, "cannot call GPL-restricted function from non-GPL compatible program\n");
3998 return -EINVAL;
3999 }
4000
4001
4002 changes_data = bpf_helper_changes_pkt_data(fn->func);
4003 if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) {
4004 verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n",
4005 func_id_name(func_id), func_id);
4006 return -EINVAL;
4007 }
4008
4009 memset(&meta, 0, sizeof(meta));
4010 meta.pkt_access = fn->pkt_access;
4011
4012 err = check_func_proto(fn, func_id);
4013 if (err) {
4014 verbose(env, "kernel subsystem misconfigured func %s#%d\n",
4015 func_id_name(func_id), func_id);
4016 return err;
4017 }
4018
4019 meta.func_id = func_id;
4020
4021 err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta);
4022 if (err)
4023 return err;
4024 err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta);
4025 if (err)
4026 return err;
4027 err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta);
4028 if (err)
4029 return err;
4030 err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta);
4031 if (err)
4032 return err;
4033 err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta);
4034 if (err)
4035 return err;
4036
4037 err = record_func_map(env, &meta, func_id, insn_idx);
4038 if (err)
4039 return err;
4040
4041
4042
4043
4044 for (i = 0; i < meta.access_size; i++) {
4045 err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B,
4046 BPF_WRITE, -1, false);
4047 if (err)
4048 return err;
4049 }
4050
4051 if (func_id == BPF_FUNC_tail_call) {
4052 err = check_reference_leak(env);
4053 if (err) {
4054 verbose(env, "tail_call would lead to reference leak\n");
4055 return err;
4056 }
4057 } else if (is_release_function(func_id)) {
4058 err = release_reference(env, meta.ref_obj_id);
4059 if (err) {
4060 verbose(env, "func %s#%d reference has not been acquired before\n",
4061 func_id_name(func_id), func_id);
4062 return err;
4063 }
4064 }
4065
4066 regs = cur_regs(env);
4067
4068
4069
4070
4071 if (func_id == BPF_FUNC_get_local_storage &&
4072 !register_is_null(®s[BPF_REG_2])) {
4073 verbose(env, "get_local_storage() doesn't support non-zero flags\n");
4074 return -EINVAL;
4075 }
4076
4077
4078 for (i = 0; i < CALLER_SAVED_REGS; i++) {
4079 mark_reg_not_init(env, regs, caller_saved[i]);
4080 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
4081 }
4082
4083
4084 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
4085
4086
4087 if (fn->ret_type == RET_INTEGER) {
4088
4089 mark_reg_unknown(env, regs, BPF_REG_0);
4090 } else if (fn->ret_type == RET_VOID) {
4091 regs[BPF_REG_0].type = NOT_INIT;
4092 } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
4093 fn->ret_type == RET_PTR_TO_MAP_VALUE) {
4094
4095 mark_reg_known_zero(env, regs, BPF_REG_0);
4096
4097
4098
4099
4100 if (meta.map_ptr == NULL) {
4101 verbose(env,
4102 "kernel subsystem misconfigured verifier\n");
4103 return -EINVAL;
4104 }
4105 regs[BPF_REG_0].map_ptr = meta.map_ptr;
4106 if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
4107 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
4108 if (map_value_has_spin_lock(meta.map_ptr))
4109 regs[BPF_REG_0].id = ++env->id_gen;
4110 } else {
4111 regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
4112 regs[BPF_REG_0].id = ++env->id_gen;
4113 }
4114 } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
4115 mark_reg_known_zero(env, regs, BPF_REG_0);
4116 regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
4117 regs[BPF_REG_0].id = ++env->id_gen;
4118 } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
4119 mark_reg_known_zero(env, regs, BPF_REG_0);
4120 regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
4121 regs[BPF_REG_0].id = ++env->id_gen;
4122 } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
4123 mark_reg_known_zero(env, regs, BPF_REG_0);
4124 regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
4125 regs[BPF_REG_0].id = ++env->id_gen;
4126 } else {
4127 verbose(env, "unknown return type %d of func %s#%d\n",
4128 fn->ret_type, func_id_name(func_id), func_id);
4129 return -EINVAL;
4130 }
4131
4132 if (is_ptr_cast_function(func_id)) {
4133
4134 regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
4135 } else if (is_acquire_function(func_id)) {
4136 int id = acquire_reference_state(env, insn_idx);
4137
4138 if (id < 0)
4139 return id;
4140
4141 regs[BPF_REG_0].id = id;
4142
4143 regs[BPF_REG_0].ref_obj_id = id;
4144 }
4145
4146 err = do_refine_retval_range(env, regs, fn->ret_type, func_id, &meta);
4147 if (err)
4148 return err;
4149
4150 err = check_map_func_compatibility(env, meta.map_ptr, func_id);
4151 if (err)
4152 return err;
4153
4154 if (func_id == BPF_FUNC_get_stack && !env->prog->has_callchain_buf) {
4155 const char *err_str;
4156
4157 #ifdef CONFIG_PERF_EVENTS
4158 err = get_callchain_buffers(sysctl_perf_event_max_stack);
4159 err_str = "cannot get callchain buffer for func %s#%d\n";
4160 #else
4161 err = -ENOTSUPP;
4162 err_str = "func %s#%d not supported without CONFIG_PERF_EVENTS\n";
4163 #endif
4164 if (err) {
4165 verbose(env, err_str, func_id_name(func_id), func_id);
4166 return err;
4167 }
4168
4169 env->prog->has_callchain_buf = true;
4170 }
4171
4172 if (changes_data)
4173 clear_all_pkt_pointers(env);
4174 return 0;
4175 }
4176
4177 static bool signed_add_overflows(s64 a, s64 b)
4178 {
4179
4180 s64 res = (s64)((u64)a + (u64)b);
4181
4182 if (b < 0)
4183 return res > a;
4184 return res < a;
4185 }
4186
4187 static bool signed_sub_overflows(s64 a, s64 b)
4188 {
4189
4190 s64 res = (s64)((u64)a - (u64)b);
4191
4192 if (b < 0)
4193 return res < a;
4194 return res > a;
4195 }
4196
4197 static bool check_reg_sane_offset(struct bpf_verifier_env *env,
4198 const struct bpf_reg_state *reg,
4199 enum bpf_reg_type type)
4200 {
4201 bool known = tnum_is_const(reg->var_off);
4202 s64 val = reg->var_off.value;
4203 s64 smin = reg->smin_value;
4204
4205 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
4206 verbose(env, "math between %s pointer and %lld is not allowed\n",
4207 reg_type_str[type], val);
4208 return false;
4209 }
4210
4211 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
4212 verbose(env, "%s pointer offset %d is not allowed\n",
4213 reg_type_str[type], reg->off);
4214 return false;
4215 }
4216
4217 if (smin == S64_MIN) {
4218 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
4219 reg_type_str[type]);
4220 return false;
4221 }
4222
4223 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
4224 verbose(env, "value %lld makes %s pointer be out of bounds\n",
4225 smin, reg_type_str[type]);
4226 return false;
4227 }
4228
4229 return true;
4230 }
4231
4232 static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
4233 {
4234 return &env->insn_aux_data[env->insn_idx];
4235 }
4236
4237 static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
4238 u32 *ptr_limit, u8 opcode, bool off_is_neg)
4239 {
4240 bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
4241 (opcode == BPF_SUB && !off_is_neg);
4242 u32 off;
4243
4244 switch (ptr_reg->type) {
4245 case PTR_TO_STACK:
4246
4247
4248
4249 off = ptr_reg->off + ptr_reg->var_off.value;
4250 if (mask_to_left)
4251 *ptr_limit = MAX_BPF_STACK + off;
4252 else
4253 *ptr_limit = -off;
4254 return 0;
4255 case PTR_TO_MAP_VALUE:
4256 if (mask_to_left) {
4257 *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
4258 } else {
4259 off = ptr_reg->smin_value + ptr_reg->off;
4260 *ptr_limit = ptr_reg->map_ptr->value_size - off;
4261 }
4262 return 0;
4263 default:
4264 return -EINVAL;
4265 }
4266 }
4267
4268 static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
4269 const struct bpf_insn *insn)
4270 {
4271 return env->allow_ptr_leaks || BPF_SRC(insn->code) == BPF_K;
4272 }
4273
4274 static int update_alu_sanitation_state(struct bpf_insn_aux_data *aux,
4275 u32 alu_state, u32 alu_limit)
4276 {
4277
4278
4279
4280 if (aux->alu_state &&
4281 (aux->alu_state != alu_state ||
4282 aux->alu_limit != alu_limit))
4283 return -EACCES;
4284
4285
4286 aux->alu_state = alu_state;
4287 aux->alu_limit = alu_limit;
4288 return 0;
4289 }
4290
4291 static int sanitize_val_alu(struct bpf_verifier_env *env,
4292 struct bpf_insn *insn)
4293 {
4294 struct bpf_insn_aux_data *aux = cur_aux(env);
4295
4296 if (can_skip_alu_sanitation(env, insn))
4297 return 0;
4298
4299 return update_alu_sanitation_state(aux, BPF_ALU_NON_POINTER, 0);
4300 }
4301
4302 static int sanitize_ptr_alu(struct bpf_verifier_env *env,
4303 struct bpf_insn *insn,
4304 const struct bpf_reg_state *ptr_reg,
4305 struct bpf_reg_state *dst_reg,
4306 bool off_is_neg)
4307 {
4308 struct bpf_verifier_state *vstate = env->cur_state;
4309 struct bpf_insn_aux_data *aux = cur_aux(env);
4310 bool ptr_is_dst_reg = ptr_reg == dst_reg;
4311 u8 opcode = BPF_OP(insn->code);
4312 u32 alu_state, alu_limit;
4313 struct bpf_reg_state tmp;
4314 bool ret;
4315
4316 if (can_skip_alu_sanitation(env, insn))
4317 return 0;
4318
4319
4320
4321
4322
4323 if (vstate->speculative)
4324 goto do_sim;
4325
4326 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0;
4327 alu_state |= ptr_is_dst_reg ?
4328 BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
4329
4330 if (retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg))
4331 return 0;
4332 if (update_alu_sanitation_state(aux, alu_state, alu_limit))
4333 return -EACCES;
4334 do_sim:
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344 if (!ptr_is_dst_reg) {
4345 tmp = *dst_reg;
4346 *dst_reg = *ptr_reg;
4347 }
4348 ret = push_stack(env, env->insn_idx + 1, env->insn_idx, true);
4349 if (!ptr_is_dst_reg && ret)
4350 *dst_reg = tmp;
4351 return !ret ? -EFAULT : 0;
4352 }
4353
4354
4355
4356
4357
4358
4359 static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
4360 struct bpf_insn *insn,
4361 const struct bpf_reg_state *ptr_reg,
4362 const struct bpf_reg_state *off_reg)
4363 {
4364 struct bpf_verifier_state *vstate = env->cur_state;
4365 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4366 struct bpf_reg_state *regs = state->regs, *dst_reg;
4367 bool known = tnum_is_const(off_reg->var_off);
4368 s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value,
4369 smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
4370 u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
4371 umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
4372 u32 dst = insn->dst_reg, src = insn->src_reg;
4373 u8 opcode = BPF_OP(insn->code);
4374 int ret;
4375
4376 dst_reg = ®s[dst];
4377
4378 if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
4379 smin_val > smax_val || umin_val > umax_val) {
4380
4381
4382
4383 __mark_reg_unknown(env, dst_reg);
4384 return 0;
4385 }
4386
4387 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4388
4389 verbose(env,
4390 "R%d 32-bit pointer arithmetic prohibited\n",
4391 dst);
4392 return -EACCES;
4393 }
4394
4395 switch (ptr_reg->type) {
4396 case PTR_TO_MAP_VALUE_OR_NULL:
4397 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
4398 dst, reg_type_str[ptr_reg->type]);
4399 return -EACCES;
4400 case CONST_PTR_TO_MAP:
4401 case PTR_TO_PACKET_END:
4402 case PTR_TO_SOCKET:
4403 case PTR_TO_SOCKET_OR_NULL:
4404 case PTR_TO_SOCK_COMMON:
4405 case PTR_TO_SOCK_COMMON_OR_NULL:
4406 case PTR_TO_TCP_SOCK:
4407 case PTR_TO_TCP_SOCK_OR_NULL:
4408 case PTR_TO_XDP_SOCK:
4409 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
4410 dst, reg_type_str[ptr_reg->type]);
4411 return -EACCES;
4412 case PTR_TO_MAP_VALUE:
4413 if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
4414 verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
4415 off_reg == dst_reg ? dst : src);
4416 return -EACCES;
4417 }
4418
4419 default:
4420 break;
4421 }
4422
4423
4424
4425
4426 dst_reg->type = ptr_reg->type;
4427 dst_reg->id = ptr_reg->id;
4428
4429 if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) ||
4430 !check_reg_sane_offset(env, ptr_reg, ptr_reg->type))
4431 return -EINVAL;
4432
4433 switch (opcode) {
4434 case BPF_ADD:
4435 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
4436 if (ret < 0) {
4437 verbose(env, "R%d tried to add from different maps or paths\n", dst);
4438 return ret;
4439 }
4440
4441
4442
4443 if (known && (ptr_reg->off + smin_val ==
4444 (s64)(s32)(ptr_reg->off + smin_val))) {
4445
4446 dst_reg->smin_value = smin_ptr;
4447 dst_reg->smax_value = smax_ptr;
4448 dst_reg->umin_value = umin_ptr;
4449 dst_reg->umax_value = umax_ptr;
4450 dst_reg->var_off = ptr_reg->var_off;
4451 dst_reg->off = ptr_reg->off + smin_val;
4452 dst_reg->raw = ptr_reg->raw;
4453 break;
4454 }
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464 if (signed_add_overflows(smin_ptr, smin_val) ||
4465 signed_add_overflows(smax_ptr, smax_val)) {
4466 dst_reg->smin_value = S64_MIN;
4467 dst_reg->smax_value = S64_MAX;
4468 } else {
4469 dst_reg->smin_value = smin_ptr + smin_val;
4470 dst_reg->smax_value = smax_ptr + smax_val;
4471 }
4472 if (umin_ptr + umin_val < umin_ptr ||
4473 umax_ptr + umax_val < umax_ptr) {
4474 dst_reg->umin_value = 0;
4475 dst_reg->umax_value = U64_MAX;
4476 } else {
4477 dst_reg->umin_value = umin_ptr + umin_val;
4478 dst_reg->umax_value = umax_ptr + umax_val;
4479 }
4480 dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off);
4481 dst_reg->off = ptr_reg->off;
4482 dst_reg->raw = ptr_reg->raw;
4483 if (reg_is_pkt_pointer(ptr_reg)) {
4484 dst_reg->id = ++env->id_gen;
4485
4486 dst_reg->raw = 0;
4487 }
4488 break;
4489 case BPF_SUB:
4490 ret = sanitize_ptr_alu(env, insn, ptr_reg, dst_reg, smin_val < 0);
4491 if (ret < 0) {
4492 verbose(env, "R%d tried to sub from different maps or paths\n", dst);
4493 return ret;
4494 }
4495 if (dst_reg == off_reg) {
4496
4497 verbose(env, "R%d tried to subtract pointer from scalar\n",
4498 dst);
4499 return -EACCES;
4500 }
4501
4502
4503
4504
4505 if (ptr_reg->type == PTR_TO_STACK) {
4506 verbose(env, "R%d subtraction from stack pointer prohibited\n",
4507 dst);
4508 return -EACCES;
4509 }
4510 if (known && (ptr_reg->off - smin_val ==
4511 (s64)(s32)(ptr_reg->off - smin_val))) {
4512
4513 dst_reg->smin_value = smin_ptr;
4514 dst_reg->smax_value = smax_ptr;
4515 dst_reg->umin_value = umin_ptr;
4516 dst_reg->umax_value = umax_ptr;
4517 dst_reg->var_off = ptr_reg->var_off;
4518 dst_reg->id = ptr_reg->id;
4519 dst_reg->off = ptr_reg->off - smin_val;
4520 dst_reg->raw = ptr_reg->raw;
4521 break;
4522 }
4523
4524
4525
4526 if (signed_sub_overflows(smin_ptr, smax_val) ||
4527 signed_sub_overflows(smax_ptr, smin_val)) {
4528
4529 dst_reg->smin_value = S64_MIN;
4530 dst_reg->smax_value = S64_MAX;
4531 } else {
4532 dst_reg->smin_value = smin_ptr - smax_val;
4533 dst_reg->smax_value = smax_ptr - smin_val;
4534 }
4535 if (umin_ptr < umax_val) {
4536
4537 dst_reg->umin_value = 0;
4538 dst_reg->umax_value = U64_MAX;
4539 } else {
4540
4541 dst_reg->umin_value = umin_ptr - umax_val;
4542 dst_reg->umax_value = umax_ptr - umin_val;
4543 }
4544 dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off);
4545 dst_reg->off = ptr_reg->off;
4546 dst_reg->raw = ptr_reg->raw;
4547 if (reg_is_pkt_pointer(ptr_reg)) {
4548 dst_reg->id = ++env->id_gen;
4549
4550 if (smin_val < 0)
4551 dst_reg->raw = 0;
4552 }
4553 break;
4554 case BPF_AND:
4555 case BPF_OR:
4556 case BPF_XOR:
4557
4558 verbose(env, "R%d bitwise operator %s on pointer prohibited\n",
4559 dst, bpf_alu_string[opcode >> 4]);
4560 return -EACCES;
4561 default:
4562
4563 verbose(env, "R%d pointer arithmetic with %s operator prohibited\n",
4564 dst, bpf_alu_string[opcode >> 4]);
4565 return -EACCES;
4566 }
4567
4568 if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type))
4569 return -EINVAL;
4570
4571 __update_reg_bounds(dst_reg);
4572 __reg_deduce_bounds(dst_reg);
4573 __reg_bound_offset(dst_reg);
4574
4575
4576
4577
4578 if (!env->allow_ptr_leaks) {
4579 if (dst_reg->type == PTR_TO_MAP_VALUE &&
4580 check_map_access(env, dst, dst_reg->off, 1, false)) {
4581 verbose(env, "R%d pointer arithmetic of map value goes out of range, "
4582 "prohibited for !root\n", dst);
4583 return -EACCES;
4584 } else if (dst_reg->type == PTR_TO_STACK &&
4585 check_stack_access(env, dst_reg, dst_reg->off +
4586 dst_reg->var_off.value, 1)) {
4587 verbose(env, "R%d stack pointer arithmetic goes out of range, "
4588 "prohibited for !root\n", dst);
4589 return -EACCES;
4590 }
4591 }
4592
4593 return 0;
4594 }
4595
4596
4597
4598
4599
4600 static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
4601 struct bpf_insn *insn,
4602 struct bpf_reg_state *dst_reg,
4603 struct bpf_reg_state src_reg)
4604 {
4605 struct bpf_reg_state *regs = cur_regs(env);
4606 u8 opcode = BPF_OP(insn->code);
4607 bool src_known, dst_known;
4608 s64 smin_val, smax_val;
4609 u64 umin_val, umax_val;
4610 u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32;
4611 u32 dst = insn->dst_reg;
4612 int ret;
4613
4614 if (insn_bitness == 32) {
4615
4616
4617
4618
4619 coerce_reg_to_size(dst_reg, 4);
4620 coerce_reg_to_size(&src_reg, 4);
4621 }
4622
4623 smin_val = src_reg.smin_value;
4624 smax_val = src_reg.smax_value;
4625 umin_val = src_reg.umin_value;
4626 umax_val = src_reg.umax_value;
4627 src_known = tnum_is_const(src_reg.var_off);
4628 dst_known = tnum_is_const(dst_reg->var_off);
4629
4630 if ((src_known && (smin_val != smax_val || umin_val != umax_val)) ||
4631 smin_val > smax_val || umin_val > umax_val) {
4632
4633
4634
4635 __mark_reg_unknown(env, dst_reg);
4636 return 0;
4637 }
4638
4639 if (!src_known &&
4640 opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) {
4641 __mark_reg_unknown(env, dst_reg);
4642 return 0;
4643 }
4644
4645 switch (opcode) {
4646 case BPF_ADD:
4647 ret = sanitize_val_alu(env, insn);
4648 if (ret < 0) {
4649 verbose(env, "R%d tried to add from different pointers or scalars\n", dst);
4650 return ret;
4651 }
4652 if (signed_add_overflows(dst_reg->smin_value, smin_val) ||
4653 signed_add_overflows(dst_reg->smax_value, smax_val)) {
4654 dst_reg->smin_value = S64_MIN;
4655 dst_reg->smax_value = S64_MAX;
4656 } else {
4657 dst_reg->smin_value += smin_val;
4658 dst_reg->smax_value += smax_val;
4659 }
4660 if (dst_reg->umin_value + umin_val < umin_val ||
4661 dst_reg->umax_value + umax_val < umax_val) {
4662 dst_reg->umin_value = 0;
4663 dst_reg->umax_value = U64_MAX;
4664 } else {
4665 dst_reg->umin_value += umin_val;
4666 dst_reg->umax_value += umax_val;
4667 }
4668 dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off);
4669 break;
4670 case BPF_SUB:
4671 ret = sanitize_val_alu(env, insn);
4672 if (ret < 0) {
4673 verbose(env, "R%d tried to sub from different pointers or scalars\n", dst);
4674 return ret;
4675 }
4676 if (signed_sub_overflows(dst_reg->smin_value, smax_val) ||
4677 signed_sub_overflows(dst_reg->smax_value, smin_val)) {
4678
4679 dst_reg->smin_value = S64_MIN;
4680 dst_reg->smax_value = S64_MAX;
4681 } else {
4682 dst_reg->smin_value -= smax_val;
4683 dst_reg->smax_value -= smin_val;
4684 }
4685 if (dst_reg->umin_value < umax_val) {
4686
4687 dst_reg->umin_value = 0;
4688 dst_reg->umax_value = U64_MAX;
4689 } else {
4690
4691 dst_reg->umin_value -= umax_val;
4692 dst_reg->umax_value -= umin_val;
4693 }
4694 dst_reg->var_off = tnum_sub(dst_reg->var_off, src_reg.var_off);
4695 break;
4696 case BPF_MUL:
4697 dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off);
4698 if (smin_val < 0 || dst_reg->smin_value < 0) {
4699
4700 __mark_reg_unbounded(dst_reg);
4701 __update_reg_bounds(dst_reg);
4702 break;
4703 }
4704
4705
4706
4707 if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) {
4708
4709 __mark_reg_unbounded(dst_reg);
4710
4711 __update_reg_bounds(dst_reg);
4712 break;
4713 }
4714 dst_reg->umin_value *= umin_val;
4715 dst_reg->umax_value *= umax_val;
4716 if (dst_reg->umax_value > S64_MAX) {
4717
4718 dst_reg->smin_value = S64_MIN;
4719 dst_reg->smax_value = S64_MAX;
4720 } else {
4721 dst_reg->smin_value = dst_reg->umin_value;
4722 dst_reg->smax_value = dst_reg->umax_value;
4723 }
4724 break;
4725 case BPF_AND:
4726 if (src_known && dst_known) {
4727 __mark_reg_known(dst_reg, dst_reg->var_off.value &
4728 src_reg.var_off.value);
4729 break;
4730 }
4731
4732
4733
4734 dst_reg->var_off = tnum_and(dst_reg->var_off, src_reg.var_off);
4735 dst_reg->umin_value = dst_reg->var_off.value;
4736 dst_reg->umax_value = min(dst_reg->umax_value, umax_val);
4737 if (dst_reg->smin_value < 0 || smin_val < 0) {
4738
4739
4740
4741 dst_reg->smin_value = S64_MIN;
4742 dst_reg->smax_value = S64_MAX;
4743 } else {
4744
4745
4746
4747 dst_reg->smin_value = dst_reg->umin_value;
4748 dst_reg->smax_value = dst_reg->umax_value;
4749 }
4750
4751 __update_reg_bounds(dst_reg);
4752 break;
4753 case BPF_OR:
4754 if (src_known && dst_known) {
4755 __mark_reg_known(dst_reg, dst_reg->var_off.value |
4756 src_reg.var_off.value);
4757 break;
4758 }
4759
4760
4761
4762 dst_reg->var_off = tnum_or(dst_reg->var_off, src_reg.var_off);
4763 dst_reg->umin_value = max(dst_reg->umin_value, umin_val);
4764 dst_reg->umax_value = dst_reg->var_off.value |
4765 dst_reg->var_off.mask;
4766 if (dst_reg->smin_value < 0 || smin_val < 0) {
4767
4768
4769
4770 dst_reg->smin_value = S64_MIN;
4771 dst_reg->smax_value = S64_MAX;
4772 } else {
4773
4774
4775
4776 dst_reg->smin_value = dst_reg->umin_value;
4777 dst_reg->smax_value = dst_reg->umax_value;
4778 }
4779
4780 __update_reg_bounds(dst_reg);
4781 break;
4782 case BPF_LSH:
4783 if (umax_val >= insn_bitness) {
4784
4785
4786
4787 mark_reg_unknown(env, regs, insn->dst_reg);
4788 break;
4789 }
4790
4791
4792
4793 dst_reg->smin_value = S64_MIN;
4794 dst_reg->smax_value = S64_MAX;
4795
4796 if (dst_reg->umax_value > 1ULL << (63 - umax_val)) {
4797 dst_reg->umin_value = 0;
4798 dst_reg->umax_value = U64_MAX;
4799 } else {
4800 dst_reg->umin_value <<= umin_val;
4801 dst_reg->umax_value <<= umax_val;
4802 }
4803 dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val);
4804
4805 __update_reg_bounds(dst_reg);
4806 break;
4807 case BPF_RSH:
4808 if (umax_val >= insn_bitness) {
4809
4810
4811
4812 mark_reg_unknown(env, regs, insn->dst_reg);
4813 break;
4814 }
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829 dst_reg->smin_value = S64_MIN;
4830 dst_reg->smax_value = S64_MAX;
4831 dst_reg->var_off = tnum_rshift(dst_reg->var_off, umin_val);
4832 dst_reg->umin_value >>= umax_val;
4833 dst_reg->umax_value >>= umin_val;
4834
4835 __update_reg_bounds(dst_reg);
4836 break;
4837 case BPF_ARSH:
4838 if (umax_val >= insn_bitness) {
4839
4840
4841
4842 mark_reg_unknown(env, regs, insn->dst_reg);
4843 break;
4844 }
4845
4846
4847
4848
4849 if (insn_bitness == 32) {
4850 dst_reg->smin_value = (u32)(((s32)dst_reg->smin_value) >> umin_val);
4851 dst_reg->smax_value = (u32)(((s32)dst_reg->smax_value) >> umin_val);
4852 } else {
4853 dst_reg->smin_value >>= umin_val;
4854 dst_reg->smax_value >>= umin_val;
4855 }
4856
4857 dst_reg->var_off = tnum_arshift(dst_reg->var_off, umin_val,
4858 insn_bitness);
4859
4860
4861
4862
4863 dst_reg->umin_value = 0;
4864 dst_reg->umax_value = U64_MAX;
4865 __update_reg_bounds(dst_reg);
4866 break;
4867 default:
4868 mark_reg_unknown(env, regs, insn->dst_reg);
4869 break;
4870 }
4871
4872 if (BPF_CLASS(insn->code) != BPF_ALU64) {
4873
4874 coerce_reg_to_size(dst_reg, 4);
4875 }
4876
4877 __reg_deduce_bounds(dst_reg);
4878 __reg_bound_offset(dst_reg);
4879 return 0;
4880 }
4881
4882
4883
4884
4885 static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
4886 struct bpf_insn *insn)
4887 {
4888 struct bpf_verifier_state *vstate = env->cur_state;
4889 struct bpf_func_state *state = vstate->frame[vstate->curframe];
4890 struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg;
4891 struct bpf_reg_state *ptr_reg = NULL, off_reg = {0};
4892 u8 opcode = BPF_OP(insn->code);
4893 int err;
4894
4895 dst_reg = ®s[insn->dst_reg];
4896 src_reg = NULL;
4897 if (dst_reg->type != SCALAR_VALUE)
4898 ptr_reg = dst_reg;
4899 if (BPF_SRC(insn->code) == BPF_X) {
4900 src_reg = ®s[insn->src_reg];
4901 if (src_reg->type != SCALAR_VALUE) {
4902 if (dst_reg->type != SCALAR_VALUE) {
4903
4904
4905
4906
4907 if (opcode == BPF_SUB && env->allow_ptr_leaks) {
4908 mark_reg_unknown(env, regs, insn->dst_reg);
4909 return 0;
4910 }
4911 verbose(env, "R%d pointer %s pointer prohibited\n",
4912 insn->dst_reg,
4913 bpf_alu_string[opcode >> 4]);
4914 return -EACCES;
4915 } else {
4916
4917
4918
4919
4920 err = mark_chain_precision(env, insn->dst_reg);
4921 if (err)
4922 return err;
4923 return adjust_ptr_min_max_vals(env, insn,
4924 src_reg, dst_reg);
4925 }
4926 } else if (ptr_reg) {
4927
4928 err = mark_chain_precision(env, insn->src_reg);
4929 if (err)
4930 return err;
4931 return adjust_ptr_min_max_vals(env, insn,
4932 dst_reg, src_reg);
4933 }
4934 } else {
4935
4936
4937
4938 off_reg.type = SCALAR_VALUE;
4939 __mark_reg_known(&off_reg, insn->imm);
4940 src_reg = &off_reg;
4941 if (ptr_reg)
4942 return adjust_ptr_min_max_vals(env, insn,
4943 ptr_reg, src_reg);
4944 }
4945
4946
4947 if (WARN_ON_ONCE(ptr_reg)) {
4948 print_verifier_state(env, state);
4949 verbose(env, "verifier internal error: unexpected ptr_reg\n");
4950 return -EINVAL;
4951 }
4952 if (WARN_ON(!src_reg)) {
4953 print_verifier_state(env, state);
4954 verbose(env, "verifier internal error: no src_reg\n");
4955 return -EINVAL;
4956 }
4957 return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg);
4958 }
4959
4960
4961 static int check_alu_op(struct bpf_verifier_env *env, struct bpf_insn *insn)
4962 {
4963 struct bpf_reg_state *regs = cur_regs(env);
4964 u8 opcode = BPF_OP(insn->code);
4965 int err;
4966
4967 if (opcode == BPF_END || opcode == BPF_NEG) {
4968 if (opcode == BPF_NEG) {
4969 if (BPF_SRC(insn->code) != 0 ||
4970 insn->src_reg != BPF_REG_0 ||
4971 insn->off != 0 || insn->imm != 0) {
4972 verbose(env, "BPF_NEG uses reserved fields\n");
4973 return -EINVAL;
4974 }
4975 } else {
4976 if (insn->src_reg != BPF_REG_0 || insn->off != 0 ||
4977 (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) ||
4978 BPF_CLASS(insn->code) == BPF_ALU64) {
4979 verbose(env, "BPF_END uses reserved fields\n");
4980 return -EINVAL;
4981 }
4982 }
4983
4984
4985 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
4986 if (err)
4987 return err;
4988
4989 if (is_pointer_value(env, insn->dst_reg)) {
4990 verbose(env, "R%d pointer arithmetic prohibited\n",
4991 insn->dst_reg);
4992 return -EACCES;
4993 }
4994
4995
4996 err = check_reg_arg(env, insn->dst_reg, DST_OP);
4997 if (err)
4998 return err;
4999
5000 } else if (opcode == BPF_MOV) {
5001
5002 if (BPF_SRC(insn->code) == BPF_X) {
5003 if (insn->imm != 0 || insn->off != 0) {
5004 verbose(env, "BPF_MOV uses reserved fields\n");
5005 return -EINVAL;
5006 }
5007
5008
5009 err = check_reg_arg(env, insn->src_reg, SRC_OP);
5010 if (err)
5011 return err;
5012 } else {
5013 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
5014 verbose(env, "BPF_MOV uses reserved fields\n");
5015 return -EINVAL;
5016 }
5017 }
5018
5019
5020 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
5021 if (err)
5022 return err;
5023
5024 if (BPF_SRC(insn->code) == BPF_X) {
5025 struct bpf_reg_state *src_reg = regs + insn->src_reg;
5026 struct bpf_reg_state *dst_reg = regs + insn->dst_reg;
5027
5028 if (BPF_CLASS(insn->code) == BPF_ALU64) {
5029
5030
5031
5032 *dst_reg = *src_reg;
5033 dst_reg->live |= REG_LIVE_WRITTEN;
5034 dst_reg->subreg_def = DEF_NOT_SUBREG;
5035 } else {
5036
5037 if (is_pointer_value(env, insn->src_reg)) {
5038 verbose(env,
5039 "R%d partial copy of pointer\n",
5040 insn->src_reg);
5041 return -EACCES;
5042 } else if (src_reg->type == SCALAR_VALUE) {
5043 *dst_reg = *src_reg;
5044 dst_reg->live |= REG_LIVE_WRITTEN;
5045 dst_reg->subreg_def = env->insn_idx + 1;
5046 } else {
5047 mark_reg_unknown(env, regs,
5048 insn->dst_reg);
5049 }
5050 coerce_reg_to_size(dst_reg, 4);
5051 }
5052 } else {
5053
5054
5055
5056
5057 mark_reg_unknown(env, regs, insn->dst_reg);
5058 regs[insn->dst_reg].type = SCALAR_VALUE;
5059 if (BPF_CLASS(insn->code) == BPF_ALU64) {
5060 __mark_reg_known(regs + insn->dst_reg,
5061 insn->imm);
5062 } else {
5063 __mark_reg_known(regs + insn->dst_reg,
5064 (u32)insn->imm);
5065 }
5066 }
5067
5068 } else if (opcode > BPF_END) {
5069 verbose(env, "invalid BPF_ALU opcode %x\n", opcode);
5070 return -EINVAL;
5071
5072 } else {
5073
5074 if (BPF_SRC(insn->code) == BPF_X) {
5075 if (insn->imm != 0 || insn->off != 0) {
5076 verbose(env, "BPF_ALU uses reserved fields\n");
5077 return -EINVAL;
5078 }
5079
5080 err = check_reg_arg(env, insn->src_reg, SRC_OP);
5081 if (err)
5082 return err;
5083 } else {
5084 if (insn->src_reg != BPF_REG_0 || insn->off != 0) {
5085 verbose(env, "BPF_ALU uses reserved fields\n");
5086 return -EINVAL;
5087 }
5088 }
5089
5090
5091 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
5092 if (err)
5093 return err;
5094
5095 if ((opcode == BPF_MOD || opcode == BPF_DIV) &&
5096 BPF_SRC(insn->code) == BPF_K && insn->imm == 0) {
5097 verbose(env, "div by zero\n");
5098 return -EINVAL;
5099 }
5100
5101 if ((opcode == BPF_LSH || opcode == BPF_RSH ||
5102 opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) {
5103 int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32;
5104
5105 if (insn->imm < 0 || insn->imm >= size) {
5106 verbose(env, "invalid shift %d\n", insn->imm);
5107 return -EINVAL;
5108 }
5109 }
5110
5111
5112 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
5113 if (err)
5114 return err;
5115
5116 return adjust_reg_min_max_vals(env, insn);
5117 }
5118
5119 return 0;
5120 }
5121
5122 static void __find_good_pkt_pointers(struct bpf_func_state *state,
5123 struct bpf_reg_state *dst_reg,
5124 enum bpf_reg_type type, u16 new_range)
5125 {
5126 struct bpf_reg_state *reg;
5127 int i;
5128
5129 for (i = 0; i < MAX_BPF_REG; i++) {
5130 reg = &state->regs[i];
5131 if (reg->type == type && reg->id == dst_reg->id)
5132
5133 reg->range = max(reg->range, new_range);
5134 }
5135
5136 bpf_for_each_spilled_reg(i, state, reg) {
5137 if (!reg)
5138 continue;
5139 if (reg->type == type && reg->id == dst_reg->id)
5140 reg->range = max(reg->range, new_range);
5141 }
5142 }
5143
5144 static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
5145 struct bpf_reg_state *dst_reg,
5146 enum bpf_reg_type type,
5147 bool range_right_open)
5148 {
5149 u16 new_range;
5150 int i;
5151
5152 if (dst_reg->off < 0 ||
5153 (dst_reg->off == 0 && range_right_open))
5154
5155 return;
5156
5157 if (dst_reg->umax_value > MAX_PACKET_OFF ||
5158 dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF)
5159
5160
5161
5162 return;
5163
5164 new_range = dst_reg->off;
5165 if (range_right_open)
5166 new_range--;
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215 for (i = 0; i <= vstate->curframe; i++)
5216 __find_good_pkt_pointers(vstate->frame[i], dst_reg, type,
5217 new_range);
5218 }
5219
5220
5221
5222
5223
5224
5225
5226 static int is_branch_taken(struct bpf_reg_state *reg, u64 val, u8 opcode,
5227 bool is_jmp32)
5228 {
5229 struct bpf_reg_state reg_lo;
5230 s64 sval;
5231
5232 if (__is_pointer_value(false, reg))
5233 return -1;
5234
5235 if (is_jmp32) {
5236 reg_lo = *reg;
5237 reg = ®_lo;
5238
5239
5240
5241
5242 coerce_reg_to_size(reg, 4);
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256 if ((reg->umax_value ^ reg->umin_value) &
5257 (1ULL << 31)) {
5258 reg->smin_value = S32_MIN;
5259 reg->smax_value = S32_MAX;
5260 }
5261 reg->smin_value = (s64)(s32)reg->smin_value;
5262 reg->smax_value = (s64)(s32)reg->smax_value;
5263
5264 val = (u32)val;
5265 sval = (s64)(s32)val;
5266 } else {
5267 sval = (s64)val;
5268 }
5269
5270 switch (opcode) {
5271 case BPF_JEQ:
5272 if (tnum_is_const(reg->var_off))
5273 return !!tnum_equals_const(reg->var_off, val);
5274 break;
5275 case BPF_JNE:
5276 if (tnum_is_const(reg->var_off))
5277 return !tnum_equals_const(reg->var_off, val);
5278 break;
5279 case BPF_JSET:
5280 if ((~reg->var_off.mask & reg->var_off.value) & val)
5281 return 1;
5282 if (!((reg->var_off.mask | reg->var_off.value) & val))
5283 return 0;
5284 break;
5285 case BPF_JGT:
5286 if (reg->umin_value > val)
5287 return 1;
5288 else if (reg->umax_value <= val)
5289 return 0;
5290 break;
5291 case BPF_JSGT:
5292 if (reg->smin_value > sval)
5293 return 1;
5294 else if (reg->smax_value < sval)
5295 return 0;
5296 break;
5297 case BPF_JLT:
5298 if (reg->umax_value < val)
5299 return 1;
5300 else if (reg->umin_value >= val)
5301 return 0;
5302 break;
5303 case BPF_JSLT:
5304 if (reg->smax_value < sval)
5305 return 1;
5306 else if (reg->smin_value >= sval)
5307 return 0;
5308 break;
5309 case BPF_JGE:
5310 if (reg->umin_value >= val)
5311 return 1;
5312 else if (reg->umax_value < val)
5313 return 0;
5314 break;
5315 case BPF_JSGE:
5316 if (reg->smin_value >= sval)
5317 return 1;
5318 else if (reg->smax_value < sval)
5319 return 0;
5320 break;
5321 case BPF_JLE:
5322 if (reg->umax_value <= val)
5323 return 1;
5324 else if (reg->umin_value > val)
5325 return 0;
5326 break;
5327 case BPF_JSLE:
5328 if (reg->smax_value <= sval)
5329 return 1;
5330 else if (reg->smin_value > sval)
5331 return 0;
5332 break;
5333 }
5334
5335 return -1;
5336 }
5337
5338
5339 static u64 gen_hi_min(struct tnum var)
5340 {
5341 return var.value & ~0xffffffffULL;
5342 }
5343
5344
5345 static u64 gen_hi_max(struct tnum var)
5346 {
5347 return (var.value | var.mask) & ~0xffffffffULL;
5348 }
5349
5350
5351
5352
5353 static bool cmp_val_with_extended_s64(s64 sval, struct bpf_reg_state *reg)
5354 {
5355 return ((s32)sval >= 0 &&
5356 reg->smin_value >= 0 && reg->smax_value <= S32_MAX) ||
5357 ((s32)sval < 0 &&
5358 reg->smax_value <= 0 && reg->smin_value >= S32_MIN);
5359 }
5360
5361
5362
5363
5364
5365
5366 static void set_upper_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5367 bool is_exclusive)
5368 {
5369 if (is_exclusive) {
5370
5371 if (bound == 0)
5372 return;
5373 bound--;
5374 }
5375 if (is_jmp32) {
5376
5377
5378
5379
5380
5381
5382 struct tnum t = tnum_range(0, bound);
5383
5384 t.mask |= ~0xffffffffULL;
5385 reg->var_off = tnum_intersect(reg->var_off, t);
5386
5387
5388 bound += gen_hi_max(reg->var_off);
5389 }
5390 reg->umax_value = min(reg->umax_value, bound);
5391 }
5392
5393
5394
5395
5396
5397
5398 static void set_lower_bound(struct bpf_reg_state *reg, u64 bound, bool is_jmp32,
5399 bool is_exclusive)
5400 {
5401 if (is_exclusive) {
5402
5403 if (bound == (is_jmp32 ? U32_MAX : U64_MAX))
5404 return;
5405 bound++;
5406 }
5407 if (is_jmp32) {
5408
5409
5410
5411
5412
5413
5414 struct tnum t = tnum_range(bound, U32_MAX);
5415
5416 t.mask |= ~0xffffffffULL;
5417 reg->var_off = tnum_intersect(reg->var_off, t);
5418
5419
5420 bound += gen_hi_min(reg->var_off);
5421 }
5422 reg->umin_value = max(reg->umin_value, bound);
5423 }
5424
5425
5426
5427
5428
5429
5430 static void reg_set_min_max(struct bpf_reg_state *true_reg,
5431 struct bpf_reg_state *false_reg, u64 val,
5432 u8 opcode, bool is_jmp32)
5433 {
5434 s64 sval;
5435
5436
5437
5438
5439
5440
5441
5442 if (__is_pointer_value(false, false_reg))
5443 return;
5444
5445 val = is_jmp32 ? (u32)val : val;
5446 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
5447
5448 switch (opcode) {
5449 case BPF_JEQ:
5450 case BPF_JNE:
5451 {
5452 struct bpf_reg_state *reg =
5453 opcode == BPF_JEQ ? true_reg : false_reg;
5454
5455
5456
5457
5458
5459 if (is_jmp32) {
5460 u64 old_v = reg->var_off.value;
5461 u64 hi_mask = ~0xffffffffULL;
5462
5463 reg->var_off.value = (old_v & hi_mask) | val;
5464 reg->var_off.mask &= hi_mask;
5465 } else {
5466 __mark_reg_known(reg, val);
5467 }
5468 break;
5469 }
5470 case BPF_JSET:
5471 false_reg->var_off = tnum_and(false_reg->var_off,
5472 tnum_const(~val));
5473 if (is_power_of_2(val))
5474 true_reg->var_off = tnum_or(true_reg->var_off,
5475 tnum_const(val));
5476 break;
5477 case BPF_JGE:
5478 case BPF_JGT:
5479 {
5480 set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5481 set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
5482 break;
5483 }
5484 case BPF_JSGE:
5485 case BPF_JSGT:
5486 {
5487 s64 false_smax = opcode == BPF_JSGT ? sval : sval - 1;
5488 s64 true_smin = opcode == BPF_JSGT ? sval + 1 : sval;
5489
5490
5491
5492
5493 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5494 break;
5495 false_reg->smax_value = min(false_reg->smax_value, false_smax);
5496 true_reg->smin_value = max(true_reg->smin_value, true_smin);
5497 break;
5498 }
5499 case BPF_JLE:
5500 case BPF_JLT:
5501 {
5502 set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5503 set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
5504 break;
5505 }
5506 case BPF_JSLE:
5507 case BPF_JSLT:
5508 {
5509 s64 false_smin = opcode == BPF_JSLT ? sval : sval + 1;
5510 s64 true_smax = opcode == BPF_JSLT ? sval - 1 : sval;
5511
5512 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5513 break;
5514 false_reg->smin_value = max(false_reg->smin_value, false_smin);
5515 true_reg->smax_value = min(true_reg->smax_value, true_smax);
5516 break;
5517 }
5518 default:
5519 break;
5520 }
5521
5522 __reg_deduce_bounds(false_reg);
5523 __reg_deduce_bounds(true_reg);
5524
5525 __reg_bound_offset(false_reg);
5526 __reg_bound_offset(true_reg);
5527
5528
5529
5530
5531 __update_reg_bounds(false_reg);
5532 __update_reg_bounds(true_reg);
5533 }
5534
5535
5536
5537
5538 static void reg_set_min_max_inv(struct bpf_reg_state *true_reg,
5539 struct bpf_reg_state *false_reg, u64 val,
5540 u8 opcode, bool is_jmp32)
5541 {
5542 s64 sval;
5543
5544 if (__is_pointer_value(false, false_reg))
5545 return;
5546
5547 val = is_jmp32 ? (u32)val : val;
5548 sval = is_jmp32 ? (s64)(s32)val : (s64)val;
5549
5550 switch (opcode) {
5551 case BPF_JEQ:
5552 case BPF_JNE:
5553 {
5554 struct bpf_reg_state *reg =
5555 opcode == BPF_JEQ ? true_reg : false_reg;
5556
5557 if (is_jmp32) {
5558 u64 old_v = reg->var_off.value;
5559 u64 hi_mask = ~0xffffffffULL;
5560
5561 reg->var_off.value = (old_v & hi_mask) | val;
5562 reg->var_off.mask &= hi_mask;
5563 } else {
5564 __mark_reg_known(reg, val);
5565 }
5566 break;
5567 }
5568 case BPF_JSET:
5569 false_reg->var_off = tnum_and(false_reg->var_off,
5570 tnum_const(~val));
5571 if (is_power_of_2(val))
5572 true_reg->var_off = tnum_or(true_reg->var_off,
5573 tnum_const(val));
5574 break;
5575 case BPF_JGE:
5576 case BPF_JGT:
5577 {
5578 set_lower_bound(false_reg, val, is_jmp32, opcode == BPF_JGE);
5579 set_upper_bound(true_reg, val, is_jmp32, opcode == BPF_JGT);
5580 break;
5581 }
5582 case BPF_JSGE:
5583 case BPF_JSGT:
5584 {
5585 s64 false_smin = opcode == BPF_JSGT ? sval : sval + 1;
5586 s64 true_smax = opcode == BPF_JSGT ? sval - 1 : sval;
5587
5588 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5589 break;
5590 false_reg->smin_value = max(false_reg->smin_value, false_smin);
5591 true_reg->smax_value = min(true_reg->smax_value, true_smax);
5592 break;
5593 }
5594 case BPF_JLE:
5595 case BPF_JLT:
5596 {
5597 set_upper_bound(false_reg, val, is_jmp32, opcode == BPF_JLE);
5598 set_lower_bound(true_reg, val, is_jmp32, opcode == BPF_JLT);
5599 break;
5600 }
5601 case BPF_JSLE:
5602 case BPF_JSLT:
5603 {
5604 s64 false_smax = opcode == BPF_JSLT ? sval : sval - 1;
5605 s64 true_smin = opcode == BPF_JSLT ? sval + 1 : sval;
5606
5607 if (is_jmp32 && !cmp_val_with_extended_s64(sval, false_reg))
5608 break;
5609 false_reg->smax_value = min(false_reg->smax_value, false_smax);
5610 true_reg->smin_value = max(true_reg->smin_value, true_smin);
5611 break;
5612 }
5613 default:
5614 break;
5615 }
5616
5617 __reg_deduce_bounds(false_reg);
5618 __reg_deduce_bounds(true_reg);
5619
5620 __reg_bound_offset(false_reg);
5621 __reg_bound_offset(true_reg);
5622
5623
5624
5625
5626 __update_reg_bounds(false_reg);
5627 __update_reg_bounds(true_reg);
5628 }
5629
5630
5631 static void __reg_combine_min_max(struct bpf_reg_state *src_reg,
5632 struct bpf_reg_state *dst_reg)
5633 {
5634 src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value,
5635 dst_reg->umin_value);
5636 src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value,
5637 dst_reg->umax_value);
5638 src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value,
5639 dst_reg->smin_value);
5640 src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value,
5641 dst_reg->smax_value);
5642 src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off,
5643 dst_reg->var_off);
5644
5645 __update_reg_bounds(src_reg);
5646 __update_reg_bounds(dst_reg);
5647
5648 __reg_deduce_bounds(src_reg);
5649 __reg_deduce_bounds(dst_reg);
5650
5651 __reg_bound_offset(src_reg);
5652 __reg_bound_offset(dst_reg);
5653
5654
5655
5656
5657 __update_reg_bounds(src_reg);
5658 __update_reg_bounds(dst_reg);
5659 }
5660
5661 static void reg_combine_min_max(struct bpf_reg_state *true_src,
5662 struct bpf_reg_state *true_dst,
5663 struct bpf_reg_state *false_src,
5664 struct bpf_reg_state *false_dst,
5665 u8 opcode)
5666 {
5667 switch (opcode) {
5668 case BPF_JEQ:
5669 __reg_combine_min_max(true_src, true_dst);
5670 break;
5671 case BPF_JNE:
5672 __reg_combine_min_max(false_src, false_dst);
5673 break;
5674 }
5675 }
5676
5677 static void mark_ptr_or_null_reg(struct bpf_func_state *state,
5678 struct bpf_reg_state *reg, u32 id,
5679 bool is_null)
5680 {
5681 if (reg_type_may_be_null(reg->type) && reg->id == id) {
5682
5683
5684
5685
5686 if (WARN_ON_ONCE(reg->smin_value || reg->smax_value ||
5687 !tnum_equals_const(reg->var_off, 0) ||
5688 reg->off)) {
5689 __mark_reg_known_zero(reg);
5690 reg->off = 0;
5691 }
5692 if (is_null) {
5693 reg->type = SCALAR_VALUE;
5694 } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) {
5695 if (reg->map_ptr->inner_map_meta) {
5696 reg->type = CONST_PTR_TO_MAP;
5697 reg->map_ptr = reg->map_ptr->inner_map_meta;
5698 } else if (reg->map_ptr->map_type ==
5699 BPF_MAP_TYPE_XSKMAP) {
5700 reg->type = PTR_TO_XDP_SOCK;
5701 } else {
5702 reg->type = PTR_TO_MAP_VALUE;
5703 }
5704 } else if (reg->type == PTR_TO_SOCKET_OR_NULL) {
5705 reg->type = PTR_TO_SOCKET;
5706 } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) {
5707 reg->type = PTR_TO_SOCK_COMMON;
5708 } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) {
5709 reg->type = PTR_TO_TCP_SOCK;
5710 }
5711 if (is_null) {
5712
5713
5714
5715
5716 reg->id = 0;
5717 reg->ref_obj_id = 0;
5718 } else if (!reg_may_point_to_spin_lock(reg)) {
5719
5720
5721
5722
5723
5724
5725 reg->id = 0;
5726 }
5727 }
5728 }
5729
5730 static void __mark_ptr_or_null_regs(struct bpf_func_state *state, u32 id,
5731 bool is_null)
5732 {
5733 struct bpf_reg_state *reg;
5734 int i;
5735
5736 for (i = 0; i < MAX_BPF_REG; i++)
5737 mark_ptr_or_null_reg(state, &state->regs[i], id, is_null);
5738
5739 bpf_for_each_spilled_reg(i, state, reg) {
5740 if (!reg)
5741 continue;
5742 mark_ptr_or_null_reg(state, reg, id, is_null);
5743 }
5744 }
5745
5746
5747
5748
5749 static void mark_ptr_or_null_regs(struct bpf_verifier_state *vstate, u32 regno,
5750 bool is_null)
5751 {
5752 struct bpf_func_state *state = vstate->frame[vstate->curframe];
5753 struct bpf_reg_state *regs = state->regs;
5754 u32 ref_obj_id = regs[regno].ref_obj_id;
5755 u32 id = regs[regno].id;
5756 int i;
5757
5758 if (ref_obj_id && ref_obj_id == id && is_null)
5759
5760
5761
5762
5763 WARN_ON_ONCE(release_reference_state(state, id));
5764
5765 for (i = 0; i <= vstate->curframe; i++)
5766 __mark_ptr_or_null_regs(vstate->frame[i], id, is_null);
5767 }
5768
5769 static bool try_match_pkt_pointers(const struct bpf_insn *insn,
5770 struct bpf_reg_state *dst_reg,
5771 struct bpf_reg_state *src_reg,
5772 struct bpf_verifier_state *this_branch,
5773 struct bpf_verifier_state *other_branch)
5774 {
5775 if (BPF_SRC(insn->code) != BPF_X)
5776 return false;
5777
5778
5779 if (BPF_CLASS(insn->code) == BPF_JMP32)
5780 return false;
5781
5782 switch (BPF_OP(insn->code)) {
5783 case BPF_JGT:
5784 if ((dst_reg->type == PTR_TO_PACKET &&
5785 src_reg->type == PTR_TO_PACKET_END) ||
5786 (dst_reg->type == PTR_TO_PACKET_META &&
5787 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5788
5789 find_good_pkt_pointers(this_branch, dst_reg,
5790 dst_reg->type, false);
5791 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5792 src_reg->type == PTR_TO_PACKET) ||
5793 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5794 src_reg->type == PTR_TO_PACKET_META)) {
5795
5796 find_good_pkt_pointers(other_branch, src_reg,
5797 src_reg->type, true);
5798 } else {
5799 return false;
5800 }
5801 break;
5802 case BPF_JLT:
5803 if ((dst_reg->type == PTR_TO_PACKET &&
5804 src_reg->type == PTR_TO_PACKET_END) ||
5805 (dst_reg->type == PTR_TO_PACKET_META &&
5806 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5807
5808 find_good_pkt_pointers(other_branch, dst_reg,
5809 dst_reg->type, true);
5810 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5811 src_reg->type == PTR_TO_PACKET) ||
5812 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5813 src_reg->type == PTR_TO_PACKET_META)) {
5814
5815 find_good_pkt_pointers(this_branch, src_reg,
5816 src_reg->type, false);
5817 } else {
5818 return false;
5819 }
5820 break;
5821 case BPF_JGE:
5822 if ((dst_reg->type == PTR_TO_PACKET &&
5823 src_reg->type == PTR_TO_PACKET_END) ||
5824 (dst_reg->type == PTR_TO_PACKET_META &&
5825 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5826
5827 find_good_pkt_pointers(this_branch, dst_reg,
5828 dst_reg->type, true);
5829 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5830 src_reg->type == PTR_TO_PACKET) ||
5831 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5832 src_reg->type == PTR_TO_PACKET_META)) {
5833
5834 find_good_pkt_pointers(other_branch, src_reg,
5835 src_reg->type, false);
5836 } else {
5837 return false;
5838 }
5839 break;
5840 case BPF_JLE:
5841 if ((dst_reg->type == PTR_TO_PACKET &&
5842 src_reg->type == PTR_TO_PACKET_END) ||
5843 (dst_reg->type == PTR_TO_PACKET_META &&
5844 reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) {
5845
5846 find_good_pkt_pointers(other_branch, dst_reg,
5847 dst_reg->type, false);
5848 } else if ((dst_reg->type == PTR_TO_PACKET_END &&
5849 src_reg->type == PTR_TO_PACKET) ||
5850 (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) &&
5851 src_reg->type == PTR_TO_PACKET_META)) {
5852
5853 find_good_pkt_pointers(this_branch, src_reg,
5854 src_reg->type, true);
5855 } else {
5856 return false;
5857 }
5858 break;
5859 default:
5860 return false;
5861 }
5862
5863 return true;
5864 }
5865
5866 static int check_cond_jmp_op(struct bpf_verifier_env *env,
5867 struct bpf_insn *insn, int *insn_idx)
5868 {
5869 struct bpf_verifier_state *this_branch = env->cur_state;
5870 struct bpf_verifier_state *other_branch;
5871 struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs;
5872 struct bpf_reg_state *dst_reg, *other_branch_regs, *src_reg = NULL;
5873 u8 opcode = BPF_OP(insn->code);
5874 bool is_jmp32;
5875 int pred = -1;
5876 int err;
5877
5878
5879 if (opcode == BPF_JA || opcode > BPF_JSLE) {
5880 verbose(env, "invalid BPF_JMP/JMP32 opcode %x\n", opcode);
5881 return -EINVAL;
5882 }
5883
5884 if (BPF_SRC(insn->code) == BPF_X) {
5885 if (insn->imm != 0) {
5886 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
5887 return -EINVAL;
5888 }
5889
5890
5891 err = check_reg_arg(env, insn->src_reg, SRC_OP);
5892 if (err)
5893 return err;
5894
5895 if (is_pointer_value(env, insn->src_reg)) {
5896 verbose(env, "R%d pointer comparison prohibited\n",
5897 insn->src_reg);
5898 return -EACCES;
5899 }
5900 src_reg = ®s[insn->src_reg];
5901 } else {
5902 if (insn->src_reg != BPF_REG_0) {
5903 verbose(env, "BPF_JMP/JMP32 uses reserved fields\n");
5904 return -EINVAL;
5905 }
5906 }
5907
5908
5909 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
5910 if (err)
5911 return err;
5912
5913 dst_reg = ®s[insn->dst_reg];
5914 is_jmp32 = BPF_CLASS(insn->code) == BPF_JMP32;
5915
5916 if (BPF_SRC(insn->code) == BPF_K)
5917 pred = is_branch_taken(dst_reg, insn->imm,
5918 opcode, is_jmp32);
5919 else if (src_reg->type == SCALAR_VALUE &&
5920 tnum_is_const(src_reg->var_off))
5921 pred = is_branch_taken(dst_reg, src_reg->var_off.value,
5922 opcode, is_jmp32);
5923 if (pred >= 0) {
5924 err = mark_chain_precision(env, insn->dst_reg);
5925 if (BPF_SRC(insn->code) == BPF_X && !err)
5926 err = mark_chain_precision(env, insn->src_reg);
5927 if (err)
5928 return err;
5929 }
5930 if (pred == 1) {
5931
5932 *insn_idx += insn->off;
5933 return 0;
5934 } else if (pred == 0) {
5935
5936
5937
5938 return 0;
5939 }
5940
5941 other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx,
5942 false);
5943 if (!other_branch)
5944 return -EFAULT;
5945 other_branch_regs = other_branch->frame[other_branch->curframe]->regs;
5946
5947
5948
5949
5950
5951
5952
5953
5954 if (BPF_SRC(insn->code) == BPF_X) {
5955 struct bpf_reg_state *src_reg = ®s[insn->src_reg];
5956 struct bpf_reg_state lo_reg0 = *dst_reg;
5957 struct bpf_reg_state lo_reg1 = *src_reg;
5958 struct bpf_reg_state *src_lo, *dst_lo;
5959
5960 dst_lo = &lo_reg0;
5961 src_lo = &lo_reg1;
5962 coerce_reg_to_size(dst_lo, 4);
5963 coerce_reg_to_size(src_lo, 4);
5964
5965 if (dst_reg->type == SCALAR_VALUE &&
5966 src_reg->type == SCALAR_VALUE) {
5967 if (tnum_is_const(src_reg->var_off) ||
5968 (is_jmp32 && tnum_is_const(src_lo->var_off)))
5969 reg_set_min_max(&other_branch_regs[insn->dst_reg],
5970 dst_reg,
5971 is_jmp32
5972 ? src_lo->var_off.value
5973 : src_reg->var_off.value,
5974 opcode, is_jmp32);
5975 else if (tnum_is_const(dst_reg->var_off) ||
5976 (is_jmp32 && tnum_is_const(dst_lo->var_off)))
5977 reg_set_min_max_inv(&other_branch_regs[insn->src_reg],
5978 src_reg,
5979 is_jmp32
5980 ? dst_lo->var_off.value
5981 : dst_reg->var_off.value,
5982 opcode, is_jmp32);
5983 else if (!is_jmp32 &&
5984 (opcode == BPF_JEQ || opcode == BPF_JNE))
5985
5986 reg_combine_min_max(&other_branch_regs[insn->src_reg],
5987 &other_branch_regs[insn->dst_reg],
5988 src_reg, dst_reg, opcode);
5989 }
5990 } else if (dst_reg->type == SCALAR_VALUE) {
5991 reg_set_min_max(&other_branch_regs[insn->dst_reg],
5992 dst_reg, insn->imm, opcode, is_jmp32);
5993 }
5994
5995
5996
5997
5998
5999 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
6000 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
6001 reg_type_may_be_null(dst_reg->type)) {
6002
6003
6004
6005 mark_ptr_or_null_regs(this_branch, insn->dst_reg,
6006 opcode == BPF_JNE);
6007 mark_ptr_or_null_regs(other_branch, insn->dst_reg,
6008 opcode == BPF_JEQ);
6009 } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg],
6010 this_branch, other_branch) &&
6011 is_pointer_value(env, insn->dst_reg)) {
6012 verbose(env, "R%d pointer comparison prohibited\n",
6013 insn->dst_reg);
6014 return -EACCES;
6015 }
6016 if (env->log.level & BPF_LOG_LEVEL)
6017 print_verifier_state(env, this_branch->frame[this_branch->curframe]);
6018 return 0;
6019 }
6020
6021
6022 static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn)
6023 {
6024 struct bpf_insn_aux_data *aux = cur_aux(env);
6025 struct bpf_reg_state *regs = cur_regs(env);
6026 struct bpf_map *map;
6027 int err;
6028
6029 if (BPF_SIZE(insn->code) != BPF_DW) {
6030 verbose(env, "invalid BPF_LD_IMM insn\n");
6031 return -EINVAL;
6032 }
6033 if (insn->off != 0) {
6034 verbose(env, "BPF_LD_IMM64 uses reserved fields\n");
6035 return -EINVAL;
6036 }
6037
6038 err = check_reg_arg(env, insn->dst_reg, DST_OP);
6039 if (err)
6040 return err;
6041
6042 if (insn->src_reg == 0) {
6043 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
6044
6045 regs[insn->dst_reg].type = SCALAR_VALUE;
6046 __mark_reg_known(®s[insn->dst_reg], imm);
6047 return 0;
6048 }
6049
6050 map = env->used_maps[aux->map_index];
6051 mark_reg_known_zero(env, regs, insn->dst_reg);
6052 regs[insn->dst_reg].map_ptr = map;
6053
6054 if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) {
6055 regs[insn->dst_reg].type = PTR_TO_MAP_VALUE;
6056 regs[insn->dst_reg].off = aux->map_off;
6057 if (map_value_has_spin_lock(map))
6058 regs[insn->dst_reg].id = ++env->id_gen;
6059 } else if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
6060 regs[insn->dst_reg].type = CONST_PTR_TO_MAP;
6061 } else {
6062 verbose(env, "bpf verifier is misconfigured\n");
6063 return -EINVAL;
6064 }
6065
6066 return 0;
6067 }
6068
6069 static bool may_access_skb(enum bpf_prog_type type)
6070 {
6071 switch (type) {
6072 case BPF_PROG_TYPE_SOCKET_FILTER:
6073 case BPF_PROG_TYPE_SCHED_CLS:
6074 case BPF_PROG_TYPE_SCHED_ACT:
6075 return true;
6076 default:
6077 return false;
6078 }
6079 }
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096 static int check_ld_abs(struct bpf_verifier_env *env, struct bpf_insn *insn)
6097 {
6098 struct bpf_reg_state *regs = cur_regs(env);
6099 static const int ctx_reg = BPF_REG_6;
6100 u8 mode = BPF_MODE(insn->code);
6101 int i, err;
6102
6103 if (!may_access_skb(env->prog->type)) {
6104 verbose(env, "BPF_LD_[ABS|IND] instructions not allowed for this program type\n");
6105 return -EINVAL;
6106 }
6107
6108 if (!env->ops->gen_ld_abs) {
6109 verbose(env, "bpf verifier is misconfigured\n");
6110 return -EINVAL;
6111 }
6112
6113 if (env->subprog_cnt > 1) {
6114
6115
6116
6117
6118
6119
6120
6121 verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n");
6122 return -EINVAL;
6123 }
6124
6125 if (insn->dst_reg != BPF_REG_0 || insn->off != 0 ||
6126 BPF_SIZE(insn->code) == BPF_DW ||
6127 (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) {
6128 verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n");
6129 return -EINVAL;
6130 }
6131
6132
6133 err = check_reg_arg(env, ctx_reg, SRC_OP);
6134 if (err)
6135 return err;
6136
6137
6138
6139
6140
6141 err = check_reference_leak(env);
6142 if (err) {
6143 verbose(env, "BPF_LD_[ABS|IND] cannot be mixed with socket references\n");
6144 return err;
6145 }
6146
6147 if (env->cur_state->active_spin_lock) {
6148 verbose(env, "BPF_LD_[ABS|IND] cannot be used inside bpf_spin_lock-ed region\n");
6149 return -EINVAL;
6150 }
6151
6152 if (regs[ctx_reg].type != PTR_TO_CTX) {
6153 verbose(env,
6154 "at the time of BPF_LD_ABS|IND R6 != pointer to skb\n");
6155 return -EINVAL;
6156 }
6157
6158 if (mode == BPF_IND) {
6159
6160 err = check_reg_arg(env, insn->src_reg, SRC_OP);
6161 if (err)
6162 return err;
6163 }
6164
6165 err = check_ctx_reg(env, ®s[ctx_reg], ctx_reg);
6166 if (err < 0)
6167 return err;
6168
6169
6170 for (i = 0; i < CALLER_SAVED_REGS; i++) {
6171 mark_reg_not_init(env, regs, caller_saved[i]);
6172 check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK);
6173 }
6174
6175
6176
6177
6178
6179 mark_reg_unknown(env, regs, BPF_REG_0);
6180
6181 regs[BPF_REG_0].subreg_def = env->insn_idx + 1;
6182 return 0;
6183 }
6184
6185 static int check_return_code(struct bpf_verifier_env *env)
6186 {
6187 struct tnum enforce_attach_type_range = tnum_unknown;
6188 struct bpf_reg_state *reg;
6189 struct tnum range = tnum_range(0, 1);
6190
6191 switch (env->prog->type) {
6192 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
6193 if (env->prog->expected_attach_type == BPF_CGROUP_UDP4_RECVMSG ||
6194 env->prog->expected_attach_type == BPF_CGROUP_UDP6_RECVMSG)
6195 range = tnum_range(1, 1);
6196 break;
6197 case BPF_PROG_TYPE_CGROUP_SKB:
6198 if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) {
6199 range = tnum_range(0, 3);
6200 enforce_attach_type_range = tnum_range(2, 3);
6201 }
6202 break;
6203 case BPF_PROG_TYPE_CGROUP_SOCK:
6204 case BPF_PROG_TYPE_SOCK_OPS:
6205 case BPF_PROG_TYPE_CGROUP_DEVICE:
6206 case BPF_PROG_TYPE_CGROUP_SYSCTL:
6207 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
6208 break;
6209 default:
6210 return 0;
6211 }
6212
6213 reg = cur_regs(env) + BPF_REG_0;
6214 if (reg->type != SCALAR_VALUE) {
6215 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
6216 reg_type_str[reg->type]);
6217 return -EINVAL;
6218 }
6219
6220 if (!tnum_in(range, reg->var_off)) {
6221 char tn_buf[48];
6222
6223 verbose(env, "At program exit the register R0 ");
6224 if (!tnum_is_unknown(reg->var_off)) {
6225 tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off);
6226 verbose(env, "has value %s", tn_buf);
6227 } else {
6228 verbose(env, "has unknown scalar value");
6229 }
6230 tnum_strn(tn_buf, sizeof(tn_buf), range);
6231 verbose(env, " should have been in %s\n", tn_buf);
6232 return -EINVAL;
6233 }
6234
6235 if (!tnum_is_unknown(enforce_attach_type_range) &&
6236 tnum_in(enforce_attach_type_range, reg->var_off))
6237 env->prog->enforce_expected_attach_type = 1;
6238 return 0;
6239 }
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274 enum {
6275 DISCOVERED = 0x10,
6276 EXPLORED = 0x20,
6277 FALLTHROUGH = 1,
6278 BRANCH = 2,
6279 };
6280
6281 static u32 state_htab_size(struct bpf_verifier_env *env)
6282 {
6283 return env->prog->len;
6284 }
6285
6286 static struct bpf_verifier_state_list **explored_state(
6287 struct bpf_verifier_env *env,
6288 int idx)
6289 {
6290 struct bpf_verifier_state *cur = env->cur_state;
6291 struct bpf_func_state *state = cur->frame[cur->curframe];
6292
6293 return &env->explored_states[(idx ^ state->callsite) % state_htab_size(env)];
6294 }
6295
6296 static void init_explored_state(struct bpf_verifier_env *env, int idx)
6297 {
6298 env->insn_aux_data[idx].prune_point = true;
6299 }
6300
6301
6302
6303
6304
6305
6306 static int push_insn(int t, int w, int e, struct bpf_verifier_env *env,
6307 bool loop_ok)
6308 {
6309 int *insn_stack = env->cfg.insn_stack;
6310 int *insn_state = env->cfg.insn_state;
6311
6312 if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH))
6313 return 0;
6314
6315 if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH))
6316 return 0;
6317
6318 if (w < 0 || w >= env->prog->len) {
6319 verbose_linfo(env, t, "%d: ", t);
6320 verbose(env, "jump out of range from insn %d to %d\n", t, w);
6321 return -EINVAL;
6322 }
6323
6324 if (e == BRANCH)
6325
6326 init_explored_state(env, w);
6327
6328 if (insn_state[w] == 0) {
6329
6330 insn_state[t] = DISCOVERED | e;
6331 insn_state[w] = DISCOVERED;
6332 if (env->cfg.cur_stack >= env->prog->len)
6333 return -E2BIG;
6334 insn_stack[env->cfg.cur_stack++] = w;
6335 return 1;
6336 } else if ((insn_state[w] & 0xF0) == DISCOVERED) {
6337 if (loop_ok && env->allow_ptr_leaks)
6338 return 0;
6339 verbose_linfo(env, t, "%d: ", t);
6340 verbose_linfo(env, w, "%d: ", w);
6341 verbose(env, "back-edge from insn %d to %d\n", t, w);
6342 return -EINVAL;
6343 } else if (insn_state[w] == EXPLORED) {
6344
6345 insn_state[t] = DISCOVERED | e;
6346 } else {
6347 verbose(env, "insn state internal bug\n");
6348 return -EFAULT;
6349 }
6350 return 0;
6351 }
6352
6353
6354
6355
6356 static int check_cfg(struct bpf_verifier_env *env)
6357 {
6358 struct bpf_insn *insns = env->prog->insnsi;
6359 int insn_cnt = env->prog->len;
6360 int *insn_stack, *insn_state;
6361 int ret = 0;
6362 int i, t;
6363
6364 insn_state = env->cfg.insn_state = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
6365 if (!insn_state)
6366 return -ENOMEM;
6367
6368 insn_stack = env->cfg.insn_stack = kvcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
6369 if (!insn_stack) {
6370 kvfree(insn_state);
6371 return -ENOMEM;
6372 }
6373
6374 insn_state[0] = DISCOVERED;
6375 insn_stack[0] = 0;
6376 env->cfg.cur_stack = 1;
6377
6378 peek_stack:
6379 if (env->cfg.cur_stack == 0)
6380 goto check_state;
6381 t = insn_stack[env->cfg.cur_stack - 1];
6382
6383 if (BPF_CLASS(insns[t].code) == BPF_JMP ||
6384 BPF_CLASS(insns[t].code) == BPF_JMP32) {
6385 u8 opcode = BPF_OP(insns[t].code);
6386
6387 if (opcode == BPF_EXIT) {
6388 goto mark_explored;
6389 } else if (opcode == BPF_CALL) {
6390 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
6391 if (ret == 1)
6392 goto peek_stack;
6393 else if (ret < 0)
6394 goto err_free;
6395 if (t + 1 < insn_cnt)
6396 init_explored_state(env, t + 1);
6397 if (insns[t].src_reg == BPF_PSEUDO_CALL) {
6398 init_explored_state(env, t);
6399 ret = push_insn(t, t + insns[t].imm + 1, BRANCH,
6400 env, false);
6401 if (ret == 1)
6402 goto peek_stack;
6403 else if (ret < 0)
6404 goto err_free;
6405 }
6406 } else if (opcode == BPF_JA) {
6407 if (BPF_SRC(insns[t].code) != BPF_K) {
6408 ret = -EINVAL;
6409 goto err_free;
6410 }
6411
6412 ret = push_insn(t, t + insns[t].off + 1,
6413 FALLTHROUGH, env, true);
6414 if (ret == 1)
6415 goto peek_stack;
6416 else if (ret < 0)
6417 goto err_free;
6418
6419
6420
6421
6422 init_explored_state(env, t + insns[t].off + 1);
6423
6424
6425
6426 if (t + 1 < insn_cnt)
6427 init_explored_state(env, t + 1);
6428 } else {
6429
6430 init_explored_state(env, t);
6431 ret = push_insn(t, t + 1, FALLTHROUGH, env, true);
6432 if (ret == 1)
6433 goto peek_stack;
6434 else if (ret < 0)
6435 goto err_free;
6436
6437 ret = push_insn(t, t + insns[t].off + 1, BRANCH, env, true);
6438 if (ret == 1)
6439 goto peek_stack;
6440 else if (ret < 0)
6441 goto err_free;
6442 }
6443 } else {
6444
6445
6446
6447 ret = push_insn(t, t + 1, FALLTHROUGH, env, false);
6448 if (ret == 1)
6449 goto peek_stack;
6450 else if (ret < 0)
6451 goto err_free;
6452 }
6453
6454 mark_explored:
6455 insn_state[t] = EXPLORED;
6456 if (env->cfg.cur_stack-- <= 0) {
6457 verbose(env, "pop stack internal bug\n");
6458 ret = -EFAULT;
6459 goto err_free;
6460 }
6461 goto peek_stack;
6462
6463 check_state:
6464 for (i = 0; i < insn_cnt; i++) {
6465 if (insn_state[i] != EXPLORED) {
6466 verbose(env, "unreachable insn %d\n", i);
6467 ret = -EINVAL;
6468 goto err_free;
6469 }
6470 }
6471 ret = 0;
6472
6473 err_free:
6474 kvfree(insn_state);
6475 kvfree(insn_stack);
6476 env->cfg.insn_state = env->cfg.insn_stack = NULL;
6477 return ret;
6478 }
6479
6480
6481 #define MIN_BPF_FUNCINFO_SIZE 8
6482 #define MAX_FUNCINFO_REC_SIZE 252
6483
6484 static int check_btf_func(struct bpf_verifier_env *env,
6485 const union bpf_attr *attr,
6486 union bpf_attr __user *uattr)
6487 {
6488 u32 i, nfuncs, urec_size, min_size;
6489 u32 krec_size = sizeof(struct bpf_func_info);
6490 struct bpf_func_info *krecord;
6491 const struct btf_type *type;
6492 struct bpf_prog *prog;
6493 const struct btf *btf;
6494 void __user *urecord;
6495 u32 prev_offset = 0;
6496 int ret = 0;
6497
6498 nfuncs = attr->func_info_cnt;
6499 if (!nfuncs)
6500 return 0;
6501
6502 if (nfuncs != env->subprog_cnt) {
6503 verbose(env, "number of funcs in func_info doesn't match number of subprogs\n");
6504 return -EINVAL;
6505 }
6506
6507 urec_size = attr->func_info_rec_size;
6508 if (urec_size < MIN_BPF_FUNCINFO_SIZE ||
6509 urec_size > MAX_FUNCINFO_REC_SIZE ||
6510 urec_size % sizeof(u32)) {
6511 verbose(env, "invalid func info rec size %u\n", urec_size);
6512 return -EINVAL;
6513 }
6514
6515 prog = env->prog;
6516 btf = prog->aux->btf;
6517
6518 urecord = u64_to_user_ptr(attr->func_info);
6519 min_size = min_t(u32, krec_size, urec_size);
6520
6521 krecord = kvcalloc(nfuncs, krec_size, GFP_KERNEL | __GFP_NOWARN);
6522 if (!krecord)
6523 return -ENOMEM;
6524
6525 for (i = 0; i < nfuncs; i++) {
6526 ret = bpf_check_uarg_tail_zero(urecord, krec_size, urec_size);
6527 if (ret) {
6528 if (ret == -E2BIG) {
6529 verbose(env, "nonzero tailing record in func info");
6530
6531
6532
6533 if (put_user(min_size, &uattr->func_info_rec_size))
6534 ret = -EFAULT;
6535 }
6536 goto err_free;
6537 }
6538
6539 if (copy_from_user(&krecord[i], urecord, min_size)) {
6540 ret = -EFAULT;
6541 goto err_free;
6542 }
6543
6544
6545 if (i == 0) {
6546 if (krecord[i].insn_off) {
6547 verbose(env,
6548 "nonzero insn_off %u for the first func info record",
6549 krecord[i].insn_off);
6550 ret = -EINVAL;
6551 goto err_free;
6552 }
6553 } else if (krecord[i].insn_off <= prev_offset) {
6554 verbose(env,
6555 "same or smaller insn offset (%u) than previous func info record (%u)",
6556 krecord[i].insn_off, prev_offset);
6557 ret = -EINVAL;
6558 goto err_free;
6559 }
6560
6561 if (env->subprog_info[i].start != krecord[i].insn_off) {
6562 verbose(env, "func_info BTF section doesn't match subprog layout in BPF program\n");
6563 ret = -EINVAL;
6564 goto err_free;
6565 }
6566
6567
6568 type = btf_type_by_id(btf, krecord[i].type_id);
6569 if (!type || BTF_INFO_KIND(type->info) != BTF_KIND_FUNC) {
6570 verbose(env, "invalid type id %d in func info",
6571 krecord[i].type_id);
6572 ret = -EINVAL;
6573 goto err_free;
6574 }
6575
6576 prev_offset = krecord[i].insn_off;
6577 urecord += urec_size;
6578 }
6579
6580 prog->aux->func_info = krecord;
6581 prog->aux->func_info_cnt = nfuncs;
6582 return 0;
6583
6584 err_free:
6585 kvfree(krecord);
6586 return ret;
6587 }
6588
6589 static void adjust_btf_func(struct bpf_verifier_env *env)
6590 {
6591 int i;
6592
6593 if (!env->prog->aux->func_info)
6594 return;
6595
6596 for (i = 0; i < env->subprog_cnt; i++)
6597 env->prog->aux->func_info[i].insn_off = env->subprog_info[i].start;
6598 }
6599
6600 #define MIN_BPF_LINEINFO_SIZE (offsetof(struct bpf_line_info, line_col) + \
6601 sizeof(((struct bpf_line_info *)(0))->line_col))
6602 #define MAX_LINEINFO_REC_SIZE MAX_FUNCINFO_REC_SIZE
6603
6604 static int check_btf_line(struct bpf_verifier_env *env,
6605 const union bpf_attr *attr,
6606 union bpf_attr __user *uattr)
6607 {
6608 u32 i, s, nr_linfo, ncopy, expected_size, rec_size, prev_offset = 0;
6609 struct bpf_subprog_info *sub;
6610 struct bpf_line_info *linfo;
6611 struct bpf_prog *prog;
6612 const struct btf *btf;
6613 void __user *ulinfo;
6614 int err;
6615
6616 nr_linfo = attr->line_info_cnt;
6617 if (!nr_linfo)
6618 return 0;
6619
6620 rec_size = attr->line_info_rec_size;
6621 if (rec_size < MIN_BPF_LINEINFO_SIZE ||
6622 rec_size > MAX_LINEINFO_REC_SIZE ||
6623 rec_size & (sizeof(u32) - 1))
6624 return -EINVAL;
6625
6626
6627
6628
6629 linfo = kvcalloc(nr_linfo, sizeof(struct bpf_line_info),
6630 GFP_KERNEL | __GFP_NOWARN);
6631 if (!linfo)
6632 return -ENOMEM;
6633
6634 prog = env->prog;
6635 btf = prog->aux->btf;
6636
6637 s = 0;
6638 sub = env->subprog_info;
6639 ulinfo = u64_to_user_ptr(attr->line_info);
6640 expected_size = sizeof(struct bpf_line_info);
6641 ncopy = min_t(u32, expected_size, rec_size);
6642 for (i = 0; i < nr_linfo; i++) {
6643 err = bpf_check_uarg_tail_zero(ulinfo, expected_size, rec_size);
6644 if (err) {
6645 if (err == -E2BIG) {
6646 verbose(env, "nonzero tailing record in line_info");
6647 if (put_user(expected_size,
6648 &uattr->line_info_rec_size))
6649 err = -EFAULT;
6650 }
6651 goto err_free;
6652 }
6653
6654 if (copy_from_user(&linfo[i], ulinfo, ncopy)) {
6655 err = -EFAULT;
6656 goto err_free;
6657 }
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670 if ((i && linfo[i].insn_off <= prev_offset) ||
6671 linfo[i].insn_off >= prog->len) {
6672 verbose(env, "Invalid line_info[%u].insn_off:%u (prev_offset:%u prog->len:%u)\n",
6673 i, linfo[i].insn_off, prev_offset,
6674 prog->len);
6675 err = -EINVAL;
6676 goto err_free;
6677 }
6678
6679 if (!prog->insnsi[linfo[i].insn_off].code) {
6680 verbose(env,
6681 "Invalid insn code at line_info[%u].insn_off\n",
6682 i);
6683 err = -EINVAL;
6684 goto err_free;
6685 }
6686
6687 if (!btf_name_by_offset(btf, linfo[i].line_off) ||
6688 !btf_name_by_offset(btf, linfo[i].file_name_off)) {
6689 verbose(env, "Invalid line_info[%u].line_off or .file_name_off\n", i);
6690 err = -EINVAL;
6691 goto err_free;
6692 }
6693
6694 if (s != env->subprog_cnt) {
6695 if (linfo[i].insn_off == sub[s].start) {
6696 sub[s].linfo_idx = i;
6697 s++;
6698 } else if (sub[s].start < linfo[i].insn_off) {
6699 verbose(env, "missing bpf_line_info for func#%u\n", s);
6700 err = -EINVAL;
6701 goto err_free;
6702 }
6703 }
6704
6705 prev_offset = linfo[i].insn_off;
6706 ulinfo += rec_size;
6707 }
6708
6709 if (s != env->subprog_cnt) {
6710 verbose(env, "missing bpf_line_info for %u funcs starting from func#%u\n",
6711 env->subprog_cnt - s, s);
6712 err = -EINVAL;
6713 goto err_free;
6714 }
6715
6716 prog->aux->linfo = linfo;
6717 prog->aux->nr_linfo = nr_linfo;
6718
6719 return 0;
6720
6721 err_free:
6722 kvfree(linfo);
6723 return err;
6724 }
6725
6726 static int check_btf_info(struct bpf_verifier_env *env,
6727 const union bpf_attr *attr,
6728 union bpf_attr __user *uattr)
6729 {
6730 struct btf *btf;
6731 int err;
6732
6733 if (!attr->func_info_cnt && !attr->line_info_cnt)
6734 return 0;
6735
6736 btf = btf_get_by_fd(attr->prog_btf_fd);
6737 if (IS_ERR(btf))
6738 return PTR_ERR(btf);
6739 env->prog->aux->btf = btf;
6740
6741 err = check_btf_func(env, attr, uattr);
6742 if (err)
6743 return err;
6744
6745 err = check_btf_line(env, attr, uattr);
6746 if (err)
6747 return err;
6748
6749 return 0;
6750 }
6751
6752
6753 static bool range_within(struct bpf_reg_state *old,
6754 struct bpf_reg_state *cur)
6755 {
6756 return old->umin_value <= cur->umin_value &&
6757 old->umax_value >= cur->umax_value &&
6758 old->smin_value <= cur->smin_value &&
6759 old->smax_value >= cur->smax_value;
6760 }
6761
6762
6763 #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
6764 struct idpair {
6765 u32 old;
6766 u32 cur;
6767 };
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779 static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap)
6780 {
6781 unsigned int i;
6782
6783 for (i = 0; i < ID_MAP_SIZE; i++) {
6784 if (!idmap[i].old) {
6785
6786 idmap[i].old = old_id;
6787 idmap[i].cur = cur_id;
6788 return true;
6789 }
6790 if (idmap[i].old == old_id)
6791 return idmap[i].cur == cur_id;
6792 }
6793
6794 WARN_ON_ONCE(1);
6795 return false;
6796 }
6797
6798 static void clean_func_state(struct bpf_verifier_env *env,
6799 struct bpf_func_state *st)
6800 {
6801 enum bpf_reg_liveness live;
6802 int i, j;
6803
6804 for (i = 0; i < BPF_REG_FP; i++) {
6805 live = st->regs[i].live;
6806
6807 st->regs[i].live |= REG_LIVE_DONE;
6808 if (!(live & REG_LIVE_READ))
6809
6810
6811
6812 __mark_reg_not_init(env, &st->regs[i]);
6813 }
6814
6815 for (i = 0; i < st->allocated_stack / BPF_REG_SIZE; i++) {
6816 live = st->stack[i].spilled_ptr.live;
6817
6818 st->stack[i].spilled_ptr.live |= REG_LIVE_DONE;
6819 if (!(live & REG_LIVE_READ)) {
6820 __mark_reg_not_init(env, &st->stack[i].spilled_ptr);
6821 for (j = 0; j < BPF_REG_SIZE; j++)
6822 st->stack[i].slot_type[j] = STACK_INVALID;
6823 }
6824 }
6825 }
6826
6827 static void clean_verifier_state(struct bpf_verifier_env *env,
6828 struct bpf_verifier_state *st)
6829 {
6830 int i;
6831
6832 if (st->frame[0]->regs[0].live & REG_LIVE_DONE)
6833
6834 return;
6835
6836 for (i = 0; i <= st->curframe; i++)
6837 clean_func_state(env, st->frame[i]);
6838 }
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872 static void clean_live_states(struct bpf_verifier_env *env, int insn,
6873 struct bpf_verifier_state *cur)
6874 {
6875 struct bpf_verifier_state_list *sl;
6876 int i;
6877
6878 sl = *explored_state(env, insn);
6879 while (sl) {
6880 if (sl->state.branches)
6881 goto next;
6882 if (sl->state.insn_idx != insn ||
6883 sl->state.curframe != cur->curframe)
6884 goto next;
6885 for (i = 0; i <= cur->curframe; i++)
6886 if (sl->state.frame[i]->callsite != cur->frame[i]->callsite)
6887 goto next;
6888 clean_verifier_state(env, &sl->state);
6889 next:
6890 sl = sl->next;
6891 }
6892 }
6893
6894
6895 static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
6896 struct idpair *idmap)
6897 {
6898 bool equal;
6899
6900 if (!(rold->live & REG_LIVE_READ))
6901
6902 return true;
6903
6904 equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, parent)) == 0;
6905
6906 if (rold->type == PTR_TO_STACK)
6907
6908
6909
6910 return equal && rold->frameno == rcur->frameno;
6911
6912 if (equal)
6913 return true;
6914
6915 if (rold->type == NOT_INIT)
6916
6917 return true;
6918 if (rcur->type == NOT_INIT)
6919 return false;
6920 switch (rold->type) {
6921 case SCALAR_VALUE:
6922 if (rcur->type == SCALAR_VALUE) {
6923 if (!rold->precise && !rcur->precise)
6924 return true;
6925
6926 return range_within(rold, rcur) &&
6927 tnum_in(rold->var_off, rcur->var_off);
6928 } else {
6929
6930
6931
6932
6933
6934
6935
6936 return false;
6937 }
6938 case PTR_TO_MAP_VALUE:
6939
6940
6941
6942
6943
6944
6945
6946
6947 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
6948 range_within(rold, rcur) &&
6949 tnum_in(rold->var_off, rcur->var_off);
6950 case PTR_TO_MAP_VALUE_OR_NULL:
6951
6952
6953
6954
6955
6956
6957
6958 if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
6959 return false;
6960 if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
6961 return false;
6962
6963 return check_ids(rold->id, rcur->id, idmap);
6964 case PTR_TO_PACKET_META:
6965 case PTR_TO_PACKET:
6966 if (rcur->type != rold->type)
6967 return false;
6968
6969
6970
6971
6972
6973
6974 if (rold->range > rcur->range)
6975 return false;
6976
6977
6978
6979 if (rold->off != rcur->off)
6980 return false;
6981
6982 if (rold->id && !check_ids(rold->id, rcur->id, idmap))
6983 return false;
6984
6985 return range_within(rold, rcur) &&
6986 tnum_in(rold->var_off, rcur->var_off);
6987 case PTR_TO_CTX:
6988 case CONST_PTR_TO_MAP:
6989 case PTR_TO_PACKET_END:
6990 case PTR_TO_FLOW_KEYS:
6991 case PTR_TO_SOCKET:
6992 case PTR_TO_SOCKET_OR_NULL:
6993 case PTR_TO_SOCK_COMMON:
6994 case PTR_TO_SOCK_COMMON_OR_NULL:
6995 case PTR_TO_TCP_SOCK:
6996 case PTR_TO_TCP_SOCK_OR_NULL:
6997 case PTR_TO_XDP_SOCK:
6998
6999
7000
7001 default:
7002
7003 return false;
7004 }
7005
7006
7007 WARN_ON_ONCE(1);
7008 return false;
7009 }
7010
7011 static bool stacksafe(struct bpf_func_state *old,
7012 struct bpf_func_state *cur,
7013 struct idpair *idmap)
7014 {
7015 int i, spi;
7016
7017
7018
7019
7020
7021 for (i = 0; i < old->allocated_stack; i++) {
7022 spi = i / BPF_REG_SIZE;
7023
7024 if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) {
7025 i += BPF_REG_SIZE - 1;
7026
7027 continue;
7028 }
7029
7030 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
7031 continue;
7032
7033
7034
7035
7036 if (i >= cur->allocated_stack)
7037 return false;
7038
7039
7040
7041
7042
7043 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC &&
7044 cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO)
7045 continue;
7046 if (old->stack[spi].slot_type[i % BPF_REG_SIZE] !=
7047 cur->stack[spi].slot_type[i % BPF_REG_SIZE])
7048
7049
7050
7051
7052
7053 return false;
7054 if (i % BPF_REG_SIZE)
7055 continue;
7056 if (old->stack[spi].slot_type[0] != STACK_SPILL)
7057 continue;
7058 if (!regsafe(&old->stack[spi].spilled_ptr,
7059 &cur->stack[spi].spilled_ptr,
7060 idmap))
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071 return false;
7072 }
7073 return true;
7074 }
7075
7076 static bool refsafe(struct bpf_func_state *old, struct bpf_func_state *cur)
7077 {
7078 if (old->acquired_refs != cur->acquired_refs)
7079 return false;
7080 return !memcmp(old->refs, cur->refs,
7081 sizeof(*old->refs) * old->acquired_refs);
7082 }
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110 static bool func_states_equal(struct bpf_func_state *old,
7111 struct bpf_func_state *cur)
7112 {
7113 struct idpair *idmap;
7114 bool ret = false;
7115 int i;
7116
7117 idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL);
7118
7119 if (!idmap)
7120 return false;
7121
7122 for (i = 0; i < MAX_BPF_REG; i++) {
7123 if (!regsafe(&old->regs[i], &cur->regs[i], idmap))
7124 goto out_free;
7125 }
7126
7127 if (!stacksafe(old, cur, idmap))
7128 goto out_free;
7129
7130 if (!refsafe(old, cur))
7131 goto out_free;
7132 ret = true;
7133 out_free:
7134 kfree(idmap);
7135 return ret;
7136 }
7137
7138 static bool states_equal(struct bpf_verifier_env *env,
7139 struct bpf_verifier_state *old,
7140 struct bpf_verifier_state *cur)
7141 {
7142 int i;
7143
7144 if (old->curframe != cur->curframe)
7145 return false;
7146
7147
7148
7149
7150 if (old->speculative && !cur->speculative)
7151 return false;
7152
7153 if (old->active_spin_lock != cur->active_spin_lock)
7154 return false;
7155
7156
7157
7158
7159 for (i = 0; i <= old->curframe; i++) {
7160 if (old->frame[i]->callsite != cur->frame[i]->callsite)
7161 return false;
7162 if (!func_states_equal(old->frame[i], cur->frame[i]))
7163 return false;
7164 }
7165 return true;
7166 }
7167
7168
7169
7170
7171 static int propagate_liveness_reg(struct bpf_verifier_env *env,
7172 struct bpf_reg_state *reg,
7173 struct bpf_reg_state *parent_reg)
7174 {
7175 u8 parent_flag = parent_reg->live & REG_LIVE_READ;
7176 u8 flag = reg->live & REG_LIVE_READ;
7177 int err;
7178
7179
7180
7181
7182
7183 if (parent_flag == REG_LIVE_READ64 ||
7184
7185 !flag ||
7186
7187 parent_flag == flag)
7188 return 0;
7189
7190 err = mark_reg_read(env, reg, parent_reg, flag);
7191 if (err)
7192 return err;
7193
7194 return flag;
7195 }
7196
7197
7198
7199
7200
7201
7202
7203
7204 static int propagate_liveness(struct bpf_verifier_env *env,
7205 const struct bpf_verifier_state *vstate,
7206 struct bpf_verifier_state *vparent)
7207 {
7208 struct bpf_reg_state *state_reg, *parent_reg;
7209 struct bpf_func_state *state, *parent;
7210 int i, frame, err = 0;
7211
7212 if (vparent->curframe != vstate->curframe) {
7213 WARN(1, "propagate_live: parent frame %d current frame %d\n",
7214 vparent->curframe, vstate->curframe);
7215 return -EFAULT;
7216 }
7217
7218 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
7219 for (frame = 0; frame <= vstate->curframe; frame++) {
7220 parent = vparent->frame[frame];
7221 state = vstate->frame[frame];
7222 parent_reg = parent->regs;
7223 state_reg = state->regs;
7224
7225 for (i = frame < vstate->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++) {
7226 err = propagate_liveness_reg(env, &state_reg[i],
7227 &parent_reg[i]);
7228 if (err < 0)
7229 return err;
7230 if (err == REG_LIVE_READ64)
7231 mark_insn_zext(env, &parent_reg[i]);
7232 }
7233
7234
7235 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE &&
7236 i < parent->allocated_stack / BPF_REG_SIZE; i++) {
7237 parent_reg = &parent->stack[i].spilled_ptr;
7238 state_reg = &state->stack[i].spilled_ptr;
7239 err = propagate_liveness_reg(env, state_reg,
7240 parent_reg);
7241 if (err < 0)
7242 return err;
7243 }
7244 }
7245 return 0;
7246 }
7247
7248
7249
7250
7251 static int propagate_precision(struct bpf_verifier_env *env,
7252 const struct bpf_verifier_state *old)
7253 {
7254 struct bpf_reg_state *state_reg;
7255 struct bpf_func_state *state;
7256 int i, err = 0;
7257
7258 state = old->frame[old->curframe];
7259 state_reg = state->regs;
7260 for (i = 0; i < BPF_REG_FP; i++, state_reg++) {
7261 if (state_reg->type != SCALAR_VALUE ||
7262 !state_reg->precise)
7263 continue;
7264 if (env->log.level & BPF_LOG_LEVEL2)
7265 verbose(env, "propagating r%d\n", i);
7266 err = mark_chain_precision(env, i);
7267 if (err < 0)
7268 return err;
7269 }
7270
7271 for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
7272 if (state->stack[i].slot_type[0] != STACK_SPILL)
7273 continue;
7274 state_reg = &state->stack[i].spilled_ptr;
7275 if (state_reg->type != SCALAR_VALUE ||
7276 !state_reg->precise)
7277 continue;
7278 if (env->log.level & BPF_LOG_LEVEL2)
7279 verbose(env, "propagating fp%d\n",
7280 (-i - 1) * BPF_REG_SIZE);
7281 err = mark_chain_precision_stack(env, i);
7282 if (err < 0)
7283 return err;
7284 }
7285 return 0;
7286 }
7287
7288 static bool states_maybe_looping(struct bpf_verifier_state *old,
7289 struct bpf_verifier_state *cur)
7290 {
7291 struct bpf_func_state *fold, *fcur;
7292 int i, fr = cur->curframe;
7293
7294 if (old->curframe != fr)
7295 return false;
7296
7297 fold = old->frame[fr];
7298 fcur = cur->frame[fr];
7299 for (i = 0; i < MAX_BPF_REG; i++)
7300 if (memcmp(&fold->regs[i], &fcur->regs[i],
7301 offsetof(struct bpf_reg_state, parent)))
7302 return false;
7303 return true;
7304 }
7305
7306
7307 static int is_state_visited(struct bpf_verifier_env *env, int insn_idx)
7308 {
7309 struct bpf_verifier_state_list *new_sl;
7310 struct bpf_verifier_state_list *sl, **pprev;
7311 struct bpf_verifier_state *cur = env->cur_state, *new;
7312 int i, j, err, states_cnt = 0;
7313 bool add_new_state = env->test_state_freq ? true : false;
7314
7315 cur->last_insn_idx = env->prev_insn_idx;
7316 if (!env->insn_aux_data[insn_idx].prune_point)
7317
7318
7319
7320 return 0;
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330 if (env->jmps_processed - env->prev_jmps_processed >= 2 &&
7331 env->insn_processed - env->prev_insn_processed >= 8)
7332 add_new_state = true;
7333
7334 pprev = explored_state(env, insn_idx);
7335 sl = *pprev;
7336
7337 clean_live_states(env, insn_idx, cur);
7338
7339 while (sl) {
7340 states_cnt++;
7341 if (sl->state.insn_idx != insn_idx)
7342 goto next;
7343 if (sl->state.branches) {
7344 if (states_maybe_looping(&sl->state, cur) &&
7345 states_equal(env, &sl->state, cur)) {
7346 verbose_linfo(env, insn_idx, "; ");
7347 verbose(env, "infinite loop detected at insn %d\n", insn_idx);
7348 return -EINVAL;
7349 }
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362 if (env->jmps_processed - env->prev_jmps_processed < 20 &&
7363 env->insn_processed - env->prev_insn_processed < 100)
7364 add_new_state = false;
7365 goto miss;
7366 }
7367 if (states_equal(env, &sl->state, cur)) {
7368 sl->hit_cnt++;
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379 err = propagate_liveness(env, &sl->state, cur);
7380
7381
7382
7383
7384
7385
7386 err = err ? : push_jmp_history(env, cur);
7387 err = err ? : propagate_precision(env, &sl->state);
7388 if (err)
7389 return err;
7390 return 1;
7391 }
7392 miss:
7393
7394
7395
7396
7397
7398
7399 if (add_new_state)
7400 sl->miss_cnt++;
7401
7402
7403
7404
7405
7406 if (sl->miss_cnt > sl->hit_cnt * 3 + 3) {
7407
7408
7409
7410 *pprev = sl->next;
7411 if (sl->state.frame[0]->regs[0].live & REG_LIVE_DONE) {
7412 u32 br = sl->state.branches;
7413
7414 WARN_ONCE(br,
7415 "BUG live_done but branches_to_explore %d\n",
7416 br);
7417 free_verifier_state(&sl->state, false);
7418 kfree(sl);
7419 env->peak_states--;
7420 } else {
7421
7422
7423
7424
7425 sl->next = env->free_list;
7426 env->free_list = sl;
7427 }
7428 sl = *pprev;
7429 continue;
7430 }
7431 next:
7432 pprev = &sl->next;
7433 sl = *pprev;
7434 }
7435
7436 if (env->max_states_per_insn < states_cnt)
7437 env->max_states_per_insn = states_cnt;
7438
7439 if (!env->allow_ptr_leaks && states_cnt > BPF_COMPLEXITY_LIMIT_STATES)
7440 return push_jmp_history(env, cur);
7441
7442 if (!add_new_state)
7443 return push_jmp_history(env, cur);
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454 new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL);
7455 if (!new_sl)
7456 return -ENOMEM;
7457 env->total_states++;
7458 env->peak_states++;
7459 env->prev_jmps_processed = env->jmps_processed;
7460 env->prev_insn_processed = env->insn_processed;
7461
7462
7463 new = &new_sl->state;
7464 err = copy_verifier_state(new, cur);
7465 if (err) {
7466 free_verifier_state(new, false);
7467 kfree(new_sl);
7468 return err;
7469 }
7470 new->insn_idx = insn_idx;
7471 WARN_ONCE(new->branches != 1,
7472 "BUG is_state_visited:branches_to_explore=%d insn %d\n", new->branches, insn_idx);
7473
7474 cur->parent = new;
7475 cur->first_insn_idx = insn_idx;
7476 clear_jmp_history(cur);
7477 new_sl->next = *explored_state(env, insn_idx);
7478 *explored_state(env, insn_idx) = new_sl;
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492 for (j = 0; j <= cur->curframe; j++) {
7493 for (i = j < cur->curframe ? BPF_REG_6 : 0; i < BPF_REG_FP; i++)
7494 cur->frame[j]->regs[i].parent = &new->frame[j]->regs[i];
7495 for (i = 0; i < BPF_REG_FP; i++)
7496 cur->frame[j]->regs[i].live = REG_LIVE_NONE;
7497 }
7498
7499
7500 for (j = 0; j <= cur->curframe; j++) {
7501 struct bpf_func_state *frame = cur->frame[j];
7502 struct bpf_func_state *newframe = new->frame[j];
7503
7504 for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) {
7505 frame->stack[i].spilled_ptr.live = REG_LIVE_NONE;
7506 frame->stack[i].spilled_ptr.parent =
7507 &newframe->stack[i].spilled_ptr;
7508 }
7509 }
7510 return 0;
7511 }
7512
7513
7514 static bool reg_type_mismatch_ok(enum bpf_reg_type type)
7515 {
7516 switch (type) {
7517 case PTR_TO_CTX:
7518 case PTR_TO_SOCKET:
7519 case PTR_TO_SOCKET_OR_NULL:
7520 case PTR_TO_SOCK_COMMON:
7521 case PTR_TO_SOCK_COMMON_OR_NULL:
7522 case PTR_TO_TCP_SOCK:
7523 case PTR_TO_TCP_SOCK_OR_NULL:
7524 case PTR_TO_XDP_SOCK:
7525 return false;
7526 default:
7527 return true;
7528 }
7529 }
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543 static bool reg_type_mismatch(enum bpf_reg_type src, enum bpf_reg_type prev)
7544 {
7545 return src != prev && (!reg_type_mismatch_ok(src) ||
7546 !reg_type_mismatch_ok(prev));
7547 }
7548
7549 static int do_check(struct bpf_verifier_env *env)
7550 {
7551 struct bpf_verifier_state *state;
7552 struct bpf_insn *insns = env->prog->insnsi;
7553 struct bpf_reg_state *regs;
7554 int insn_cnt = env->prog->len;
7555 bool do_print_state = false;
7556 int prev_insn_idx = -1;
7557
7558 env->prev_linfo = NULL;
7559
7560 state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
7561 if (!state)
7562 return -ENOMEM;
7563 state->curframe = 0;
7564 state->speculative = false;
7565 state->branches = 1;
7566 state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL);
7567 if (!state->frame[0]) {
7568 kfree(state);
7569 return -ENOMEM;
7570 }
7571 env->cur_state = state;
7572 init_func_state(env, state->frame[0],
7573 BPF_MAIN_FUNC ,
7574 0 ,
7575 0 );
7576
7577 for (;;) {
7578 struct bpf_insn *insn;
7579 u8 class;
7580 int err;
7581
7582 env->prev_insn_idx = prev_insn_idx;
7583 if (env->insn_idx >= insn_cnt) {
7584 verbose(env, "invalid insn idx %d insn_cnt %d\n",
7585 env->insn_idx, insn_cnt);
7586 return -EFAULT;
7587 }
7588
7589 insn = &insns[env->insn_idx];
7590 class = BPF_CLASS(insn->code);
7591
7592 if (++env->insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) {
7593 verbose(env,
7594 "BPF program is too large. Processed %d insn\n",
7595 env->insn_processed);
7596 return -E2BIG;
7597 }
7598
7599 err = is_state_visited(env, env->insn_idx);
7600 if (err < 0)
7601 return err;
7602 if (err == 1) {
7603
7604 if (env->log.level & BPF_LOG_LEVEL) {
7605 if (do_print_state)
7606 verbose(env, "\nfrom %d to %d%s: safe\n",
7607 env->prev_insn_idx, env->insn_idx,
7608 env->cur_state->speculative ?
7609 " (speculative execution)" : "");
7610 else
7611 verbose(env, "%d: safe\n", env->insn_idx);
7612 }
7613 goto process_bpf_exit;
7614 }
7615
7616 if (signal_pending(current))
7617 return -EAGAIN;
7618
7619 if (need_resched())
7620 cond_resched();
7621
7622 if (env->log.level & BPF_LOG_LEVEL2 ||
7623 (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
7624 if (env->log.level & BPF_LOG_LEVEL2)
7625 verbose(env, "%d:", env->insn_idx);
7626 else
7627 verbose(env, "\nfrom %d to %d%s:",
7628 env->prev_insn_idx, env->insn_idx,
7629 env->cur_state->speculative ?
7630 " (speculative execution)" : "");
7631 print_verifier_state(env, state->frame[state->curframe]);
7632 do_print_state = false;
7633 }
7634
7635 if (env->log.level & BPF_LOG_LEVEL) {
7636 const struct bpf_insn_cbs cbs = {
7637 .cb_print = verbose,
7638 .private_data = env,
7639 };
7640
7641 verbose_linfo(env, env->insn_idx, "; ");
7642 verbose(env, "%d: ", env->insn_idx);
7643 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
7644 }
7645
7646 if (bpf_prog_is_dev_bound(env->prog->aux)) {
7647 err = bpf_prog_offload_verify_insn(env, env->insn_idx,
7648 env->prev_insn_idx);
7649 if (err)
7650 return err;
7651 }
7652
7653 regs = cur_regs(env);
7654 env->insn_aux_data[env->insn_idx].seen = true;
7655 prev_insn_idx = env->insn_idx;
7656
7657 if (class == BPF_ALU || class == BPF_ALU64) {
7658 err = check_alu_op(env, insn);
7659 if (err)
7660 return err;
7661
7662 } else if (class == BPF_LDX) {
7663 enum bpf_reg_type *prev_src_type, src_reg_type;
7664
7665
7666
7667
7668 err = check_reg_arg(env, insn->src_reg, SRC_OP);
7669 if (err)
7670 return err;
7671
7672 err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK);
7673 if (err)
7674 return err;
7675
7676 src_reg_type = regs[insn->src_reg].type;
7677
7678
7679
7680
7681 err = check_mem_access(env, env->insn_idx, insn->src_reg,
7682 insn->off, BPF_SIZE(insn->code),
7683 BPF_READ, insn->dst_reg, false);
7684 if (err)
7685 return err;
7686
7687 prev_src_type = &env->insn_aux_data[env->insn_idx].ptr_type;
7688
7689 if (*prev_src_type == NOT_INIT) {
7690
7691
7692
7693
7694 *prev_src_type = src_reg_type;
7695
7696 } else if (reg_type_mismatch(src_reg_type, *prev_src_type)) {
7697
7698
7699
7700
7701
7702
7703
7704 verbose(env, "same insn cannot be used with different pointers\n");
7705 return -EINVAL;
7706 }
7707
7708 } else if (class == BPF_STX) {
7709 enum bpf_reg_type *prev_dst_type, dst_reg_type;
7710
7711 if (BPF_MODE(insn->code) == BPF_XADD) {
7712 err = check_xadd(env, env->insn_idx, insn);
7713 if (err)
7714 return err;
7715 env->insn_idx++;
7716 continue;
7717 }
7718
7719
7720 err = check_reg_arg(env, insn->src_reg, SRC_OP);
7721 if (err)
7722 return err;
7723
7724 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
7725 if (err)
7726 return err;
7727
7728 dst_reg_type = regs[insn->dst_reg].type;
7729
7730
7731 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7732 insn->off, BPF_SIZE(insn->code),
7733 BPF_WRITE, insn->src_reg, false);
7734 if (err)
7735 return err;
7736
7737 prev_dst_type = &env->insn_aux_data[env->insn_idx].ptr_type;
7738
7739 if (*prev_dst_type == NOT_INIT) {
7740 *prev_dst_type = dst_reg_type;
7741 } else if (reg_type_mismatch(dst_reg_type, *prev_dst_type)) {
7742 verbose(env, "same insn cannot be used with different pointers\n");
7743 return -EINVAL;
7744 }
7745
7746 } else if (class == BPF_ST) {
7747 if (BPF_MODE(insn->code) != BPF_MEM ||
7748 insn->src_reg != BPF_REG_0) {
7749 verbose(env, "BPF_ST uses reserved fields\n");
7750 return -EINVAL;
7751 }
7752
7753 err = check_reg_arg(env, insn->dst_reg, SRC_OP);
7754 if (err)
7755 return err;
7756
7757 if (is_ctx_reg(env, insn->dst_reg)) {
7758 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
7759 insn->dst_reg,
7760 reg_type_str[reg_state(env, insn->dst_reg)->type]);
7761 return -EACCES;
7762 }
7763
7764
7765 err = check_mem_access(env, env->insn_idx, insn->dst_reg,
7766 insn->off, BPF_SIZE(insn->code),
7767 BPF_WRITE, -1, false);
7768 if (err)
7769 return err;
7770
7771 } else if (class == BPF_JMP || class == BPF_JMP32) {
7772 u8 opcode = BPF_OP(insn->code);
7773
7774 env->jmps_processed++;
7775 if (opcode == BPF_CALL) {
7776 if (BPF_SRC(insn->code) != BPF_K ||
7777 insn->off != 0 ||
7778 (insn->src_reg != BPF_REG_0 &&
7779 insn->src_reg != BPF_PSEUDO_CALL) ||
7780 insn->dst_reg != BPF_REG_0 ||
7781 class == BPF_JMP32) {
7782 verbose(env, "BPF_CALL uses reserved fields\n");
7783 return -EINVAL;
7784 }
7785
7786 if (env->cur_state->active_spin_lock &&
7787 (insn->src_reg == BPF_PSEUDO_CALL ||
7788 insn->imm != BPF_FUNC_spin_unlock)) {
7789 verbose(env, "function calls are not allowed while holding a lock\n");
7790 return -EINVAL;
7791 }
7792 if (insn->src_reg == BPF_PSEUDO_CALL)
7793 err = check_func_call(env, insn, &env->insn_idx);
7794 else
7795 err = check_helper_call(env, insn->imm, env->insn_idx);
7796 if (err)
7797 return err;
7798
7799 } else if (opcode == BPF_JA) {
7800 if (BPF_SRC(insn->code) != BPF_K ||
7801 insn->imm != 0 ||
7802 insn->src_reg != BPF_REG_0 ||
7803 insn->dst_reg != BPF_REG_0 ||
7804 class == BPF_JMP32) {
7805 verbose(env, "BPF_JA uses reserved fields\n");
7806 return -EINVAL;
7807 }
7808
7809 env->insn_idx += insn->off + 1;
7810 continue;
7811
7812 } else if (opcode == BPF_EXIT) {
7813 if (BPF_SRC(insn->code) != BPF_K ||
7814 insn->imm != 0 ||
7815 insn->src_reg != BPF_REG_0 ||
7816 insn->dst_reg != BPF_REG_0 ||
7817 class == BPF_JMP32) {
7818 verbose(env, "BPF_EXIT uses reserved fields\n");
7819 return -EINVAL;
7820 }
7821
7822 if (env->cur_state->active_spin_lock) {
7823 verbose(env, "bpf_spin_unlock is missing\n");
7824 return -EINVAL;
7825 }
7826
7827 if (state->curframe) {
7828
7829 err = prepare_func_exit(env, &env->insn_idx);
7830 if (err)
7831 return err;
7832 do_print_state = true;
7833 continue;
7834 }
7835
7836 err = check_reference_leak(env);
7837 if (err)
7838 return err;
7839
7840
7841
7842
7843
7844
7845
7846 err = check_reg_arg(env, BPF_REG_0, SRC_OP);
7847 if (err)
7848 return err;
7849
7850 if (is_pointer_value(env, BPF_REG_0)) {
7851 verbose(env, "R0 leaks addr as return value\n");
7852 return -EACCES;
7853 }
7854
7855 err = check_return_code(env);
7856 if (err)
7857 return err;
7858 process_bpf_exit:
7859 update_branch_counts(env, env->cur_state);
7860 err = pop_stack(env, &prev_insn_idx,
7861 &env->insn_idx);
7862 if (err < 0) {
7863 if (err != -ENOENT)
7864 return err;
7865 break;
7866 } else {
7867 do_print_state = true;
7868 continue;
7869 }
7870 } else {
7871 err = check_cond_jmp_op(env, insn, &env->insn_idx);
7872 if (err)
7873 return err;
7874 }
7875 } else if (class == BPF_LD) {
7876 u8 mode = BPF_MODE(insn->code);
7877
7878 if (mode == BPF_ABS || mode == BPF_IND) {
7879 err = check_ld_abs(env, insn);
7880 if (err)
7881 return err;
7882
7883 } else if (mode == BPF_IMM) {
7884 err = check_ld_imm(env, insn);
7885 if (err)
7886 return err;
7887
7888 env->insn_idx++;
7889 env->insn_aux_data[env->insn_idx].seen = true;
7890 } else {
7891 verbose(env, "invalid BPF_LD mode\n");
7892 return -EINVAL;
7893 }
7894 } else {
7895 verbose(env, "unknown insn class %d\n", class);
7896 return -EINVAL;
7897 }
7898
7899 env->insn_idx++;
7900 }
7901
7902 env->prog->aux->stack_depth = env->subprog_info[0].stack_depth;
7903 return 0;
7904 }
7905
7906 static int check_map_prealloc(struct bpf_map *map)
7907 {
7908 return (map->map_type != BPF_MAP_TYPE_HASH &&
7909 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
7910 map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) ||
7911 !(map->map_flags & BPF_F_NO_PREALLOC);
7912 }
7913
7914 static bool is_tracing_prog_type(enum bpf_prog_type type)
7915 {
7916 switch (type) {
7917 case BPF_PROG_TYPE_KPROBE:
7918 case BPF_PROG_TYPE_TRACEPOINT:
7919 case BPF_PROG_TYPE_PERF_EVENT:
7920 case BPF_PROG_TYPE_RAW_TRACEPOINT:
7921 return true;
7922 default:
7923 return false;
7924 }
7925 }
7926
7927 static int check_map_prog_compatibility(struct bpf_verifier_env *env,
7928 struct bpf_map *map,
7929 struct bpf_prog *prog)
7930
7931 {
7932
7933
7934
7935
7936
7937 if (prog->type == BPF_PROG_TYPE_PERF_EVENT) {
7938 if (!check_map_prealloc(map)) {
7939 verbose(env, "perf_event programs can only use preallocated hash map\n");
7940 return -EINVAL;
7941 }
7942 if (map->inner_map_meta &&
7943 !check_map_prealloc(map->inner_map_meta)) {
7944 verbose(env, "perf_event programs can only use preallocated inner hash map\n");
7945 return -EINVAL;
7946 }
7947 }
7948
7949 if ((is_tracing_prog_type(prog->type) ||
7950 prog->type == BPF_PROG_TYPE_SOCKET_FILTER) &&
7951 map_value_has_spin_lock(map)) {
7952 verbose(env, "tracing progs cannot use bpf_spin_lock yet\n");
7953 return -EINVAL;
7954 }
7955
7956 if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
7957 !bpf_offload_prog_map_match(prog, map)) {
7958 verbose(env, "offload device mismatch between prog and map\n");
7959 return -EINVAL;
7960 }
7961
7962 return 0;
7963 }
7964
7965 static bool bpf_map_is_cgroup_storage(struct bpf_map *map)
7966 {
7967 return (map->map_type == BPF_MAP_TYPE_CGROUP_STORAGE ||
7968 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE);
7969 }
7970
7971
7972
7973
7974 static int replace_map_fd_with_map_ptr(struct bpf_verifier_env *env)
7975 {
7976 struct bpf_insn *insn = env->prog->insnsi;
7977 int insn_cnt = env->prog->len;
7978 int i, j, err;
7979
7980 err = bpf_prog_calc_tag(env->prog);
7981 if (err)
7982 return err;
7983
7984 for (i = 0; i < insn_cnt; i++, insn++) {
7985 if (BPF_CLASS(insn->code) == BPF_LDX &&
7986 (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) {
7987 verbose(env, "BPF_LDX uses reserved fields\n");
7988 return -EINVAL;
7989 }
7990
7991 if (BPF_CLASS(insn->code) == BPF_STX &&
7992 ((BPF_MODE(insn->code) != BPF_MEM &&
7993 BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) {
7994 verbose(env, "BPF_STX uses reserved fields\n");
7995 return -EINVAL;
7996 }
7997
7998 if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) {
7999 struct bpf_insn_aux_data *aux;
8000 struct bpf_map *map;
8001 struct fd f;
8002 u64 addr;
8003
8004 if (i == insn_cnt - 1 || insn[1].code != 0 ||
8005 insn[1].dst_reg != 0 || insn[1].src_reg != 0 ||
8006 insn[1].off != 0) {
8007 verbose(env, "invalid bpf_ld_imm64 insn\n");
8008 return -EINVAL;
8009 }
8010
8011 if (insn[0].src_reg == 0)
8012
8013 goto next_insn;
8014
8015
8016
8017
8018 if ((insn[0].src_reg != BPF_PSEUDO_MAP_FD &&
8019 insn[0].src_reg != BPF_PSEUDO_MAP_VALUE) ||
8020 (insn[0].src_reg == BPF_PSEUDO_MAP_FD &&
8021 insn[1].imm != 0)) {
8022 verbose(env,
8023 "unrecognized bpf_ld_imm64 insn\n");
8024 return -EINVAL;
8025 }
8026
8027 f = fdget(insn[0].imm);
8028 map = __bpf_map_get(f);
8029 if (IS_ERR(map)) {
8030 verbose(env, "fd %d is not pointing to valid bpf_map\n",
8031 insn[0].imm);
8032 return PTR_ERR(map);
8033 }
8034
8035 err = check_map_prog_compatibility(env, map, env->prog);
8036 if (err) {
8037 fdput(f);
8038 return err;
8039 }
8040
8041 aux = &env->insn_aux_data[i];
8042 if (insn->src_reg == BPF_PSEUDO_MAP_FD) {
8043 addr = (unsigned long)map;
8044 } else {
8045 u32 off = insn[1].imm;
8046
8047 if (off >= BPF_MAX_VAR_OFF) {
8048 verbose(env, "direct value offset of %u is not allowed\n", off);
8049 fdput(f);
8050 return -EINVAL;
8051 }
8052
8053 if (!map->ops->map_direct_value_addr) {
8054 verbose(env, "no direct value access support for this map type\n");
8055 fdput(f);
8056 return -EINVAL;
8057 }
8058
8059 err = map->ops->map_direct_value_addr(map, &addr, off);
8060 if (err) {
8061 verbose(env, "invalid access to map value pointer, value_size=%u off=%u\n",
8062 map->value_size, off);
8063 fdput(f);
8064 return err;
8065 }
8066
8067 aux->map_off = off;
8068 addr += off;
8069 }
8070
8071 insn[0].imm = (u32)addr;
8072 insn[1].imm = addr >> 32;
8073
8074
8075 for (j = 0; j < env->used_map_cnt; j++) {
8076 if (env->used_maps[j] == map) {
8077 aux->map_index = j;
8078 fdput(f);
8079 goto next_insn;
8080 }
8081 }
8082
8083 if (env->used_map_cnt >= MAX_USED_MAPS) {
8084 fdput(f);
8085 return -E2BIG;
8086 }
8087
8088
8089
8090
8091
8092
8093 map = bpf_map_inc(map, false);
8094 if (IS_ERR(map)) {
8095 fdput(f);
8096 return PTR_ERR(map);
8097 }
8098
8099 aux->map_index = env->used_map_cnt;
8100 env->used_maps[env->used_map_cnt++] = map;
8101
8102 if (bpf_map_is_cgroup_storage(map) &&
8103 bpf_cgroup_storage_assign(env->prog, map)) {
8104 verbose(env, "only one cgroup storage of each type is allowed\n");
8105 fdput(f);
8106 return -EBUSY;
8107 }
8108
8109 fdput(f);
8110 next_insn:
8111 insn++;
8112 i++;
8113 continue;
8114 }
8115
8116
8117 if (!bpf_opcode_in_insntable(insn->code)) {
8118 verbose(env, "unknown opcode %02x\n", insn->code);
8119 return -EINVAL;
8120 }
8121 }
8122
8123
8124
8125
8126
8127 return 0;
8128 }
8129
8130
8131 static void release_maps(struct bpf_verifier_env *env)
8132 {
8133 enum bpf_cgroup_storage_type stype;
8134 int i;
8135
8136 for_each_cgroup_storage_type(stype) {
8137 if (!env->prog->aux->cgroup_storage[stype])
8138 continue;
8139 bpf_cgroup_storage_release(env->prog,
8140 env->prog->aux->cgroup_storage[stype]);
8141 }
8142
8143 for (i = 0; i < env->used_map_cnt; i++)
8144 bpf_map_put(env->used_maps[i]);
8145 }
8146
8147
8148 static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env)
8149 {
8150 struct bpf_insn *insn = env->prog->insnsi;
8151 int insn_cnt = env->prog->len;
8152 int i;
8153
8154 for (i = 0; i < insn_cnt; i++, insn++)
8155 if (insn->code == (BPF_LD | BPF_IMM | BPF_DW))
8156 insn->src_reg = 0;
8157 }
8158
8159
8160
8161
8162
8163 static int adjust_insn_aux_data(struct bpf_verifier_env *env,
8164 struct bpf_prog *new_prog, u32 off, u32 cnt)
8165 {
8166 struct bpf_insn_aux_data *new_data, *old_data = env->insn_aux_data;
8167 struct bpf_insn *insn = new_prog->insnsi;
8168 u32 prog_len;
8169 int i;
8170
8171
8172
8173
8174
8175 old_data[off].zext_dst = insn_has_def32(env, insn + off + cnt - 1);
8176
8177 if (cnt == 1)
8178 return 0;
8179 prog_len = new_prog->len;
8180 new_data = vzalloc(array_size(prog_len,
8181 sizeof(struct bpf_insn_aux_data)));
8182 if (!new_data)
8183 return -ENOMEM;
8184 memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off);
8185 memcpy(new_data + off + cnt - 1, old_data + off,
8186 sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
8187 for (i = off; i < off + cnt - 1; i++) {
8188 new_data[i].seen = true;
8189 new_data[i].zext_dst = insn_has_def32(env, insn + i);
8190 }
8191 env->insn_aux_data = new_data;
8192 vfree(old_data);
8193 return 0;
8194 }
8195
8196 static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len)
8197 {
8198 int i;
8199
8200 if (len == 1)
8201 return;
8202
8203 for (i = 0; i <= env->subprog_cnt; i++) {
8204 if (env->subprog_info[i].start <= off)
8205 continue;
8206 env->subprog_info[i].start += len - 1;
8207 }
8208 }
8209
8210 static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
8211 const struct bpf_insn *patch, u32 len)
8212 {
8213 struct bpf_prog *new_prog;
8214
8215 new_prog = bpf_patch_insn_single(env->prog, off, patch, len);
8216 if (IS_ERR(new_prog)) {
8217 if (PTR_ERR(new_prog) == -ERANGE)
8218 verbose(env,
8219 "insn %d cannot be patched due to 16-bit range\n",
8220 env->insn_aux_data[off].orig_idx);
8221 return NULL;
8222 }
8223 if (adjust_insn_aux_data(env, new_prog, off, len))
8224 return NULL;
8225 adjust_subprog_starts(env, off, len);
8226 return new_prog;
8227 }
8228
8229 static int adjust_subprog_starts_after_remove(struct bpf_verifier_env *env,
8230 u32 off, u32 cnt)
8231 {
8232 int i, j;
8233
8234
8235 for (i = 0; i < env->subprog_cnt; i++)
8236 if (env->subprog_info[i].start >= off)
8237 break;
8238
8239 for (j = i; j < env->subprog_cnt; j++)
8240 if (env->subprog_info[j].start >= off + cnt)
8241 break;
8242
8243
8244
8245 if (env->subprog_info[j].start != off + cnt)
8246 j--;
8247
8248 if (j > i) {
8249 struct bpf_prog_aux *aux = env->prog->aux;
8250 int move;
8251
8252
8253 move = env->subprog_cnt + 1 - j;
8254
8255 memmove(env->subprog_info + i,
8256 env->subprog_info + j,
8257 sizeof(*env->subprog_info) * move);
8258 env->subprog_cnt -= j - i;
8259
8260
8261 if (aux->func_info) {
8262 move = aux->func_info_cnt - j;
8263
8264 memmove(aux->func_info + i,
8265 aux->func_info + j,
8266 sizeof(*aux->func_info) * move);
8267 aux->func_info_cnt -= j - i;
8268
8269
8270
8271 }
8272 } else {
8273
8274 if (env->subprog_info[i].start == off)
8275 i++;
8276 }
8277
8278
8279 for (; i <= env->subprog_cnt; i++)
8280 env->subprog_info[i].start -= cnt;
8281
8282 return 0;
8283 }
8284
8285 static int bpf_adj_linfo_after_remove(struct bpf_verifier_env *env, u32 off,
8286 u32 cnt)
8287 {
8288 struct bpf_prog *prog = env->prog;
8289 u32 i, l_off, l_cnt, nr_linfo;
8290 struct bpf_line_info *linfo;
8291
8292 nr_linfo = prog->aux->nr_linfo;
8293 if (!nr_linfo)
8294 return 0;
8295
8296 linfo = prog->aux->linfo;
8297
8298
8299 for (i = 0; i < nr_linfo; i++)
8300 if (linfo[i].insn_off >= off)
8301 break;
8302
8303 l_off = i;
8304 l_cnt = 0;
8305 for (; i < nr_linfo; i++)
8306 if (linfo[i].insn_off < off + cnt)
8307 l_cnt++;
8308 else
8309 break;
8310
8311
8312
8313
8314
8315 if (prog->len != off && l_cnt &&
8316 (i == nr_linfo || linfo[i].insn_off != off + cnt)) {
8317 l_cnt--;
8318 linfo[--i].insn_off = off + cnt;
8319 }
8320
8321
8322 if (l_cnt) {
8323 memmove(linfo + l_off, linfo + i,
8324 sizeof(*linfo) * (nr_linfo - i));
8325
8326 prog->aux->nr_linfo -= l_cnt;
8327 nr_linfo = prog->aux->nr_linfo;
8328 }
8329
8330
8331 for (i = l_off; i < nr_linfo; i++)
8332 linfo[i].insn_off -= cnt;
8333
8334
8335 for (i = 0; i <= env->subprog_cnt; i++)
8336 if (env->subprog_info[i].linfo_idx > l_off) {
8337
8338
8339
8340 if (env->subprog_info[i].linfo_idx >= l_off + l_cnt)
8341 env->subprog_info[i].linfo_idx -= l_cnt;
8342 else
8343 env->subprog_info[i].linfo_idx = l_off;
8344 }
8345
8346 return 0;
8347 }
8348
8349 static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
8350 {
8351 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8352 unsigned int orig_prog_len = env->prog->len;
8353 int err;
8354
8355 if (bpf_prog_is_dev_bound(env->prog->aux))
8356 bpf_prog_offload_remove_insns(env, off, cnt);
8357
8358 err = bpf_remove_insns(env->prog, off, cnt);
8359 if (err)
8360 return err;
8361
8362 err = adjust_subprog_starts_after_remove(env, off, cnt);
8363 if (err)
8364 return err;
8365
8366 err = bpf_adj_linfo_after_remove(env, off, cnt);
8367 if (err)
8368 return err;
8369
8370 memmove(aux_data + off, aux_data + off + cnt,
8371 sizeof(*aux_data) * (orig_prog_len - off - cnt));
8372
8373 return 0;
8374 }
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387 static void sanitize_dead_code(struct bpf_verifier_env *env)
8388 {
8389 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8390 struct bpf_insn trap = BPF_JMP_IMM(BPF_JA, 0, 0, -1);
8391 struct bpf_insn *insn = env->prog->insnsi;
8392 const int insn_cnt = env->prog->len;
8393 int i;
8394
8395 for (i = 0; i < insn_cnt; i++) {
8396 if (aux_data[i].seen)
8397 continue;
8398 memcpy(insn + i, &trap, sizeof(trap));
8399 }
8400 }
8401
8402 static bool insn_is_cond_jump(u8 code)
8403 {
8404 u8 op;
8405
8406 if (BPF_CLASS(code) == BPF_JMP32)
8407 return true;
8408
8409 if (BPF_CLASS(code) != BPF_JMP)
8410 return false;
8411
8412 op = BPF_OP(code);
8413 return op != BPF_JA && op != BPF_EXIT && op != BPF_CALL;
8414 }
8415
8416 static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
8417 {
8418 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8419 struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
8420 struct bpf_insn *insn = env->prog->insnsi;
8421 const int insn_cnt = env->prog->len;
8422 int i;
8423
8424 for (i = 0; i < insn_cnt; i++, insn++) {
8425 if (!insn_is_cond_jump(insn->code))
8426 continue;
8427
8428 if (!aux_data[i + 1].seen)
8429 ja.off = insn->off;
8430 else if (!aux_data[i + 1 + insn->off].seen)
8431 ja.off = 0;
8432 else
8433 continue;
8434
8435 if (bpf_prog_is_dev_bound(env->prog->aux))
8436 bpf_prog_offload_replace_insn(env, i, &ja);
8437
8438 memcpy(insn, &ja, sizeof(ja));
8439 }
8440 }
8441
8442 static int opt_remove_dead_code(struct bpf_verifier_env *env)
8443 {
8444 struct bpf_insn_aux_data *aux_data = env->insn_aux_data;
8445 int insn_cnt = env->prog->len;
8446 int i, err;
8447
8448 for (i = 0; i < insn_cnt; i++) {
8449 int j;
8450
8451 j = 0;
8452 while (i + j < insn_cnt && !aux_data[i + j].seen)
8453 j++;
8454 if (!j)
8455 continue;
8456
8457 err = verifier_remove_insns(env, i, j);
8458 if (err)
8459 return err;
8460 insn_cnt = env->prog->len;
8461 }
8462
8463 return 0;
8464 }
8465
8466 static int opt_remove_nops(struct bpf_verifier_env *env)
8467 {
8468 const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
8469 struct bpf_insn *insn = env->prog->insnsi;
8470 int insn_cnt = env->prog->len;
8471 int i, err;
8472
8473 for (i = 0; i < insn_cnt; i++) {
8474 if (memcmp(&insn[i], &ja, sizeof(ja)))
8475 continue;
8476
8477 err = verifier_remove_insns(env, i, 1);
8478 if (err)
8479 return err;
8480 insn_cnt--;
8481 i--;
8482 }
8483
8484 return 0;
8485 }
8486
8487 static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
8488 const union bpf_attr *attr)
8489 {
8490 struct bpf_insn *patch, zext_patch[2], rnd_hi32_patch[4];
8491 struct bpf_insn_aux_data *aux = env->insn_aux_data;
8492 int i, patch_len, delta = 0, len = env->prog->len;
8493 struct bpf_insn *insns = env->prog->insnsi;
8494 struct bpf_prog *new_prog;
8495 bool rnd_hi32;
8496
8497 rnd_hi32 = attr->prog_flags & BPF_F_TEST_RND_HI32;
8498 zext_patch[1] = BPF_ZEXT_REG(0);
8499 rnd_hi32_patch[1] = BPF_ALU64_IMM(BPF_MOV, BPF_REG_AX, 0);
8500 rnd_hi32_patch[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_AX, 32);
8501 rnd_hi32_patch[3] = BPF_ALU64_REG(BPF_OR, 0, BPF_REG_AX);
8502 for (i = 0; i < len; i++) {
8503 int adj_idx = i + delta;
8504 struct bpf_insn insn;
8505
8506 insn = insns[adj_idx];
8507 if (!aux[adj_idx].zext_dst) {
8508 u8 code, class;
8509 u32 imm_rnd;
8510
8511 if (!rnd_hi32)
8512 continue;
8513
8514 code = insn.code;
8515 class = BPF_CLASS(code);
8516 if (insn_no_def(&insn))
8517 continue;
8518
8519
8520
8521
8522
8523 if (is_reg64(env, &insn, insn.dst_reg, NULL, DST_OP)) {
8524 if (class == BPF_LD &&
8525 BPF_MODE(code) == BPF_IMM)
8526 i++;
8527 continue;
8528 }
8529
8530
8531 if (class == BPF_LDX &&
8532 aux[adj_idx].ptr_type == PTR_TO_CTX)
8533 continue;
8534
8535 imm_rnd = get_random_int();
8536 rnd_hi32_patch[0] = insn;
8537 rnd_hi32_patch[1].imm = imm_rnd;
8538 rnd_hi32_patch[3].dst_reg = insn.dst_reg;
8539 patch = rnd_hi32_patch;
8540 patch_len = 4;
8541 goto apply_patch_buffer;
8542 }
8543
8544 if (!bpf_jit_needs_zext())
8545 continue;
8546
8547 zext_patch[0] = insn;
8548 zext_patch[1].dst_reg = insn.dst_reg;
8549 zext_patch[1].src_reg = insn.dst_reg;
8550 patch = zext_patch;
8551 patch_len = 2;
8552 apply_patch_buffer:
8553 new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
8554 if (!new_prog)
8555 return -ENOMEM;
8556 env->prog = new_prog;
8557 insns = new_prog->insnsi;
8558 aux = env->insn_aux_data;
8559 delta += patch_len - 1;
8560 }
8561
8562 return 0;
8563 }
8564
8565
8566
8567
8568
8569
8570 static int convert_ctx_accesses(struct bpf_verifier_env *env)
8571 {
8572 const struct bpf_verifier_ops *ops = env->ops;
8573 int i, cnt, size, ctx_field_size, delta = 0;
8574 const int insn_cnt = env->prog->len;
8575 struct bpf_insn insn_buf[16], *insn;
8576 u32 target_size, size_default, off;
8577 struct bpf_prog *new_prog;
8578 enum bpf_access_type type;
8579 bool is_narrower_load;
8580
8581 if (ops->gen_prologue || env->seen_direct_write) {
8582 if (!ops->gen_prologue) {
8583 verbose(env, "bpf verifier is misconfigured\n");
8584 return -EINVAL;
8585 }
8586 cnt = ops->gen_prologue(insn_buf, env->seen_direct_write,
8587 env->prog);
8588 if (cnt >= ARRAY_SIZE(insn_buf)) {
8589 verbose(env, "bpf verifier is misconfigured\n");
8590 return -EINVAL;
8591 } else if (cnt) {
8592 new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt);
8593 if (!new_prog)
8594 return -ENOMEM;
8595
8596 env->prog = new_prog;
8597 delta += cnt - 1;
8598 }
8599 }
8600
8601 if (bpf_prog_is_dev_bound(env->prog->aux))
8602 return 0;
8603
8604 insn = env->prog->insnsi + delta;
8605
8606 for (i = 0; i < insn_cnt; i++, insn++) {
8607 bpf_convert_ctx_access_t convert_ctx_access;
8608
8609 if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) ||
8610 insn->code == (BPF_LDX | BPF_MEM | BPF_H) ||
8611 insn->code == (BPF_LDX | BPF_MEM | BPF_W) ||
8612 insn->code == (BPF_LDX | BPF_MEM | BPF_DW))
8613 type = BPF_READ;
8614 else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) ||
8615 insn->code == (BPF_STX | BPF_MEM | BPF_H) ||
8616 insn->code == (BPF_STX | BPF_MEM | BPF_W) ||
8617 insn->code == (BPF_STX | BPF_MEM | BPF_DW))
8618 type = BPF_WRITE;
8619 else
8620 continue;
8621
8622 if (type == BPF_WRITE &&
8623 env->insn_aux_data[i + delta].sanitize_stack_off) {
8624 struct bpf_insn patch[] = {
8625
8626
8627
8628
8629
8630 BPF_ST_MEM(BPF_DW, BPF_REG_FP,
8631 env->insn_aux_data[i + delta].sanitize_stack_off,
8632 0),
8633
8634
8635
8636 *insn,
8637 };
8638
8639 cnt = ARRAY_SIZE(patch);
8640 new_prog = bpf_patch_insn_data(env, i + delta, patch, cnt);
8641 if (!new_prog)
8642 return -ENOMEM;
8643
8644 delta += cnt - 1;
8645 env->prog = new_prog;
8646 insn = new_prog->insnsi + i + delta;
8647 continue;
8648 }
8649
8650 switch (env->insn_aux_data[i + delta].ptr_type) {
8651 case PTR_TO_CTX:
8652 if (!ops->convert_ctx_access)
8653 continue;
8654 convert_ctx_access = ops->convert_ctx_access;
8655 break;
8656 case PTR_TO_SOCKET:
8657 case PTR_TO_SOCK_COMMON:
8658 convert_ctx_access = bpf_sock_convert_ctx_access;
8659 break;
8660 case PTR_TO_TCP_SOCK:
8661 convert_ctx_access = bpf_tcp_sock_convert_ctx_access;
8662 break;
8663 case PTR_TO_XDP_SOCK:
8664 convert_ctx_access = bpf_xdp_sock_convert_ctx_access;
8665 break;
8666 default:
8667 continue;
8668 }
8669
8670 ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size;
8671 size = BPF_LDST_BYTES(insn);
8672
8673
8674
8675
8676
8677
8678 is_narrower_load = size < ctx_field_size;
8679 size_default = bpf_ctx_off_adjust_machine(ctx_field_size);
8680 off = insn->off;
8681 if (is_narrower_load) {
8682 u8 size_code;
8683
8684 if (type == BPF_WRITE) {
8685 verbose(env, "bpf verifier narrow ctx access misconfigured\n");
8686 return -EINVAL;
8687 }
8688
8689 size_code = BPF_H;
8690 if (ctx_field_size == 4)
8691 size_code = BPF_W;
8692 else if (ctx_field_size == 8)
8693 size_code = BPF_DW;
8694
8695 insn->off = off & ~(size_default - 1);
8696 insn->code = BPF_LDX | BPF_MEM | size_code;
8697 }
8698
8699 target_size = 0;
8700 cnt = convert_ctx_access(type, insn, insn_buf, env->prog,
8701 &target_size);
8702 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) ||
8703 (ctx_field_size && !target_size)) {
8704 verbose(env, "bpf verifier is misconfigured\n");
8705 return -EINVAL;
8706 }
8707
8708 if (is_narrower_load && size < target_size) {
8709 u8 shift = bpf_ctx_narrow_access_offset(
8710 off, size, size_default) * 8;
8711 if (ctx_field_size <= 4) {
8712 if (shift)
8713 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_RSH,
8714 insn->dst_reg,
8715 shift);
8716 insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg,
8717 (1 << size * 8) - 1);
8718 } else {
8719 if (shift)
8720 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_RSH,
8721 insn->dst_reg,
8722 shift);
8723 insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg,
8724 (1ULL << size * 8) - 1);
8725 }
8726 }
8727
8728 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
8729 if (!new_prog)
8730 return -ENOMEM;
8731
8732 delta += cnt - 1;
8733
8734
8735 env->prog = new_prog;
8736 insn = new_prog->insnsi + i + delta;
8737 }
8738
8739 return 0;
8740 }
8741
8742 static int jit_subprogs(struct bpf_verifier_env *env)
8743 {
8744 struct bpf_prog *prog = env->prog, **func, *tmp;
8745 int i, j, subprog_start, subprog_end = 0, len, subprog;
8746 struct bpf_insn *insn;
8747 void *old_bpf_func;
8748 int err;
8749
8750 if (env->subprog_cnt <= 1)
8751 return 0;
8752
8753 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
8754 if (insn->code != (BPF_JMP | BPF_CALL) ||
8755 insn->src_reg != BPF_PSEUDO_CALL)
8756 continue;
8757
8758
8759
8760
8761 subprog = find_subprog(env, i + insn->imm + 1);
8762 if (subprog < 0) {
8763 WARN_ONCE(1, "verifier bug. No program starts at insn %d\n",
8764 i + insn->imm + 1);
8765 return -EFAULT;
8766 }
8767
8768
8769
8770 insn->off = subprog;
8771
8772
8773
8774 env->insn_aux_data[i].call_imm = insn->imm;
8775
8776 insn->imm = 1;
8777 }
8778
8779 err = bpf_prog_alloc_jited_linfo(prog);
8780 if (err)
8781 goto out_undo_insn;
8782
8783 err = -ENOMEM;
8784 func = kcalloc(env->subprog_cnt, sizeof(prog), GFP_KERNEL);
8785 if (!func)
8786 goto out_undo_insn;
8787
8788 for (i = 0; i < env->subprog_cnt; i++) {
8789 subprog_start = subprog_end;
8790 subprog_end = env->subprog_info[i + 1].start;
8791
8792 len = subprog_end - subprog_start;
8793
8794
8795
8796
8797
8798 func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER);
8799 if (!func[i])
8800 goto out_free;
8801 memcpy(func[i]->insnsi, &prog->insnsi[subprog_start],
8802 len * sizeof(struct bpf_insn));
8803 func[i]->type = prog->type;
8804 func[i]->len = len;
8805 if (bpf_prog_calc_tag(func[i]))
8806 goto out_free;
8807 func[i]->is_func = 1;
8808 func[i]->aux->func_idx = i;
8809
8810 func[i]->aux->btf = prog->aux->btf;
8811 func[i]->aux->func_info = prog->aux->func_info;
8812
8813
8814
8815
8816 func[i]->aux->name[0] = 'F';
8817 func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
8818 func[i]->jit_requested = 1;
8819 func[i]->aux->linfo = prog->aux->linfo;
8820 func[i]->aux->nr_linfo = prog->aux->nr_linfo;
8821 func[i]->aux->jited_linfo = prog->aux->jited_linfo;
8822 func[i]->aux->linfo_idx = env->subprog_info[i].linfo_idx;
8823 func[i] = bpf_int_jit_compile(func[i]);
8824 if (!func[i]->jited) {
8825 err = -ENOTSUPP;
8826 goto out_free;
8827 }
8828 cond_resched();
8829 }
8830
8831
8832
8833
8834 for (i = 0; i < env->subprog_cnt; i++) {
8835 insn = func[i]->insnsi;
8836 for (j = 0; j < func[i]->len; j++, insn++) {
8837 if (insn->code != (BPF_JMP | BPF_CALL) ||
8838 insn->src_reg != BPF_PSEUDO_CALL)
8839 continue;
8840 subprog = insn->off;
8841 insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) -
8842 __bpf_call_base;
8843 }
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856 func[i]->aux->func = func;
8857 func[i]->aux->func_cnt = env->subprog_cnt;
8858 }
8859 for (i = 0; i < env->subprog_cnt; i++) {
8860 old_bpf_func = func[i]->bpf_func;
8861 tmp = bpf_int_jit_compile(func[i]);
8862 if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
8863 verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
8864 err = -ENOTSUPP;
8865 goto out_free;
8866 }
8867 cond_resched();
8868 }
8869
8870
8871
8872
8873 for (i = 0; i < env->subprog_cnt; i++) {
8874 bpf_prog_lock_ro(func[i]);
8875 bpf_prog_kallsyms_add(func[i]);
8876 }
8877
8878
8879
8880
8881
8882 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
8883 if (insn->code != (BPF_JMP | BPF_CALL) ||
8884 insn->src_reg != BPF_PSEUDO_CALL)
8885 continue;
8886 insn->off = env->insn_aux_data[i].call_imm;
8887 subprog = find_subprog(env, i + insn->off + 1);
8888 insn->imm = subprog;
8889 }
8890
8891 prog->jited = 1;
8892 prog->bpf_func = func[0]->bpf_func;
8893 prog->aux->func = func;
8894 prog->aux->func_cnt = env->subprog_cnt;
8895 bpf_prog_free_unused_jited_linfo(prog);
8896 return 0;
8897 out_free:
8898 for (i = 0; i < env->subprog_cnt; i++)
8899 if (func[i])
8900 bpf_jit_free(func[i]);
8901 kfree(func);
8902 out_undo_insn:
8903
8904 prog->jit_requested = 0;
8905 for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
8906 if (insn->code != (BPF_JMP | BPF_CALL) ||
8907 insn->src_reg != BPF_PSEUDO_CALL)
8908 continue;
8909 insn->off = 0;
8910 insn->imm = env->insn_aux_data[i].call_imm;
8911 }
8912 bpf_prog_free_jited_linfo(prog);
8913 return err;
8914 }
8915
8916 static int fixup_call_args(struct bpf_verifier_env *env)
8917 {
8918 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
8919 struct bpf_prog *prog = env->prog;
8920 struct bpf_insn *insn = prog->insnsi;
8921 int i, depth;
8922 #endif
8923 int err = 0;
8924
8925 if (env->prog->jit_requested &&
8926 !bpf_prog_is_dev_bound(env->prog->aux)) {
8927 err = jit_subprogs(env);
8928 if (err == 0)
8929 return 0;
8930 if (err == -EFAULT)
8931 return err;
8932 }
8933 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
8934 for (i = 0; i < prog->len; i++, insn++) {
8935 if (insn->code != (BPF_JMP | BPF_CALL) ||
8936 insn->src_reg != BPF_PSEUDO_CALL)
8937 continue;
8938 depth = get_callee_stack_depth(env, insn, i);
8939 if (depth < 0)
8940 return depth;
8941 bpf_patch_call_args(insn, depth);
8942 }
8943 err = 0;
8944 #endif
8945 return err;
8946 }
8947
8948
8949
8950
8951
8952
8953 static int fixup_bpf_calls(struct bpf_verifier_env *env)
8954 {
8955 struct bpf_prog *prog = env->prog;
8956 struct bpf_insn *insn = prog->insnsi;
8957 const struct bpf_func_proto *fn;
8958 const int insn_cnt = prog->len;
8959 const struct bpf_map_ops *ops;
8960 struct bpf_insn_aux_data *aux;
8961 struct bpf_insn insn_buf[16];
8962 struct bpf_prog *new_prog;
8963 struct bpf_map *map_ptr;
8964 int i, cnt, delta = 0;
8965
8966 for (i = 0; i < insn_cnt; i++, insn++) {
8967 if (insn->code == (BPF_ALU64 | BPF_MOD | BPF_X) ||
8968 insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8969 insn->code == (BPF_ALU | BPF_MOD | BPF_X) ||
8970 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
8971 bool is64 = BPF_CLASS(insn->code) == BPF_ALU64;
8972 struct bpf_insn mask_and_div[] = {
8973 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8974
8975 BPF_JMP_IMM(BPF_JNE, insn->src_reg, 0, 2),
8976 BPF_ALU32_REG(BPF_XOR, insn->dst_reg, insn->dst_reg),
8977 BPF_JMP_IMM(BPF_JA, 0, 0, 1),
8978 *insn,
8979 };
8980 struct bpf_insn mask_and_mod[] = {
8981 BPF_MOV32_REG(insn->src_reg, insn->src_reg),
8982
8983 BPF_JMP_IMM(BPF_JEQ, insn->src_reg, 0, 1),
8984 *insn,
8985 };
8986 struct bpf_insn *patchlet;
8987
8988 if (insn->code == (BPF_ALU64 | BPF_DIV | BPF_X) ||
8989 insn->code == (BPF_ALU | BPF_DIV | BPF_X)) {
8990 patchlet = mask_and_div + (is64 ? 1 : 0);
8991 cnt = ARRAY_SIZE(mask_and_div) - (is64 ? 1 : 0);
8992 } else {
8993 patchlet = mask_and_mod + (is64 ? 1 : 0);
8994 cnt = ARRAY_SIZE(mask_and_mod) - (is64 ? 1 : 0);
8995 }
8996
8997 new_prog = bpf_patch_insn_data(env, i + delta, patchlet, cnt);
8998 if (!new_prog)
8999 return -ENOMEM;
9000
9001 delta += cnt - 1;
9002 env->prog = prog = new_prog;
9003 insn = new_prog->insnsi + i + delta;
9004 continue;
9005 }
9006
9007 if (BPF_CLASS(insn->code) == BPF_LD &&
9008 (BPF_MODE(insn->code) == BPF_ABS ||
9009 BPF_MODE(insn->code) == BPF_IND)) {
9010 cnt = env->ops->gen_ld_abs(insn, insn_buf);
9011 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
9012 verbose(env, "bpf verifier is misconfigured\n");
9013 return -EINVAL;
9014 }
9015
9016 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
9017 if (!new_prog)
9018 return -ENOMEM;
9019
9020 delta += cnt - 1;
9021 env->prog = prog = new_prog;
9022 insn = new_prog->insnsi + i + delta;
9023 continue;
9024 }
9025
9026 if (insn->code == (BPF_ALU64 | BPF_ADD | BPF_X) ||
9027 insn->code == (BPF_ALU64 | BPF_SUB | BPF_X)) {
9028 const u8 code_add = BPF_ALU64 | BPF_ADD | BPF_X;
9029 const u8 code_sub = BPF_ALU64 | BPF_SUB | BPF_X;
9030 struct bpf_insn insn_buf[16];
9031 struct bpf_insn *patch = &insn_buf[0];
9032 bool issrc, isneg;
9033 u32 off_reg;
9034
9035 aux = &env->insn_aux_data[i + delta];
9036 if (!aux->alu_state ||
9037 aux->alu_state == BPF_ALU_NON_POINTER)
9038 continue;
9039
9040 isneg = aux->alu_state & BPF_ALU_NEG_VALUE;
9041 issrc = (aux->alu_state & BPF_ALU_SANITIZE) ==
9042 BPF_ALU_SANITIZE_SRC;
9043
9044 off_reg = issrc ? insn->src_reg : insn->dst_reg;
9045 if (isneg)
9046 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
9047 *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1);
9048 *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
9049 *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
9050 *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
9051 *patch++ = BPF_ALU64_IMM(BPF_ARSH, BPF_REG_AX, 63);
9052 if (issrc) {
9053 *patch++ = BPF_ALU64_REG(BPF_AND, BPF_REG_AX,
9054 off_reg);
9055 insn->src_reg = BPF_REG_AX;
9056 } else {
9057 *patch++ = BPF_ALU64_REG(BPF_AND, off_reg,
9058 BPF_REG_AX);
9059 }
9060 if (isneg)
9061 insn->code = insn->code == code_add ?
9062 code_sub : code_add;
9063 *patch++ = *insn;
9064 if (issrc && isneg)
9065 *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
9066 cnt = patch - insn_buf;
9067
9068 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
9069 if (!new_prog)
9070 return -ENOMEM;
9071
9072 delta += cnt - 1;
9073 env->prog = prog = new_prog;
9074 insn = new_prog->insnsi + i + delta;
9075 continue;
9076 }
9077
9078 if (insn->code != (BPF_JMP | BPF_CALL))
9079 continue;
9080 if (insn->src_reg == BPF_PSEUDO_CALL)
9081 continue;
9082
9083 if (insn->imm == BPF_FUNC_get_route_realm)
9084 prog->dst_needed = 1;
9085 if (insn->imm == BPF_FUNC_get_prandom_u32)
9086 bpf_user_rnd_init_once();
9087 if (insn->imm == BPF_FUNC_override_return)
9088 prog->kprobe_override = 1;
9089 if (insn->imm == BPF_FUNC_tail_call) {
9090
9091
9092
9093
9094
9095 prog->cb_access = 1;
9096 env->prog->aux->stack_depth = MAX_BPF_STACK;
9097 env->prog->aux->max_pkt_offset = MAX_PACKET_OFF;
9098
9099
9100
9101
9102
9103
9104 insn->imm = 0;
9105 insn->code = BPF_JMP | BPF_TAIL_CALL;
9106
9107 aux = &env->insn_aux_data[i + delta];
9108 if (!bpf_map_ptr_unpriv(aux))
9109 continue;
9110
9111
9112
9113
9114
9115
9116
9117 if (bpf_map_ptr_poisoned(aux)) {
9118 verbose(env, "tail_call abusing map_ptr\n");
9119 return -EINVAL;
9120 }
9121
9122 map_ptr = BPF_MAP_PTR(aux->map_state);
9123 insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3,
9124 map_ptr->max_entries, 2);
9125 insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3,
9126 container_of(map_ptr,
9127 struct bpf_array,
9128 map)->index_mask);
9129 insn_buf[2] = *insn;
9130 cnt = 3;
9131 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
9132 if (!new_prog)
9133 return -ENOMEM;
9134
9135 delta += cnt - 1;
9136 env->prog = prog = new_prog;
9137 insn = new_prog->insnsi + i + delta;
9138 continue;
9139 }
9140
9141
9142
9143
9144
9145 if (prog->jit_requested && BITS_PER_LONG == 64 &&
9146 (insn->imm == BPF_FUNC_map_lookup_elem ||
9147 insn->imm == BPF_FUNC_map_update_elem ||
9148 insn->imm == BPF_FUNC_map_delete_elem ||
9149 insn->imm == BPF_FUNC_map_push_elem ||
9150 insn->imm == BPF_FUNC_map_pop_elem ||
9151 insn->imm == BPF_FUNC_map_peek_elem)) {
9152 aux = &env->insn_aux_data[i + delta];
9153 if (bpf_map_ptr_poisoned(aux))
9154 goto patch_call_imm;
9155
9156 map_ptr = BPF_MAP_PTR(aux->map_state);
9157 ops = map_ptr->ops;
9158 if (insn->imm == BPF_FUNC_map_lookup_elem &&
9159 ops->map_gen_lookup) {
9160 cnt = ops->map_gen_lookup(map_ptr, insn_buf);
9161 if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) {
9162 verbose(env, "bpf verifier is misconfigured\n");
9163 return -EINVAL;
9164 }
9165
9166 new_prog = bpf_patch_insn_data(env, i + delta,
9167 insn_buf, cnt);
9168 if (!new_prog)
9169 return -ENOMEM;
9170
9171 delta += cnt - 1;
9172 env->prog = prog = new_prog;
9173 insn = new_prog->insnsi + i + delta;
9174 continue;
9175 }
9176
9177 BUILD_BUG_ON(!__same_type(ops->map_lookup_elem,
9178 (void *(*)(struct bpf_map *map, void *key))NULL));
9179 BUILD_BUG_ON(!__same_type(ops->map_delete_elem,
9180 (int (*)(struct bpf_map *map, void *key))NULL));
9181 BUILD_BUG_ON(!__same_type(ops->map_update_elem,
9182 (int (*)(struct bpf_map *map, void *key, void *value,
9183 u64 flags))NULL));
9184 BUILD_BUG_ON(!__same_type(ops->map_push_elem,
9185 (int (*)(struct bpf_map *map, void *value,
9186 u64 flags))NULL));
9187 BUILD_BUG_ON(!__same_type(ops->map_pop_elem,
9188 (int (*)(struct bpf_map *map, void *value))NULL));
9189 BUILD_BUG_ON(!__same_type(ops->map_peek_elem,
9190 (int (*)(struct bpf_map *map, void *value))NULL));
9191
9192 switch (insn->imm) {
9193 case BPF_FUNC_map_lookup_elem:
9194 insn->imm = BPF_CAST_CALL(ops->map_lookup_elem) -
9195 __bpf_call_base;
9196 continue;
9197 case BPF_FUNC_map_update_elem:
9198 insn->imm = BPF_CAST_CALL(ops->map_update_elem) -
9199 __bpf_call_base;
9200 continue;
9201 case BPF_FUNC_map_delete_elem:
9202 insn->imm = BPF_CAST_CALL(ops->map_delete_elem) -
9203 __bpf_call_base;
9204 continue;
9205 case BPF_FUNC_map_push_elem:
9206 insn->imm = BPF_CAST_CALL(ops->map_push_elem) -
9207 __bpf_call_base;
9208 continue;
9209 case BPF_FUNC_map_pop_elem:
9210 insn->imm = BPF_CAST_CALL(ops->map_pop_elem) -
9211 __bpf_call_base;
9212 continue;
9213 case BPF_FUNC_map_peek_elem:
9214 insn->imm = BPF_CAST_CALL(ops->map_peek_elem) -
9215 __bpf_call_base;
9216 continue;
9217 }
9218
9219 goto patch_call_imm;
9220 }
9221
9222 patch_call_imm:
9223 fn = env->ops->get_func_proto(insn->imm, env->prog);
9224
9225
9226
9227 if (!fn->func) {
9228 verbose(env,
9229 "kernel subsystem misconfigured func %s#%d\n",
9230 func_id_name(insn->imm), insn->imm);
9231 return -EFAULT;
9232 }
9233 insn->imm = fn->func - __bpf_call_base;
9234 }
9235
9236 return 0;
9237 }
9238
9239 static void free_states(struct bpf_verifier_env *env)
9240 {
9241 struct bpf_verifier_state_list *sl, *sln;
9242 int i;
9243
9244 sl = env->free_list;
9245 while (sl) {
9246 sln = sl->next;
9247 free_verifier_state(&sl->state, false);
9248 kfree(sl);
9249 sl = sln;
9250 }
9251
9252 if (!env->explored_states)
9253 return;
9254
9255 for (i = 0; i < state_htab_size(env); i++) {
9256 sl = env->explored_states[i];
9257
9258 while (sl) {
9259 sln = sl->next;
9260 free_verifier_state(&sl->state, false);
9261 kfree(sl);
9262 sl = sln;
9263 }
9264 }
9265
9266 kvfree(env->explored_states);
9267 }
9268
9269 static void print_verification_stats(struct bpf_verifier_env *env)
9270 {
9271 int i;
9272
9273 if (env->log.level & BPF_LOG_STATS) {
9274 verbose(env, "verification time %lld usec\n",
9275 div_u64(env->verification_time, 1000));
9276 verbose(env, "stack depth ");
9277 for (i = 0; i < env->subprog_cnt; i++) {
9278 u32 depth = env->subprog_info[i].stack_depth;
9279
9280 verbose(env, "%d", depth);
9281 if (i + 1 < env->subprog_cnt)
9282 verbose(env, "+");
9283 }
9284 verbose(env, "\n");
9285 }
9286 verbose(env, "processed %d insns (limit %d) max_states_per_insn %d "
9287 "total_states %d peak_states %d mark_read %d\n",
9288 env->insn_processed, BPF_COMPLEXITY_LIMIT_INSNS,
9289 env->max_states_per_insn, env->total_states,
9290 env->peak_states, env->longest_mark_read_walk);
9291 }
9292
9293 int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
9294 union bpf_attr __user *uattr)
9295 {
9296 u64 start_time = ktime_get_ns();
9297 struct bpf_verifier_env *env;
9298 struct bpf_verifier_log *log;
9299 int i, len, ret = -EINVAL;
9300 bool is_priv;
9301
9302
9303 if (ARRAY_SIZE(bpf_verifier_ops) == 0)
9304 return -EINVAL;
9305
9306
9307
9308
9309 env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL);
9310 if (!env)
9311 return -ENOMEM;
9312 log = &env->log;
9313
9314 len = (*prog)->len;
9315 env->insn_aux_data =
9316 vzalloc(array_size(sizeof(struct bpf_insn_aux_data), len));
9317 ret = -ENOMEM;
9318 if (!env->insn_aux_data)
9319 goto err_free_env;
9320 for (i = 0; i < len; i++)
9321 env->insn_aux_data[i].orig_idx = i;
9322 env->prog = *prog;
9323 env->ops = bpf_verifier_ops[env->prog->type];
9324 is_priv = capable(CAP_SYS_ADMIN);
9325
9326
9327 if (!is_priv)
9328 mutex_lock(&bpf_verifier_lock);
9329
9330 if (attr->log_level || attr->log_buf || attr->log_size) {
9331
9332
9333
9334 log->level = attr->log_level;
9335 log->ubuf = (char __user *) (unsigned long) attr->log_buf;
9336 log->len_total = attr->log_size;
9337
9338 ret = -EINVAL;
9339
9340 if (log->len_total < 128 || log->len_total > UINT_MAX >> 2 ||
9341 !log->level || !log->ubuf || log->level & ~BPF_LOG_MASK)
9342 goto err_unlock;
9343 }
9344
9345 env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT);
9346 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
9347 env->strict_alignment = true;
9348 if (attr->prog_flags & BPF_F_ANY_ALIGNMENT)
9349 env->strict_alignment = false;
9350
9351 env->allow_ptr_leaks = is_priv;
9352
9353 if (is_priv)
9354 env->test_state_freq = attr->prog_flags & BPF_F_TEST_STATE_FREQ;
9355
9356 ret = replace_map_fd_with_map_ptr(env);
9357 if (ret < 0)
9358 goto skip_full_check;
9359
9360 if (bpf_prog_is_dev_bound(env->prog->aux)) {
9361 ret = bpf_prog_offload_verifier_prep(env->prog);
9362 if (ret)
9363 goto skip_full_check;
9364 }
9365
9366 env->explored_states = kvcalloc(state_htab_size(env),
9367 sizeof(struct bpf_verifier_state_list *),
9368 GFP_USER);
9369 ret = -ENOMEM;
9370 if (!env->explored_states)
9371 goto skip_full_check;
9372
9373 ret = check_subprogs(env);
9374 if (ret < 0)
9375 goto skip_full_check;
9376
9377 ret = check_btf_info(env, attr, uattr);
9378 if (ret < 0)
9379 goto skip_full_check;
9380
9381 ret = check_cfg(env);
9382 if (ret < 0)
9383 goto skip_full_check;
9384
9385 ret = do_check(env);
9386 if (env->cur_state) {
9387 free_verifier_state(env->cur_state, true);
9388 env->cur_state = NULL;
9389 }
9390
9391 if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
9392 ret = bpf_prog_offload_finalize(env);
9393
9394 skip_full_check:
9395 while (!pop_stack(env, NULL, NULL));
9396 free_states(env);
9397
9398 if (ret == 0)
9399 ret = check_max_stack_depth(env);
9400
9401
9402 if (is_priv) {
9403 if (ret == 0)
9404 opt_hard_wire_dead_code_branches(env);
9405 if (ret == 0)
9406 ret = opt_remove_dead_code(env);
9407 if (ret == 0)
9408 ret = opt_remove_nops(env);
9409 } else {
9410 if (ret == 0)
9411 sanitize_dead_code(env);
9412 }
9413
9414 if (ret == 0)
9415
9416 ret = convert_ctx_accesses(env);
9417
9418 if (ret == 0)
9419 ret = fixup_bpf_calls(env);
9420
9421
9422
9423
9424 if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) {
9425 ret = opt_subreg_zext_lo32_rnd_hi32(env, attr);
9426 env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret
9427 : false;
9428 }
9429
9430 if (ret == 0)
9431 ret = fixup_call_args(env);
9432
9433 env->verification_time = ktime_get_ns() - start_time;
9434 print_verification_stats(env);
9435
9436 if (log->level && bpf_verifier_log_full(log))
9437 ret = -ENOSPC;
9438 if (log->level && !log->ubuf) {
9439 ret = -EFAULT;
9440 goto err_release_maps;
9441 }
9442
9443 if (ret == 0 && env->used_map_cnt) {
9444
9445 env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt,
9446 sizeof(env->used_maps[0]),
9447 GFP_KERNEL);
9448
9449 if (!env->prog->aux->used_maps) {
9450 ret = -ENOMEM;
9451 goto err_release_maps;
9452 }
9453
9454 memcpy(env->prog->aux->used_maps, env->used_maps,
9455 sizeof(env->used_maps[0]) * env->used_map_cnt);
9456 env->prog->aux->used_map_cnt = env->used_map_cnt;
9457
9458
9459
9460
9461 convert_pseudo_ld_imm64(env);
9462 }
9463
9464 if (ret == 0)
9465 adjust_btf_func(env);
9466
9467 err_release_maps:
9468 if (!env->prog->aux->used_maps)
9469
9470
9471
9472 release_maps(env);
9473 *prog = env->prog;
9474 err_unlock:
9475 if (!is_priv)
9476 mutex_unlock(&bpf_verifier_lock);
9477 vfree(env->insn_aux_data);
9478 err_free_env:
9479 kfree(env);
9480 return ret;
9481 }