提交 e28b1ea3 编写于 作者: P Piotr Krysiuk 提交者: Yang Yingliang

bpf: Simplify alu_limit masking for pointer arithmetic

commit b5871dca upstream.

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.
Signed-off-by: NPiotr Krysiuk <piotras@gmail.com>
Co-developed-by: NDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Acked-by: NAlexei Starovoitov <ast@kernel.org>
Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 f4b3a000
...@@ -2740,16 +2740,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, ...@@ -2740,16 +2740,16 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
case PTR_TO_STACK: case PTR_TO_STACK:
off = ptr_reg->off + ptr_reg->var_off.value; off = ptr_reg->off + ptr_reg->var_off.value;
if (mask_to_left) if (mask_to_left)
*ptr_limit = MAX_BPF_STACK + off + 1; *ptr_limit = MAX_BPF_STACK + off;
else else
*ptr_limit = -off; *ptr_limit = -off - 1;
return 0; return 0;
case PTR_TO_MAP_VALUE: case PTR_TO_MAP_VALUE:
if (mask_to_left) { if (mask_to_left) {
*ptr_limit = ptr_reg->umax_value + ptr_reg->off + 1; *ptr_limit = ptr_reg->umax_value + ptr_reg->off;
} else { } else {
off = ptr_reg->smin_value + ptr_reg->off; off = ptr_reg->smin_value + ptr_reg->off;
*ptr_limit = ptr_reg->map_ptr->value_size - off; *ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
} }
return 0; return 0;
default: default:
...@@ -6082,7 +6082,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env) ...@@ -6082,7 +6082,7 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
off_reg = issrc ? insn->src_reg : insn->dst_reg; off_reg = issrc ? insn->src_reg : insn->dst_reg;
if (isneg) if (isneg)
*patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1); *patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
*patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit - 1); *patch++ = BPF_MOV32_IMM(BPF_REG_AX, aux->alu_limit);
*patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg); *patch++ = BPF_ALU64_REG(BPF_SUB, BPF_REG_AX, off_reg);
*patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg); *patch++ = BPF_ALU64_REG(BPF_OR, BPF_REG_AX, off_reg);
*patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0); *patch++ = BPF_ALU64_IMM(BPF_NEG, BPF_REG_AX, 0);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册