linux-stable/tools/testing/selftests/bpf/verifier
Andrii Nakryiko 10e14e9652 bpf: fix control-flow graph checking in privileged mode
When BPF program is verified in privileged mode, BPF verifier allows
bounded loops. This means that from CFG point of view there are
definitely some back-edges. Original commit adjusted check_cfg() logic
to not detect back-edges in control flow graph if they are resulting
from conditional jumps, which the idea that subsequent full BPF
verification process will determine whether such loops are bounded or
not, and either accept or reject the BPF program. At least that's my
reading of the intent.

Unfortunately, the implementation of this idea doesn't work correctly in
all possible situations. Conditional jump might not result in immediate
back-edge, but just a few unconditional instructions later we can arrive
at back-edge. In such situations check_cfg() would reject BPF program
even in privileged mode, despite it might be bounded loop. Next patch
adds one simple program demonstrating such scenario.

To keep things simple, instead of trying to detect back edges in
privileged mode, just assume every back edge is valid and let subsequent
BPF verification prove or reject bounded loops.

Note a few test changes. For unknown reason, we have a few tests that
are specified to detect a back-edge in a privileged mode, but looking at
their code it seems like the right outcome is passing check_cfg() and
letting subsequent verification to make a decision about bounded or not
bounded looping.

Bounded recursion case is also interesting. The example should pass, as
recursion is limited to just a few levels and so we never reach maximum
number of nested frames and never exhaust maximum stack depth. But the
way that max stack depth logic works today it falsely detects this as
exceeding max nested frame count. This patch series doesn't attempt to
fix this orthogonal problem, so we just adjust expected verifier failure.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Fixes: 2589726d12 ("bpf: introduce bounded loops")
Reported-by: Hao Sun <sunhao.th@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20231110061412.2995786-1-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2023-11-09 22:57:24 -08:00
..
.gitignore
atomic_and.c
atomic_bounds.c
atomic_cmpxchg.c selftests/bpf: Add F_NEEDS_EFFICIENT_UNALIGNED_ACCESS to some tests 2023-07-05 14:34:23 +02:00
atomic_fetch.c
atomic_fetch_add.c
atomic_invalid.c
atomic_or.c
atomic_xchg.c
atomic_xor.c
basic.c
basic_call.c
basic_instr.c selftests/bpf: Fix a test_verifier failure 2023-07-27 18:54:16 -07:00
basic_stx_ldx.c
bpf_loop_inline.c selftests/bpf: Fix test_verifier failed test in unprivileged mode 2022-07-21 21:03:25 -07:00
bpf_st_mem.c selftests/bpf: Add test for immediate spilled to stack 2023-11-01 22:30:27 -07:00
calls.c bpf: fix control-flow graph checking in privileged mode 2023-11-09 22:57:24 -08:00
ctx_sk_lookup.c
ctx_skb.c selftests/bpf: Add F_NEEDS_EFFICIENT_UNALIGNED_ACCESS to some tests 2023-07-05 14:34:23 +02:00
dead_code.c
direct_value_access.c
event_output.c
jit.c bpf: add selftests for lsh, rsh, arsh with reg operand 2022-10-19 16:53:51 -07:00
jmp32.c selftests/bpf: Add F_NEEDS_EFFICIENT_UNALIGNED_ACCESS to some tests 2023-07-05 14:34:23 +02:00
jset.c
jump.c bpf, selftests: Add verifier test case for imm=0,umin=0,umax=1 scalar 2022-07-01 12:56:27 -07:00
junk_insn.c
ld_abs.c
ld_dw.c
ld_imm64.c bpf: handle ldimm64 properly in check_cfg() 2023-11-09 20:11:20 -08:00
map_kptr.c selftests/bpf: Add F_NEEDS_EFFICIENT_UNALIGNED_ACCESS to some tests 2023-07-05 14:34:23 +02:00
perf_event_sample_period.c
precise.c selftests/bpf: Add F_NEEDS_EFFICIENT_UNALIGNED_ACCESS to some tests 2023-07-05 14:34:23 +02:00
scale.c
sleepable.c bpf: Allow BPF_PROG_TYPE_STRUCT_OPS programs to be sleepable 2023-01-25 10:25:57 -08:00
wide_access.c