• Kumar Kartikeya Dwivedi's avatar
    bpf: Teach verifier about kptr_get kfunc helpers · a1ef1959
    Kumar Kartikeya Dwivedi authored
    We introduce a new style of kfunc helpers, namely *_kptr_get, where they
    take pointer to the map value which points to a referenced kernel
    pointer contained in the map. Since this is referenced, only
    bpf_kptr_xchg from BPF side and xchg from kernel side is allowed to
    change the current value, and each pointer that resides in that location
    would be referenced, and RCU protected (this must be kept in mind while
    adding kernel types embeddable as reference kptr in BPF maps).
    
    This means that if do the load of the pointer value in an RCU read
    section, and find a live pointer, then as long as we hold RCU read lock,
    it won't be freed by a parallel xchg + release operation. This allows us
    to implement a safe refcount increment scheme. Hence, enforce that first
    argument of all such kfunc is a proper PTR_TO_MAP_VALUE pointing at the
    right offset to referenced pointer.
    
    For the rest of the arguments, they are subjected to typical kfunc
    argument checks, hence allowing some flexibility in passing more intent
    into how the reference should be taken.
    
    For instance, in case of struct nf_conn, it is not freed until RCU grace
    period ends, but can still be reused for another tuple once refcount has
    dropped to zero. Hence, a bpf_ct_kptr_get helper not only needs to call
    refcount_inc_not_zero, but also do a tuple match after incrementing the
    reference, and when it fails to match it, put the reference again and
    return NULL.
    
    This can be implemented easily if we allow passing additional parameters
    to the bpf_ct_kptr_get kfunc, like a struct bpf_sock_tuple * and a
    tuple__sz pair.
    Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20220424214901.2743946-9-memxor@gmail.com
    a1ef1959
btf.c 200 KB