Instead of having the mov32 with aux->alu_limit - 1 immediate, move this operation to retrieve_ptr_limit() instead to simplify the logic and to allow for subsequent sanity boundary checks inside retrieve_ptr_limit(). This avoids in future that at the time of the verifier masking rewrite we'd run into an underflow which would not sign extend due to the nature of mov32 instruction.
Unified · Split
| @@ -5870,16 +5870,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, · 5869 unmodified lines | |||
| 5870 | */ | 5870 | */ |
| 5871 | off = ptr_reg->off + ptr_reg->var_off.value; | 5871 | off = ptr_reg->off + ptr_reg->var_off.value; |
| 5872 | if (mask_to_left) | 5872 | if (mask_to_left) |
| 5873 | *ptr_limit = MAX_BPF_STACK + off + 1; | 5873 | *ptr_limit = MAX_BPF_STACK + off; |
| 5874 | else | 5874 | else |
| 5875 | *ptr_limit = -off; | 5875 | *ptr_limit = -off - 1; |
| 5876 | return 0; | 5876 | return 0; |
| 5877 | case PTR_TO_MAP_VALUE: | 5877 | case PTR_TO_MAP_VALUE: |
| 5878 | if (mask_to_left) { | 5878 | if (mask_to_left) { |
| 5879 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off + 1; | 5879 | *ptr_limit = ptr_reg->umax_value + ptr_reg->off; |
| 5880 | } else { | 5880 | } else { |
| 5881 | off = ptr_reg->smin_value + ptr_reg->off; | 5881 | off = ptr_reg->smin_value + ptr_reg->off; |
| 5882 | *ptr_limit = ptr_reg->map_ptr->value_size - off; | 5882 | *ptr_limit = ptr_reg->map_ptr->value_size - off - 1; |
| 5883 | } | 5883 | } |
| 5884 | return 0; | 5884 | return 0; |
| 5885 | default: | 5885 | default: |
| @@ -11668,7 +11668,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) · 5782 unmodified lines | |||
| 11668 | off_reg = issrc ? insn->src_reg : insn->dst_reg; | 11668 | off_reg = issrc ? insn->src_reg : insn->dst_reg; |
| 11669 | if (isneg) | 11669 | if (isneg) |
| 11670 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); | 11670 | *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); |
| 11671 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); | 11671 | *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit); |
| 11672 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); | 11672 | *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); |
| 11673 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); | 11673 | *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); |
| 11674 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); | 11674 | *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); |