• Andrii Nakryiko's avatar
    bpf: fix control-flow graph checking in privileged mode · 10e14e96
    Andrii Nakryiko authored
    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: default avatarAlexei Starovoitov <ast@kernel.org>
    Fixes: 2589726d ("bpf: introduce bounded loops")
    Reported-by: default avatarHao Sun <sunhao.th@gmail.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231110061412.2995786-1-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    10e14e96
calls.c 71.8 KB