Skip to content

Commit e042aa5

Browse files
committed
bpf: Fix pointer arithmetic mask tightening under state pruning
In 7fedb63 ("bpf: Tighten speculative pointer arithmetic mask") we narrowed the offset mask for unprivileged pointer arithmetic in order to mitigate a corner case where in the speculative domain it is possible to advance, for example, the map value pointer by up to value_size-1 out-of- bounds in order to leak kernel memory via side-channel to user space. The verifier's state pruning for scalars leaves one corner case open where in the first verification path R_x holds an unknown scalar with an aux->alu_limit of e.g. 7, and in a second verification path that same register R_x, here denoted as R_x', holds an unknown scalar which has tighter bounds and would thus satisfy range_within(R_x, R_x') as well as tnum_in(R_x, R_x') for state pruning, yielding an aux->alu_limit of 3: Given the second path fits the register constraints for pruning, the final generated mask from aux->alu_limit will remain at 7. While technically not wrong for the non-speculative domain, it would however be possible to craft similar cases where the mask would be too wide as in 7fedb63. One way to fix it is to detect the presence of unknown scalar map pointer arithmetic and force a deeper search on unknown scalars to ensure that we do not run into a masking mismatch. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]>
1 parent 59089a1 commit e042aa5

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ struct bpf_verifier_env {
414414
u32 used_map_cnt; /* number of used maps */
415415
u32 used_btf_cnt; /* number of used BTF objects */
416416
u32 id_gen; /* used to generate unique reg IDs */
417+
bool explore_alu_limits;
417418
bool allow_ptr_leaks;
418419
bool allow_uninit_stack;
419420
bool allow_ptr_to_map_access;

kernel/bpf/verifier.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6561,6 +6561,12 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
65616561
alu_state |= off_is_imm ? BPF_ALU_IMMEDIATE : 0;
65626562
alu_state |= ptr_is_dst_reg ?
65636563
BPF_ALU_SANITIZE_SRC : BPF_ALU_SANITIZE_DST;
6564+
6565+
/* Limit pruning on unknown scalars to enable deep search for
6566+
* potential masking differences from other program paths.
6567+
*/
6568+
if (!off_is_imm)
6569+
env->explore_alu_limits = true;
65646570
}
65656571

65666572
err = update_alu_sanitation_state(aux, alu_state, alu_limit);
@@ -9936,8 +9942,8 @@ static void clean_live_states(struct bpf_verifier_env *env, int insn,
99369942
}
99379943

99389944
/* Returns true if (rold safe implies rcur safe) */
9939-
static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
9940-
struct bpf_id_pair *idmap)
9945+
static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
9946+
struct bpf_reg_state *rcur, struct bpf_id_pair *idmap)
99419947
{
99429948
bool equal;
99439949

@@ -9963,6 +9969,8 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
99639969
return false;
99649970
switch (rold->type) {
99659971
case SCALAR_VALUE:
9972+
if (env->explore_alu_limits)
9973+
return false;
99669974
if (rcur->type == SCALAR_VALUE) {
99679975
if (!rold->precise && !rcur->precise)
99689976
return true;
@@ -10053,9 +10061,8 @@ static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur,
1005310061
return false;
1005410062
}
1005510063

10056-
static bool stacksafe(struct bpf_func_state *old,
10057-
struct bpf_func_state *cur,
10058-
struct bpf_id_pair *idmap)
10064+
static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
10065+
struct bpf_func_state *cur, struct bpf_id_pair *idmap)
1005910066
{
1006010067
int i, spi;
1006110068

@@ -10100,9 +10107,8 @@ static bool stacksafe(struct bpf_func_state *old,
1010010107
continue;
1010110108
if (old->stack[spi].slot_type[0] != STACK_SPILL)
1010210109
continue;
10103-
if (!regsafe(&old->stack[spi].spilled_ptr,
10104-
&cur->stack[spi].spilled_ptr,
10105-
idmap))
10110+
if (!regsafe(env, &old->stack[spi].spilled_ptr,
10111+
&cur->stack[spi].spilled_ptr, idmap))
1010610112
/* when explored and current stack slot are both storing
1010710113
* spilled registers, check that stored pointers types
1010810114
* are the same as well.
@@ -10159,10 +10165,11 @@ static bool func_states_equal(struct bpf_verifier_env *env, struct bpf_func_stat
1015910165

1016010166
memset(env->idmap_scratch, 0, sizeof(env->idmap_scratch));
1016110167
for (i = 0; i < MAX_BPF_REG; i++)
10162-
if (!regsafe(&old->regs[i], &cur->regs[i], env->idmap_scratch))
10168+
if (!regsafe(env, &old->regs[i], &cur->regs[i],
10169+
env->idmap_scratch))
1016310170
return false;
1016410171

10165-
if (!stacksafe(old, cur, env->idmap_scratch))
10172+
if (!stacksafe(env, old, cur, env->idmap_scratch))
1016610173
return false;
1016710174

1016810175
if (!refsafe(old, cur))

0 commit comments

Comments
 (0)