提交 ad05322f 编写于 作者: D Daniel Borkmann 提交者: Yang Yingliang

bpf: Rework ptr_limit into alu_limit and add common error path

mainline inclusion
from mainline-v5.12-rc8
commit b658bbb8
category: bugfix
bugzilla: NA
CVE: CVE-2021-29155

--------------------------------

Small refactor with no semantic changes in order to consolidate the max
ptr_limit boundary check.
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Reviewed-by: NJohn Fastabend <john.fastabend@gmail.com>
Acked-by: NAlexei Starovoitov <ast@kernel.org>
Conflicts:
  kernel/bpf/verifier.c
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
Reviewed-by: NKuohai Xu <xukuohai@huawei.com>
Reviewed-by: NXiu Jianfeng <xiujianfeng@huawei.com>
Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
上级 f902f644
...@@ -2731,12 +2731,12 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env) ...@@ -2731,12 +2731,12 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
const struct bpf_reg_state *off_reg, const struct bpf_reg_state *off_reg,
u32 *ptr_limit, u8 opcode) u32 *alu_limit, u8 opcode)
{ {
bool off_is_neg = off_reg->smin_value < 0; bool off_is_neg = off_reg->smin_value < 0;
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) || bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
(opcode == BPF_SUB && !off_is_neg); (opcode == BPF_SUB && !off_is_neg);
u32 off, max; u32 off, max = 0, ptr_limit = 0;
if (!tnum_is_const(off_reg->var_off) && if (!tnum_is_const(off_reg->var_off) &&
(off_reg->smin_value < 0) != (off_reg->smax_value < 0)) (off_reg->smin_value < 0) != (off_reg->smax_value < 0))
...@@ -2750,22 +2750,27 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg, ...@@ -2750,22 +2750,27 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
max = MAX_BPF_STACK + mask_to_left; max = MAX_BPF_STACK + mask_to_left;
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; ptr_limit = MAX_BPF_STACK + off;
else else
*ptr_limit = -off - 1; ptr_limit = -off - 1;
return *ptr_limit >= max ? -ERANGE : 0; break;
case PTR_TO_MAP_VALUE: case PTR_TO_MAP_VALUE:
max = ptr_reg->map_ptr->value_size; max = ptr_reg->map_ptr->value_size;
if (mask_to_left) { if (mask_to_left) {
*ptr_limit = ptr_reg->umax_value + ptr_reg->off; 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 - 1; ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
} }
return *ptr_limit >= max ? -ERANGE : 0; break;
default: default:
return -EINVAL; return -EINVAL;
} }
if (ptr_limit >= max)
return -ERANGE;
*alu_limit = ptr_limit;
return 0;
} }
static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env, static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册