Skip to content

Commit 15de0c5

Browse files
borkmanngregkh
authored andcommitted
bpf: Rework ptr_limit into alu_limit and add common error path
commit b658bbb upstream. Small refactor with no semantic changes in order to consolidate the max ptr_limit boundary check. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f7fbedc commit 15de0c5

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

kernel/bpf/verifier.c

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

42664266
static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
42674267
const struct bpf_reg_state *off_reg,
4268-
u32 *ptr_limit, u8 opcode)
4268+
u32 *alu_limit, u8 opcode)
42694269
{
42704270
bool off_is_neg = off_reg->smin_value < 0;
42714271
bool mask_to_left = (opcode == BPF_ADD && off_is_neg) ||
42724272
(opcode == BPF_SUB && !off_is_neg);
4273-
u32 off, max;
4273+
u32 off, max = 0, ptr_limit = 0;
42744274

42754275
if (!tnum_is_const(off_reg->var_off) &&
42764276
(off_reg->smin_value < 0) != (off_reg->smax_value < 0))
@@ -4287,22 +4287,27 @@ static int retrieve_ptr_limit(const struct bpf_reg_state *ptr_reg,
42874287
*/
42884288
off = ptr_reg->off + ptr_reg->var_off.value;
42894289
if (mask_to_left)
4290-
*ptr_limit = MAX_BPF_STACK + off;
4290+
ptr_limit = MAX_BPF_STACK + off;
42914291
else
4292-
*ptr_limit = -off - 1;
4293-
return *ptr_limit >= max ? -ERANGE : 0;
4292+
ptr_limit = -off - 1;
4293+
break;
42944294
case PTR_TO_MAP_VALUE:
42954295
max = ptr_reg->map_ptr->value_size;
42964296
if (mask_to_left) {
4297-
*ptr_limit = ptr_reg->umax_value + ptr_reg->off;
4297+
ptr_limit = ptr_reg->umax_value + ptr_reg->off;
42984298
} else {
42994299
off = ptr_reg->smin_value + ptr_reg->off;
4300-
*ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
4300+
ptr_limit = ptr_reg->map_ptr->value_size - off - 1;
43014301
}
4302-
return *ptr_limit >= max ? -ERANGE : 0;
4302+
break;
43034303
default:
43044304
return -EINVAL;
43054305
}
4306+
4307+
if (ptr_limit >= max)
4308+
return -ERANGE;
4309+
*alu_limit = ptr_limit;
4310+
return 0;
43064311
}
43074312

43084313
static bool can_skip_alu_sanitation(const struct bpf_verifier_env *env,

0 commit comments

Comments
 (0)