Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce unstable CT lookup helpers #2347

Closed
wants to merge 10 commits into from

Commits on Dec 31, 2021

  1. adding ci files

    Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    a504cac View commit details
    Browse the repository at this point in the history
  2. kernel: Add kallsyms_on_each_symbol variant for single module

    The module_kallsyms_on_each_symbol function iterates over symbols of all
    modules. To implement BTF ID set processing in each module's BTF parsing
    routine, we need a variant that can iterate over a single module's
    symbols. To implement this, extract the single module functionality out
    of module_kallsyms_on_each_symbol, and rename the old function to
    module_kallsyms_on_each_symbol_all.
    
    Then, the new module_kallsyms_on_each_symbol which iterates over a
    single module's symbols uses this extracted helper with appropriate
    locking.
    
    Next commit will make use of it to implement BTF ID set concatentation
    per hook and type.
    
    Also, since we'll be using kallsyms_on_each_symbol for vmlinux BTF
    parsing, remove its dependency on CONFIG_LIVEPATCH.
    
    Cc: Luis Chamberlain <mcgrof@kernel.org>
    Cc: Jessica Yu <jeyu@kernel.org>
    Cc: Josh Poimboeuf <jpoimboe@redhat.com>
    Cc: Jiri Kosina <jikos@kernel.org>
    Cc: Miroslav Benes <mbenes@suse.cz>
    Cc: Petr Mladek <pmladek@suse.com>
    Cc: Joe Lawrence <joe.lawrence@redhat.com>
    Cc: live-patching@vger.kernel.org
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    631cff3 View commit details
    Browse the repository at this point in the history
  3. bpf: Prepare kfunc BTF ID sets when parsing kernel BTF

    This commit prepares the BTF parsing functions for vmlinux and module
    BTFs to find all kfunc BTF ID sets from the vmlinux and module symbols
    and concatentate all sets into single unified set which is sorted and
    keyed by the 'hook' it is meant for, and 'type' of set.
    
    The 'hook' is one of the many program types, e.g. XDP and TC/SCHED_CLS,
    STRUCT_OPS, and 'types' are check (allowed or not), acquire, release,
    and ret_null (with PTR_TO_BTF_ID_OR_NULL return type).
    
    A maximum of BTF_KFUNC_SET_MAX_CNT (32) kfunc BTF IDs are permitted in a
    set of certain hook and type. They are also allocated on demand, and
    otherwise set as NULL.
    
    A new btf_kfunc_id_set_contains function is exposed for use in verifier,
    this new method is faster than the existing list searching method, and
    is also automatic. It also lets other code not care whether the set is
    unallocated or not.
    
    Next commit will update the kernel users to make use of this
    infrastructure.
    
    Finally, add __maybe_unused annotation for BTF ID macros for the
    !CONFIG_DEBUG_INFO_BTF case , so that they don't produce warnings during
    build time.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    
    fixup maybe_unused
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    37e1edd View commit details
    Browse the repository at this point in the history
  4. bpf: Remove check_kfunc_call callback and old kfunc BTF ID API

    Completely remove the old code for check_kfunc_call to help it work
    with modules.
    
    The previous commit finds all symbols in vmlinux or modules while
    parsing and preparing their BTF, and concatenates all related sets
    organized by the hook and the type. Then, they are sorted to enable
    bsearch using btf_id_set_contains.
    
    Also, since we don't need the 'owner' module anywhere when doing
    check_kfunc_call, drop the 'btf_modp' module parameter from
    find_kfunc_desc_btf.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    6356f68 View commit details
    Browse the repository at this point in the history
  5. bpf: Introduce mem, size argument pair support for kfunc

    BPF helpers can associate two adjacent arguments together to pass memory
    of certain size, using ARG_PTR_TO_MEM and ARG_CONST_SIZE arguments.
    Since we don't use bpf_func_proto for kfunc, we need to leverage BTF to
    implement similar support.
    
    The ARG_CONST_SIZE processing for helpers is refactored into a common
    check_mem_size_reg helper that is shared with kfunc as well. kfunc
    ptr_to_mem support follows logic similar to global functions, where
    verification is done as if pointer is not null, even when it may be
    null.
    
    This leads to a simple to follow rule for writing kfunc: always check
    the argument pointer for NULL, except when it is PTR_TO_CTX. Also, the
    PTR_TO_CTX case is also only safe when the helper expecting pointer to
    program ctx is not exposed to other programs where same struct is not
    ctx type. In that case, the type check will fall through to other cases
    and would permit passing other types of pointers, possibly NULL at
    runtime.
    
    Currently, we require the size argument to be suffixed with "__sz" in
    the parameter name. This information is then recorded in kernel BTF and
    verified during function argument checking. In the future we can use BTF
    tagging instead, and modify the kernel function definitions. This will
    be a purely kernel-side change.
    
    This allows us to have some form of backwards compatibility for
    structures that are passed in to the kernel function with their size,
    and allow variable length structures to be passed in if they are
    accompanied by a size parameter.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    32ded3b View commit details
    Browse the repository at this point in the history
  6. bpf: Add reference tracking support to kfunc

    This patch adds verifier support for PTR_TO_BTF_ID return type of kfunc
    to be a reference, by reusing acquire_reference_state/release_reference
    support for existing in-kernel bpf helpers.
    
    We make use of the three kfunc types:
    
    - BTF_KFUNC_TYPE_ACQUIRE
      Return true if kfunc_btf_id is an acquire kfunc.  This will
      acquire_reference_state for the returned PTR_TO_BTF_ID (this is the
      only allow return value). Note that acquire kfunc must always return a
      PTR_TO_BTF_ID{_OR_NULL}, otherwise the program is rejected.
    
    - BTF_KFUNC_TYPE_RELEASE
      Return true if kfunc_btf_id is a release kfunc.  This will release the
      reference to the passed in PTR_TO_BTF_ID which has a reference state
      (from earlier acquire kfunc).
      The btf_check_func_arg_match returns the regno (of argument register,
      hence > 0) if the kfunc is a release kfunc, and a proper referenced
      PTR_TO_BTF_ID is being passed to it.
      This is similar to how helper call check uses bpf_call_arg_meta to
      store the ref_obj_id that is later used to release the reference.
      Similar to in-kernel helper, we only allow passing one referenced
      PTR_TO_BTF_ID as an argument. It can also be passed in to normal
      kfunc, but in case of release kfunc there must always be one
      PTR_TO_BTF_ID argument that is referenced.
    
    - BTF_KFUNC_TYPE_RET_NULL
      For kfunc returning PTR_TO_BTF_ID, tells if it can be NULL, hence
      force caller to mark the pointer not null (using check) before
      accessing it. Note that taking into account the case fixed by commit
      93c230e ("bpf: Enforce id generation for all may-be-null register type")
      we assign a non-zero id for mark_ptr_or_null_reg logic. Later, if more
      return types are supported by kfunc, which have a _OR_NULL variant, it
      might be better to move this id generation under a common
      reg_type_may_be_null check, similar to the case in the commit.
    
    Referenced PTR_TO_BTF_ID is currently only limited to kfunc, but can be
    extended in the future to other BPF helpers as well.  For now, we can
    rely on the btf_struct_ids_match check to ensure we get the pointer to
    the expected struct type. In the future, care needs to be taken to avoid
    ambiguity for reference PTR_TO_BTF_ID passed to release function, in
    case multiple candidates can release same BTF ID.
    
    e.g. there might be two release kfuncs (or kfunc and helper):
    
    foo(struct abc *p);
    bar(struct abc *p);
    
    ... such that both release a PTR_TO_BTF_ID with btf_id of struct abc. In
    this case we would need to track the acquire function corresponding to
    the release function to avoid type confusion, and store this information
    in the register state so that an incorrect program can be rejected. This
    is not a problem right now, hence it is left as an exercise for the
    future patch introducing such a case in the kernel.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    c5ce7cf View commit details
    Browse the repository at this point in the history
  7. net/netfilter: Add unstable CT lookup helpers for XDP and TC-BPF

    This change adds conntrack lookup helpers using the unstable kfunc call
    interface for the XDP and TC-BPF hooks. The primary usecase is
    implementing a synproxy in XDP, see Maxim's patchset [0].
    
    Export get_net_ns_by_id as nf_conntrack_bpf.c needs to call it.
    
    This object is only built when CONFIG_DEBUG_INFO_BTF_MODULES is enabled.
    
      [0]: https://lore.kernel.org/bpf/20211019144655.3483197-1-maximmi@nvidia.com
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    e20eb2c View commit details
    Browse the repository at this point in the history
  8. selftests/bpf: Add test for unstable CT lookup API

    This tests that we return errors as documented, and also that the kfunc
    calls work from both XDP and TC hooks.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    20c8e28 View commit details
    Browse the repository at this point in the history
  9. selftests/bpf: Add test_verifier support to fixup kfunc call insns

    This allows us to add tests (esp. negative tests) where we only want to
    ensure the program doesn't pass through the verifier, and also verify
    the error. The next commit will add the tests making use of this.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    208962e View commit details
    Browse the repository at this point in the history
  10. selftests/bpf: Extend kfunc selftests

    Use the prog_test kfuncs to test the referenced PTR_TO_BTF_ID kfunc
    support, and PTR_TO_CTX, PTR_TO_MEM argument passing support. Also
    testing the various failure cases for invalid kfunc prototypes.
    
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    kkdwivedi authored and Nobody committed Dec 31, 2021
    Configuration menu
    Copy the full SHA
    00d4316 View commit details
    Browse the repository at this point in the history