Skip to content

Commit b658bbb

Browse files
committed
bpf: Rework ptr_limit into alu_limit and add common error path
Small refactor with no semantic changes in order to consolidate the max ptr_limit boundary check. Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent 24c109b commit b658bbb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

kernel/bpf/verifier.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5858,12 +5858,12 @@ static struct bpf_insn_aux_data *cur_aux(struct bpf_verifier_env *env)
58585858

58595859
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
58605860
const struct bpf_reg_state *off_reg,
5861-
u32 *ptr_limit, u8 opcode)
5861+
u32 *alu_limit, u8 opcode)
58625862
{
58635863
bool off_is_neg = off_reg->smin_value < 0;
58645864
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
58655865
(opcode == BPF_SUB && !off_is_neg);
5866-
u32 off, max;
5866+
u32 off, max = 0, ptr_limit = 0;
58675867

58685868
if (!tnum_is_const(off_reg->var_off) &&
58695869
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -5880,22 +5880,27 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
58805880
*/
58815881
off = ptr_reg->off + ptr_reg->var_off.value;
58825882
if (mask_to_left)
5883-
*ptr_limit = MAX_BPF_STACK + off;
5883+
ptr_limit = MAX_BPF_STACK + off;
58845884
else
5885-
*ptr_limit = -off - 1;
5886-
return *ptr_limit >= max ? -ERANGE : 0;
5885+
ptr_limit = -off - 1;
5886+
break;
58875887
case PTR_TO_MAP_VALUE:
58885888
max = ptr_reg->map_ptr->value_size;
58895889
if (mask_to_left) {
5890-
*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
5890+
ptr_limit = ptr_reg->umax_value + ptr_reg->off;
58915891
} else {
58925892
off = ptr_reg->smin_value + ptr_reg->off;
5893-
*ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
5893+
ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
58945894
}
5895-
return *ptr_limit >= max ? -ERANGE : 0;
5895+
break;
58965896
default:
58975897
return -EINVAL;
58985898
}
5899+
5900+
if (ptr_limit >= max)
5901+
return -ERANGE;
5902+
*alu_limit = ptr_limit;
5903+
return 0;
58995904
}
59005905

59015906
static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,

0 commit comments

Comments
 (0)