]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: consider that tail calls invalidate packet pointers
authorEduard Zingerman <eddyz87@gmail.com>
Tue, 10 Dec 2024 04:10:59 +0000 (20:10 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 10 Dec 2024 18:24:57 +0000 (10:24 -0800)
Tail-called programs could execute any of the helpers that invalidate
packet pointers. Hence, conservatively assume that each tail call
invalidates packet pointers.

Making the change in bpf_helper_changes_pkt_data() automatically makes
use of check_cfg() logic that computes 'changes_pkt_data' effect for
global sub-programs, such that the following program could be
rejected:

    int tail_call(struct __sk_buff *sk)
    {
     bpf_tail_call_static(sk, &jmp_table, 0);
     return 0;
    }

    SEC("tc")
    int not_safe(struct __sk_buff *sk)
    {
     int *p = (void *)(long)sk->data;
     ... make p valid ...
     tail_call(sk);
     *p = 42; /* this is unsafe */
     ...
    }

The tc_bpf2bpf.c:subprog_tc() needs change: mark it as a function that
can invalidate packet pointers. Otherwise, it can't be freplaced with
tailcall_freplace.c:entry_freplace() that does a tail call.

Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20241210041100.1898468-8-eddyz87@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
net/core/filter.c
tools/testing/selftests/bpf/progs/tc_bpf2bpf.c

index efb75eed2e35319670820aabbdf130410eac1a23..21131ec25f24a7f4a6575d1c87c78939f98c24ba 100644 (file)
@@ -7924,6 +7924,8 @@ bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id)
        case BPF_FUNC_xdp_adjust_head:
        case BPF_FUNC_xdp_adjust_meta:
        case BPF_FUNC_xdp_adjust_tail:
+       /* tail-called program could call any of the above */
+       case BPF_FUNC_tail_call:
                return true;
        default:
                return false;
index d1a57f7d09bd805837f6fb14ac8ac4fe449d7821..fe6249d99b315b22809151163d4b582561a1aa56 100644 (file)
@@ -11,6 +11,8 @@ int subprog_tc(struct __sk_buff *skb)
 
        __sink(skb);
        __sink(ret);
+       /* let verifier know that 'subprog_tc' can change pointers to skb->data */
+       bpf_skb_change_proto(skb, 0, 0);
        return ret;
 }