Skip to content
  1. Mar 06, 2022
    • Kumar Kartikeya Dwivedi's avatar
      selftests/bpf: Add tests for kfunc register offset checks · 8218ccb5
      Kumar Kartikeya Dwivedi authored
      
      
      Include a few verifier selftests that test against the problems being
      fixed by previous commits, i.e. release kfunc always require
      PTR_TO_BTF_ID fixed and var_off to be 0, and negative offset is not
      permitted and returns a helpful error message.
      
      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/20220304224645.3677453-9-memxor@gmail.com
      8218ccb5
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Replace __diag_ignore with unified __diag_ignore_all · 0b206c6d
      Kumar Kartikeya Dwivedi authored
      
      
      Currently, -Wmissing-prototypes warning is ignored for GCC, but not
      clang. This leads to clang build warning in W=1 mode. Since the flag
      used by both compilers is same, we can use the unified __diag_ignore_all
      macro that works for all supported versions and compilers which have
      __diag macro support (currently GCC >= 8.0, and Clang >= 11.0).
      
      Also add nf_conntrack_bpf.h include to prevent missing prototype warning
      for register_nf_conntrack_bpf.
      
      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/20220304224645.3677453-8-memxor@gmail.com
      0b206c6d
    • Kumar Kartikeya Dwivedi's avatar
      compiler_types.h: Add unified __diag_ignore_all for GCC/LLVM · 4d1ea705
      Kumar Kartikeya Dwivedi authored
      
      
      Add a __diag_ignore_all macro, to ignore warnings for both GCC and LLVM,
      without having to specify the compiler type and version. By default, GCC
      8 and clang 11 are used. This will be used by bpf subsystem to ignore
      -Wmissing-prototypes warning for functions that are meant to be global
      functions so that they are in vmlinux BTF, but don't have a prototype.
      
      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/20220304224645.3677453-7-memxor@gmail.com
      4d1ea705
    • Nathan Chancellor's avatar
      compiler-clang.h: Add __diag infrastructure for clang · f014a00b
      Nathan Chancellor authored
      
      
      Add __diag macros similar to those in compiler-gcc.h, so that warnings
      that need to be adjusted for specific cases but not globally can be
      ignored when building with clang.
      
      Signed-off-by: default avatarNathan Chancellor <nathan@kernel.org>
      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/20220304224645.3677453-6-memxor@gmail.com
      
      [ Kartikeya: wrote commit message ]
      f014a00b
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Harden register offset checks for release helpers and kfuncs · 24d5bb80
      Kumar Kartikeya Dwivedi authored
      
      
      Let's ensure that the PTR_TO_BTF_ID reg being passed in to release BPF
      helpers and kfuncs always has its offset set to 0. While not a real
      problem now, there's a very real possibility this will become a problem
      when more and more kfuncs are exposed, and more BPF helpers are added
      which can release PTR_TO_BTF_ID.
      
      Previous commits already protected against non-zero var_off. One of the
      case we are concerned about now is when we have a type that can be
      returned by e.g. an acquire kfunc:
      
      struct foo {
      	int a;
      	int b;
      	struct bar b;
      };
      
      ... and struct bar is also a type that can be returned by another
      acquire kfunc.
      
      Then, doing the following sequence:
      
      	struct foo *f = bpf_get_foo(); // acquire kfunc
      	if (!f)
      		return 0;
      	bpf_put_bar(&f->b); // release kfunc
      
      ... would work with the current code, since the btf_struct_ids_match
      takes reg->off into account for matching pointer type with release kfunc
      argument type, but would obviously be incorrect, and most likely lead to
      a kernel crash. A test has been included later to prevent regressions in
      this area.
      
      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/20220304224645.3677453-5-memxor@gmail.com
      24d5bb80
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Disallow negative offset in check_ptr_off_reg · e1fad0ff
      Kumar Kartikeya Dwivedi authored
      
      
      check_ptr_off_reg only allows fixed offset to be set for PTR_TO_BTF_ID,
      where reg->off < 0 doesn't make sense. This would shift the pointer
      backwards, and fails later in btf_struct_ids_match or btf_struct_walk
      due to out of bounds access (since offset is interpreted as unsigned).
      
      Improve the verifier by rejecting this case by using a better error
      message for BPF helpers and kfunc, by putting a check inside the
      check_func_arg_reg_off function.
      
      Also, update existing verifier selftests to work with new error string.
      
      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/20220304224645.3677453-4-memxor@gmail.com
      e1fad0ff
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Fix PTR_TO_BTF_ID var_off check · 655efe50
      Kumar Kartikeya Dwivedi authored
      When kfunc support was added, check_ctx_reg was called for PTR_TO_CTX
      register, but no offset checks were made for PTR_TO_BTF_ID. Only
      reg->off was taken into account by btf_struct_ids_match, which protected
      against type mismatch due to non-zero reg->off, but when reg->off was
      zero, a user could set the variable offset of the register and allow it
      to be passed to kfunc, leading to bad pointer being passed into the
      kernel.
      
      Fix this by reusing the extracted helper check_func_arg_reg_off from
      previous commit, and make one call before checking all supported
      register types. Since the list is maintained, any future changes will be
      taken into account by updating check_func_arg_reg_off. This function
      prevents non-zero var_off to be set for PTR_TO_BTF_ID, but still allows
      a fixed non-zero reg->off, which is needed for type matching to work
      correctly when using pointer arithmetic.
      
      ARG_DONTCARE is passed as arg_type, since kfunc doesn't support
      accepting a ARG_PTR_TO_ALLOC_MEM without relying on size of parameter
      type from BTF (in case of pointer), or using a mem, len pair. The
      forcing of offset check for ARG_PTR_TO_ALLOC_MEM is done because ringbuf
      helpers obtain the size from the header located at the beginning of the
      memory region, hence any changes to the original pointer shouldn't be
      allowed. In case of kfunc, size is always known, either at verification
      time, or using the length parameter, hence this forcing is not required.
      
      Since this check will happen once already for PTR_TO_CTX, remove the
      check_ptr_off_reg call inside its block.
      
      Fixes: e6ac2450
      
       ("bpf: Support bpf program calling kernel function")
      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/20220304224645.3677453-3-memxor@gmail.com
      655efe50
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Add check_func_arg_reg_off function · 25b35dd2
      Kumar Kartikeya Dwivedi authored
      
      
      Lift the list of register types allowed for having fixed and variable
      offsets when passed as helper function arguments into a common helper,
      so that they can be reused for kfunc checks in later commits. Keeping a
      common helper aids maintainability and allows us to follow the same
      consistent rules across helpers and kfuncs. Also, convert check_func_arg
      to use this function.
      
      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/20220304224645.3677453-2-memxor@gmail.com
      25b35dd2
    • Alexei Starovoitov's avatar
      Merge branch 'libbpf: support custom SEC() handlers' · caec5495
      Alexei Starovoitov authored
      
      
      Andrii Nakryiko says:
      
      ====================
      
      Add ability for user applications and libraries to register custom BPF program
      SEC() handlers. See patch #2 for examples where this is useful.
      
      Patch #1 does some preliminary refactoring to allow exponsing program
      init, preload, and attach callbacks as public API. It also establishes
      a protocol to allow optional auto-attach behavior. This will also help the
      case of sometimes auto-attachable uprobes.
      
      v4->v5:
        - API documentation improvements (Daniel);
      v3->v4:
        - init_fn -> prog_setup_fn, preload_fn -> prog_prepare_load_fn (Alexei);
      v2->v3:
        - moved callbacks and cookie into OPTS struct (Alan);
        - added more test scenarios (Alan);
        - address most of Alan's feedback, but kept API name;
      v1->v2:
        - resubmitting due to git send-email screw up.
      
      Cc: Alan Maguire <alan.maguire@oracle.com>
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      caec5495
    • Andrii Nakryiko's avatar
      selftests/bpf: Add custom SEC() handling selftest · aa963bcb
      Andrii Nakryiko authored
      
      
      Add a selftest validating various aspects of libbpf's handling of custom
      SEC() handlers. It also demonstrates how libraries can ensure very early
      callbacks registration and unregistration using
      __attribute__((constructor))/__attribute__((destructor)) functions.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-4-andrii@kernel.org
      aa963bcb
    • Andrii Nakryiko's avatar
      libbpf: Support custom SEC() handlers · 697f104d
      Andrii Nakryiko authored
      
      
      Allow registering and unregistering custom handlers for BPF program.
      This allows user applications and libraries to plug into libbpf's
      declarative SEC() definition handling logic. This allows to offload
      complex and intricate custom logic into external libraries, but still
      provide a great user experience.
      
      One such example is USDT handling library, which has a lot of code and
      complexity which doesn't make sense to put into libbpf directly, but it
      would be really great for users to be able to specify BPF programs with
      something like SEC("usdt/<path-to-binary>:<usdt_provider>:<usdt_name>")
      and have correct BPF program type set (BPF_PROGRAM_TYPE_KPROBE, as it is
      uprobe) and even support BPF skeleton's auto-attach logic.
      
      In some cases, it might be even good idea to override libbpf's default
      handling, like for SEC("perf_event") programs. With custom library, it's
      possible to extend logic to support specifying perf event specification
      right there in SEC() definition without burdening libbpf with lots of
      custom logic or extra library dependecies (e.g., libpfm4). With current
      patch it's possible to override libbpf's SEC("perf_event") handling and
      specify a completely custom ones.
      
      Further, it's possible to specify a generic fallback handling for any
      SEC() that doesn't match any other custom or standard libbpf handlers.
      This allows to accommodate whatever legacy use cases there might be, if
      necessary.
      
      See doc comments for libbpf_register_prog_handler() and
      libbpf_unregister_prog_handler() for detailed semantics.
      
      This patch also bumps libbpf development version to v0.8 and adds new
      APIs there.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-3-andrii@kernel.org
      697f104d
    • Andrii Nakryiko's avatar
      libbpf: Allow BPF program auto-attach handlers to bail out · 4fa5bcfe
      Andrii Nakryiko authored
      
      
      Allow some BPF program types to support auto-attach only in subste of
      cases. Currently, if some BPF program type specifies attach callback, it
      is assumed that during skeleton attach operation all such programs
      either successfully attach or entire skeleton attachment fails. If some
      program doesn't support auto-attachment from skeleton, such BPF program
      types shouldn't have attach callback specified.
      
      This is limiting for cases when, depending on how full the SEC("")
      definition is, there could either be enough details to support
      auto-attach or there might not be and user has to use some specific API
      to provide more details at runtime.
      
      One specific example of such desired behavior might be SEC("uprobe"). If
      it's specified as just uprobe auto-attach isn't possible. But if it's
      SEC("uprobe/<some_binary>:<some_func>") then there are enough details to
      support auto-attach. Note that there is a somewhat subtle difference
      between auto-attach behavior of BPF skeleton and using "generic"
      bpf_program__attach(prog) (which uses the same attach handlers under the
      cover). Skeleton allow some programs within bpf_object to not have
      auto-attach implemented and doesn't treat that as an error. Instead such
      BPF programs are just skipped during skeleton's (optional) attach step.
      bpf_program__attach(), on the other hand, is called when user *expects*
      auto-attach to work, so if specified program doesn't implement or
      doesn't support auto-attach functionality, that will be treated as an
      error.
      
      Another improvement to the way libbpf is handling SEC()s would be to not
      require providing dummy kernel function name for kprobe. Currently,
      SEC("kprobe/whatever") is necessary even if actual kernel function is
      determined by user at runtime and bpf_program__attach_kprobe() is used
      to specify it. With changes in this patch, it's possible to support both
      SEC("kprobe") and SEC("kprobe/<actual_kernel_function"), while only in
      the latter case auto-attach will be performed. In the former one, such
      kprobe will be skipped during skeleton attach operation.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Tested-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Reviewed-by: default avatarAlan Maguire <alan.maguire@oracle.com>
      Link: https://lore.kernel.org/bpf/20220305010129.1549719-2-andrii@kernel.org
      4fa5bcfe
  2. Mar 05, 2022