@@ -6409,18 +6409,10 @@ enum {
64096409};
64106410
64116411static int retrieve_ptr_limit (const struct bpf_reg_state * ptr_reg ,
6412- const struct bpf_reg_state * off_reg ,
6413- u32 * alu_limit , u8 opcode )
6412+ u32 * alu_limit , bool mask_to_left )
64146413{
6415- bool off_is_neg = off_reg -> smin_value < 0 ;
6416- bool mask_to_left = (opcode == BPF_ADD && off_is_neg ) ||
6417- (opcode == BPF_SUB && !off_is_neg );
64186414 u32 max = 0 , ptr_limit = 0 ;
64196415
6420- if (!tnum_is_const (off_reg -> var_off ) &&
6421- (off_reg -> smin_value < 0 ) != (off_reg -> smax_value < 0 ))
6422- return REASON_BOUNDS ;
6423-
64246416 switch (ptr_reg -> type ) {
64256417 case PTR_TO_STACK :
64266418 /* Offset 0 is out-of-bounds, but acceptable start for the
@@ -6486,15 +6478,20 @@ static bool sanitize_needed(u8 opcode)
64866478 return opcode == BPF_ADD || opcode == BPF_SUB ;
64876479}
64886480
6481+ struct bpf_sanitize_info {
6482+ struct bpf_insn_aux_data aux ;
6483+ bool mask_to_left ;
6484+ };
6485+
64896486static int sanitize_ptr_alu (struct bpf_verifier_env * env ,
64906487 struct bpf_insn * insn ,
64916488 const struct bpf_reg_state * ptr_reg ,
64926489 const struct bpf_reg_state * off_reg ,
64936490 struct bpf_reg_state * dst_reg ,
6494- struct bpf_insn_aux_data * tmp_aux ,
6491+ struct bpf_sanitize_info * info ,
64956492 const bool commit_window )
64966493{
6497- struct bpf_insn_aux_data * aux = commit_window ? cur_aux (env ) : tmp_aux ;
6494+ struct bpf_insn_aux_data * aux = commit_window ? cur_aux (env ) : & info -> aux ;
64986495 struct bpf_verifier_state * vstate = env -> cur_state ;
64996496 bool off_is_imm = tnum_is_const (off_reg -> var_off );
65006497 bool off_is_neg = off_reg -> smin_value < 0 ;
@@ -6515,16 +6512,25 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
65156512 if (vstate -> speculative )
65166513 goto do_sim ;
65176514
6518- err = retrieve_ptr_limit (ptr_reg , off_reg , & alu_limit , opcode );
6515+ if (!commit_window ) {
6516+ if (!tnum_is_const (off_reg -> var_off ) &&
6517+ (off_reg -> smin_value < 0 ) != (off_reg -> smax_value < 0 ))
6518+ return REASON_BOUNDS ;
6519+
6520+ info -> mask_to_left = (opcode == BPF_ADD && off_is_neg ) ||
6521+ (opcode == BPF_SUB && !off_is_neg );
6522+ }
6523+
6524+ err = retrieve_ptr_limit (ptr_reg , & alu_limit , info -> mask_to_left );
65196525 if (err < 0 )
65206526 return err ;
65216527
65226528 if (commit_window ) {
65236529 /* In commit phase we narrow the masking window based on
65246530 * the observed pointer move after the simulated operation.
65256531 */
6526- alu_state = tmp_aux -> alu_state ;
6527- alu_limit = abs (tmp_aux -> alu_limit - alu_limit );
6532+ alu_state = info -> aux . alu_state ;
6533+ alu_limit = abs (info -> aux . alu_limit - alu_limit );
65286534 } else {
65296535 alu_state = off_is_neg ? BPF_ALU_NEG_VALUE : 0 ;
65306536 alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0 ;
@@ -6539,8 +6545,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
65396545 /* If we're in commit phase, we're done here given we already
65406546 * pushed the truncated dst_reg into the speculative verification
65416547 * stack.
6548+ *
6549+ * Also, when register is a known constant, we rewrite register-based
6550+ * operation to immediate-based, and thus do not need masking (and as
6551+ * a consequence, do not need to simulate the zero-truncation either).
65426552 */
6543- if (commit_window )
6553+ if (commit_window || off_is_imm )
65446554 return 0 ;
65456555
65466556 /* Simulate and find potential out-of-bounds access under
@@ -6685,7 +6695,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
66856695 smin_ptr = ptr_reg -> smin_value , smax_ptr = ptr_reg -> smax_value ;
66866696 u64 umin_val = off_reg -> umin_value , umax_val = off_reg -> umax_value ,
66876697 umin_ptr = ptr_reg -> umin_value , umax_ptr = ptr_reg -> umax_value ;
6688- struct bpf_insn_aux_data tmp_aux = {};
6698+ struct bpf_sanitize_info info = {};
66896699 u8 opcode = BPF_OP (insn -> code );
66906700 u32 dst = insn -> dst_reg ;
66916701 int ret ;
@@ -6754,7 +6764,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
67546764
67556765 if (sanitize_needed (opcode )) {
67566766 ret = sanitize_ptr_alu (env , insn , ptr_reg , off_reg , dst_reg ,
6757- & tmp_aux , false);
6767+ & info , false);
67586768 if (ret < 0 )
67596769 return sanitize_err (env , insn , ret , off_reg , dst_reg );
67606770 }
@@ -6895,7 +6905,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
68956905 return - EACCES ;
68966906 if (sanitize_needed (opcode )) {
68976907 ret = sanitize_ptr_alu (env , insn , dst_reg , off_reg , dst_reg ,
6898- & tmp_aux , true);
6908+ & info , true);
68996909 if (ret < 0 )
69006910 return sanitize_err (env , insn , ret , off_reg , dst_reg );
69016911 }
@@ -13368,12 +13378,6 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1336813378 if (is_priv )
1336913379 env -> test_state_freq = attr -> prog_flags & BPF_F_TEST_STATE_FREQ ;
1337013380
13371- if (bpf_prog_is_dev_bound (env -> prog -> aux )) {
13372- ret = bpf_prog_offload_verifier_prep (env -> prog );
13373- if (ret )
13374- goto skip_full_check ;
13375- }
13376-
1337713381 env -> explored_states = kvcalloc (state_htab_size (env ),
1337813382 sizeof (struct bpf_verifier_state_list * ),
1337913383 GFP_USER );
@@ -13401,6 +13405,12 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
1340113405 if (ret < 0 )
1340213406 goto skip_full_check ;
1340313407
13408+ if (bpf_prog_is_dev_bound (env -> prog -> aux )) {
13409+ ret = bpf_prog_offload_verifier_prep (env -> prog );
13410+ if (ret )
13411+ goto skip_full_check ;
13412+ }
13413+
1340413414 ret = check_cfg (env );
1340513415 if (ret < 0 )
1340613416 goto skip_full_check ;
0 commit comments