Skip to content

Commit f7fbedc

Browse files
borkmanngregkh
authored andcommitted
bpf: Ensure off_reg has no mixed signed bounds for all types
commit 24c109b upstream. The mixed signed bounds check really belongs into retrieve_ptr_limit() instead of outside of it in adjust_ptr_min_max_vals(). The reason is that this check is not tied to PTR_TO_MAP_VALUE only, but to all pointer types that we handle in retrieve_ptr_limit() and given errors from the latter propagate back to adjust_ptr_min_max_vals() and lead to rejection of the program, it's a better place to reside to avoid anything slipping through for future types. The reason why we must reject such off_reg is that we otherwise would not be able to derive a mask, see details in 9d7ecee ("bpf: restrict unknown scalars of mixed signed bounds for unprivileged"). Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> [fllinden@amazon.com: backport to 5.4] Signed-off-by: Frank van der Linden <fllinden@amazon.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4a163b1 commit f7fbedc

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4264,12 +4264,18 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
42644264
}
42654265

42664266
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
4267-
u32 *ptr_limit, u8 opcode, bool off_is_neg)
4267+
const struct bpf_reg_state *off_reg,
4268+
u32 *ptr_limit, u8 opcode)
42684269
{
4270+
bool off_is_neg = off_reg->smin_value < 0;
42694271
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
42704272
(opcode == BPF_SUB && !off_is_neg);
42714273
u32 off, max;
42724274

4275+
if (!tnum_is_const(off_reg->var_off) &&
4276+
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
4277+
return -EACCES;
4278+
42734279
switch (ptr_reg->type) {
42744280
case PTR_TO_STACK:
42754281
/* Offset 0 is out-of-bounds, but acceptable start for the
@@ -4363,7 +4369,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
43634369
alu_state |= ptr_is_dst_reg ?
43644370
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
43654371

4366-
err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg);
4372+
err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
43674373
if (err < 0)
43684374
return err;
43694375

@@ -4408,8 +4414,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
44084414
smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
44094415
u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
44104416
umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
4411-
u32 dst = insn->dst_reg, src = insn->src_reg;
44124417
u8 opcode = BPF_OP(insn->code);
4418+
u32 dst = insn->dst_reg;
44134419
int ret;
44144420

44154421
dst_reg = &regs[dst];
@@ -4452,13 +4458,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
44524458
verbose(env, "R%d pointer arithmetic on %s prohibited\n",
44534459
dst, reg_type_str[ptr_reg->type]);
44544460
return -EACCES;
4455-
case PTR_TO_MAP_VALUE:
4456-
if (!env->allow_ptr_leaks && !known && (smin_val < 0) != (smax_val < 0)) {
4457-
verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
4458-
off_reg == dst_reg ? dst : src);
4459-
return -EACCES;
4460-
}
4461-
/* fall-through */
44624461
default:
44634462
break;
44644463
}

0 commit comments

Comments
 (0)