bpf: fix mark_all_scalars_precise use in mark_chain_precision
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 5 May 2023 04:33:14 +0000 (21:33 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 5 May 2023 05:35:35 +0000 (22:35 -0700)
commitc50c0b57a515826b5d2e1ce85cd85f24f0da10c2
tree4a381a53814e9cf96470affc3b3e3933f0490c28
parentf655badf2a8fc028433d9583bf86a6b473721f09
bpf: fix mark_all_scalars_precise use in mark_chain_precision

When precision backtracking bails out due to some unsupported sequence
of instructions (e.g., stack access through register other than r10), we
need to mark all SCALAR registers as precise to be safe. Currently,
though, we mark SCALARs precise only starting from the state we detected
unsupported condition, which could be one of the parent states of the
actual current state. This will leave some registers potentially not
marked as precise, even though they should. So make sure we start
marking scalars as precise from current state (env->cur_state).

Further, we don't currently detect a situation when we end up with some
stack slots marked as needing precision, but we ran out of available
states to find the instructions that populate those stack slots. This is
akin the `i >= func->allocated_stack / BPF_REG_SIZE` check and should be
handled similarly by falling back to marking all SCALARs precise. Add
this check when we run out of states.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230505043317.3629845-8-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/verifier.c
tools/testing/selftests/bpf/verifier/precise.c