Skip to content

bpf: copy_verifier_state() should copy 'loop_entry' field

Eduard Zingerman bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest10 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/10bpf: copy_verifier_state() should copy 'loop_entry' fieldkernel/bpf/verifier.c+3 -02 replies
2/10selftests/bpf: test correct loop_entry update in copy_verifier_statetools/testing/selftests/bpf/progs/iters.c+116 -00 replies
3/10bpf: don't do clean_live_states when state->loop_entry->branches > 0kernel/bpf/verifier.c+4 -00 replies
4/10selftests/bpf: check states pruning for deeply nested iteratortools/testing/selftests/bpf/progs/iters.c+23 -00 replies
5/10bpf: detect infinite loop in get_loop_entry()kernel/bpf/verifier.c+21 -180 replies
6/10bpf: make state->dfs_depth < state->loop_entry->dfs_depth an invariantkernel/bpf/verifier.c+3 -30 replies
7/10bpf: do not update state->loop_entry in get_loop_entry()kernel/bpf/verifier.c+2 -110 replies
8/10bpf: use list_head to track explored states and free listinclude/linux/bpf_verifier.h kernel/bpf/verifier.c+42 -410 replies
9/10bpf: free verifier states when they are no longer referencedinclude/linux/bpf_verifier.h kernel/bpf/verifier.c+75 -300 replies
10/10bpf: fix env->peak_states computationinclude/linux/bpf_verifier.h kernel/bpf/verifier.c+15 -20 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next v1 00/10] bpf: copy_verifier_state() should copy 'loop_entry' field

This patch set fixes a bug in copy_verifier_state() where the loop_entry field was not copied. This omission led to incorrect loop_entry fields remaining in env->cur_state, causing incorrect decisions about loop entry assignments in update_loop_entry().

An example of an unsafe program accepted by the verifier due to this bug can be found in patch #2. This bug can also cause an infinite loop in the verifier, see patch #5.

Structure of the patch set:
- Patch #1 fixes the bug but has a significant negative impact on verification performance for sched_ext programs.
- Patch #3 mitigates the verification performance impact of patch #1 by avoiding clean_live_states() for states whose loop_entry is still being verified. This reduces the number of processed instructions for sched_ext programs by 28–92% in some cases.
- Patches #5-6 simplify {get,update}_loop_entry() logic (and are not strictly necessary).
- Patches #7–10 mitigate the memory overhead introduced by patch #1 when a program with iterator-based loop hits the 1M instruction limit. This is achieved by freeing states in env->free_list when their branches and used_as_loop_entry counts reach zero.

Note: for env->peak_states computation in patch #10,

      I think this should also include env->stack_size.

Patches #1-4 were previously sent as a part of [1].

[1] https://lore.kernel.org/bpf/[email protected]/

Eduard Zingerman (10):
bpf: copy_verifier_state() should copy 'loop_entry' field selftests/bpf: test correct loop_entry update in copy_verifier_state
bpf: don't do clean_live_states when state->loop_entry->branches > 0 selftests/bpf: check states pruning for deeply nested iterator
bpf: detect infinite loop in get_loop_entry()
bpf: make state->dfs_depth < state->loop_entry->dfs_depth an invariant
bpf: do not update state->loop_entry in get_loop_entry()
bpf: use list_head to track explored states and free list
bpf: free verifier states when they are no longer referenced
bpf: fix env->peak_states computation

 include/linux/bpf_verifier.h              |  25 ++-
 kernel/bpf/verifier.c                     | 229 +++++++++++++---------
 tools/testing/selftests/bpf/progs/iters.c | 139 +++++++++++++
 3 files changed, 296 insertions(+), 97 deletions(-)
Signature · 1 line
2.48.1