Skip to content

Commit 073815b

Browse files
committed
bpf: Refactor and streamline bounds check into helper
Move the bounds check in adjust_ptr_min_max_vals() into a small helper named sanitize_check_bounds() in order to simplify the former a bit. Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: John Fastabend <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent a6aaece commit 073815b

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

kernel/bpf/verifier.c

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6075,6 +6075,37 @@ static int check_stack_access_for_ptr_arithmetic(
60756075
return 0;
60766076
}
60776077

6078+
static int sanitize_check_bounds(struct bpf_verifier_env *env,
6079+
const struct bpf_insn *insn,
6080+
const struct bpf_reg_state *dst_reg)
6081+
{
6082+
u32 dst = insn->dst_reg;
6083+
6084+
/* For unprivileged we require that resulting offset must be in bounds
6085+
* in order to be able to sanitize access later on.
6086+
*/
6087+
if (env->bypass_spec_v1)
6088+
return 0;
6089+
6090+
switch (dst_reg->type) {
6091+
case PTR_TO_STACK:
6092+
if (check_stack_access_for_ptr_arithmetic(env, dst, dst_reg,
6093+
dst_reg->off + dst_reg->var_off.value))
6094+
return -EACCES;
6095+
break;
6096+
case PTR_TO_MAP_VALUE:
6097+
if (check_map_access(env, dst, dst_reg->off, 1, false)) {
6098+
verbose(env, "R%d pointer arithmetic of map value goes out of range, "
6099+
"prohibited for !root\n", dst);
6100+
return -EACCES;
6101+
}
6102+
break;
6103+
default:
6104+
break;
6105+
}
6106+
6107+
return 0;
6108+
}
60786109

60796110
/* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off.
60806111
* Caller should also handle BPF_MOV case separately.
@@ -6300,22 +6331,8 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
63006331
__reg_deduce_bounds(dst_reg);
63016332
__reg_bound_offset(dst_reg);
63026333

6303-
/* For unprivileged we require that resulting offset must be in bounds
6304-
* in order to be able to sanitize access later on.
6305-
*/
6306-
if (!env->bypass_spec_v1) {
6307-
if (dst_reg->type == PTR_TO_MAP_VALUE &&
6308-
check_map_access(env, dst, dst_reg->off, 1, false)) {
6309-
verbose(env, "R%d pointer arithmetic of map value goes out of range, "
6310-
"prohibited for !root\n", dst);
6311-
return -EACCES;
6312-
} else if (dst_reg->type == PTR_TO_STACK &&
6313-
check_stack_access_for_ptr_arithmetic(
6314-
env, dst, dst_reg, dst_reg->off +
6315-
dst_reg->var_off.value)) {
6316-
return -EACCES;
6317-
}
6318-
}
6334+
if (sanitize_check_bounds(env, insn, dst_reg) < 0)
6335+
return -EACCES;
63196336

63206337
return 0;
63216338
}

0 commit comments

Comments
 (0)