Commit 529409ea authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

bpf: propagate precision across all frames, not just the last one

When equivalent completed state is found and it has additional precision
restrictions, BPF verifier propagates precision to
currently-being-verified state chain (i.e., including parent states) so
that if some of the states in the chain are not yet completed, necessary
precision restrictions are enforced.

Unfortunately, right now this happens only for the last frame (deepest
active subprogram's frame), not all the frames. This can lead to
incorrect matching of states due to missing precision marker. Currently
this doesn't seem possible as BPF verifier forces everything to precise
when validated BPF program has any subprograms. But with the next patch
lifting this restriction, this becomes problematic.

In fact, without this fix, we'll start getting failure in one of the
existing test_verifier test cases:

  #906/p precise: cross frame pruning FAIL
  Unexpected success to load!
  verification time 48 usec
  stack depth 0+0
  processed 26 insns (limit 1000000) max_states_per_insn 3 total_states 17 peak_states 17 mark_read 8

This patch adds precision propagation across all frames.

Fixes: a3ce685d ("bpf: fix precision tracking")
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20221104163649.121784-3-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent a3b666bf
...@@ -2756,7 +2756,7 @@ static void mark_all_scalars_precise(struct bpf_verifier_env *env, ...@@ -2756,7 +2756,7 @@ static void mark_all_scalars_precise(struct bpf_verifier_env *env,
} }
} }
static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, static int __mark_chain_precision(struct bpf_verifier_env *env, int frame, int regno,
int spi) int spi)
{ {
struct bpf_verifier_state *st = env->cur_state; struct bpf_verifier_state *st = env->cur_state;
...@@ -2773,7 +2773,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, ...@@ -2773,7 +2773,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
if (!env->bpf_capable) if (!env->bpf_capable)
return 0; return 0;
func = st->frame[st->curframe]; func = st->frame[frame];
if (regno >= 0) { if (regno >= 0) {
reg = &func->regs[regno]; reg = &func->regs[regno];
if (reg->type != SCALAR_VALUE) { if (reg->type != SCALAR_VALUE) {
...@@ -2854,7 +2854,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, ...@@ -2854,7 +2854,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
break; break;
new_marks = false; new_marks = false;
func = st->frame[st->curframe]; func = st->frame[frame];
bitmap_from_u64(mask, reg_mask); bitmap_from_u64(mask, reg_mask);
for_each_set_bit(i, mask, 32) { for_each_set_bit(i, mask, 32) {
reg = &func->regs[i]; reg = &func->regs[i];
...@@ -2920,12 +2920,17 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, ...@@ -2920,12 +2920,17 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno,
int mark_chain_precision(struct bpf_verifier_env *env, int regno) int mark_chain_precision(struct bpf_verifier_env *env, int regno)
{ {
return __mark_chain_precision(env, regno, -1); return __mark_chain_precision(env, env->cur_state->curframe, regno, -1);
} }
static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) static int mark_chain_precision_frame(struct bpf_verifier_env *env, int frame, int regno)
{ {
return __mark_chain_precision(env, -1, spi); return __mark_chain_precision(env, frame, regno, -1);
}
static int mark_chain_precision_stack_frame(struct bpf_verifier_env *env, int frame, int spi)
{
return __mark_chain_precision(env, frame, -1, spi);
} }
static bool is_spillable_regtype(enum bpf_reg_type type) static bool is_spillable_regtype(enum bpf_reg_type type)
...@@ -11794,17 +11799,18 @@ static int propagate_precision(struct bpf_verifier_env *env, ...@@ -11794,17 +11799,18 @@ static int propagate_precision(struct bpf_verifier_env *env,
{ {
struct bpf_reg_state *state_reg; struct bpf_reg_state *state_reg;
struct bpf_func_state *state; struct bpf_func_state *state;
int i, err = 0; int i, err = 0, fr;
state = old->frame[old->curframe]; for (fr = old->curframe; fr >= 0; fr--) {
state = old->frame[fr];
state_reg = state->regs; state_reg = state->regs;
for (i = 0; i < BPF_REG_FP; i++, state_reg++) { for (i = 0; i < BPF_REG_FP; i++, state_reg++) {
if (state_reg->type != SCALAR_VALUE || if (state_reg->type != SCALAR_VALUE ||
!state_reg->precise) !state_reg->precise)
continue; continue;
if (env->log.level & BPF_LOG_LEVEL2) if (env->log.level & BPF_LOG_LEVEL2)
verbose(env, "propagating r%d\n", i); verbose(env, "frame %d: propagating r%d\n", i, fr);
err = mark_chain_precision(env, i); err = mark_chain_precision_frame(env, fr, i);
if (err < 0) if (err < 0)
return err; return err;
} }
...@@ -11817,12 +11823,13 @@ static int propagate_precision(struct bpf_verifier_env *env, ...@@ -11817,12 +11823,13 @@ static int propagate_precision(struct bpf_verifier_env *env,
!state_reg->precise) !state_reg->precise)
continue; continue;
if (env->log.level & BPF_LOG_LEVEL2) if (env->log.level & BPF_LOG_LEVEL2)
verbose(env, "propagating fp%d\n", verbose(env, "frame %d: propagating fp%d\n",
(-i - 1) * BPF_REG_SIZE); (-i - 1) * BPF_REG_SIZE, fr);
err = mark_chain_precision_stack(env, i); err = mark_chain_precision_stack_frame(env, fr, i);
if (err < 0) if (err < 0)
return err; return err;
} }
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment