Skip to content

Commit 24c109b

Browse files
committed
bpf: Ensure off_reg has no mixed signed bounds for all types
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 <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent 6f55b2f commit 24c109b

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

kernel/bpf/verifier.c

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

58595859
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
5860-
u32 *ptr_limit, u8 opcode, bool off_is_neg)
5860+
const struct bpf_reg_state *off_reg,
5861+
u32 *ptr_limit, u8 opcode)
58615862
{
5863+
bool off_is_neg = off_reg->smin_value < 0;
58625864
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
58635865
(opcode == BPF_SUB && !off_is_neg);
58645866
u32 off, max;
58655867

5868+
if (!tnum_is_const(off_reg->var_off) &&
5869+
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
5870+
return -EACCES;
5871+
58665872
switch (ptr_reg->type) {
58675873
case PTR_TO_STACK:
58685874
/* Offset 0 is out-of-bounds, but acceptable start for the
@@ -5956,7 +5962,7 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
59565962
alu_state |= ptr_is_dst_reg ?
59575963
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
59585964

5959-
err = retrieve_ptr_limit(ptr_reg, &alu_limit, opcode, off_is_neg);
5965+
err = retrieve_ptr_limit(ptr_reg, off_reg, &alu_limit, opcode);
59605966
if (err < 0)
59615967
return err;
59625968

@@ -6036,8 +6042,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
60366042
smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value;
60376043
u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value,
60386044
umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value;
6039-
u32 dst = insn->dst_reg, src = insn->src_reg;
60406045
u8 opcode = BPF_OP(insn->code);
6046+
u32 dst = insn->dst_reg;
60416047
int ret;
60426048

60436049
dst_reg = &regs[dst];
@@ -6085,13 +6091,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
60856091
verbose(env, "R%d pointer arithmetic on %s prohibited\n",
60866092
dst, reg_type_str[ptr_reg->type]);
60876093
return -EACCES;
6088-
case PTR_TO_MAP_VALUE:
6089-
if (!env->env->bypass_spec_v1 && !known && (smin_val < 0) != (smax_val < 0)) {
6090-
verbose(env, "R%d has unknown scalar with mixed signed bounds, pointer arithmetic with it prohibited for !root\n",
6091-
off_reg == dst_reg ? dst : src);
6092-
return -EACCES;
6093-
}
6094-
fallthrough;
60956094
default:
60966095
break;
60976096
}

0 commit comments

Comments
 (0)