Skip to content
  1. Dec 19, 2021
    • Kumar Kartikeya Dwivedi's avatar
      bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support · 3363bd0c
      Kumar Kartikeya Dwivedi authored
      
      
      Allow passing PTR_TO_CTX, if the kfunc expects a matching struct type,
      and punt to PTR_TO_MEM block if reg->type does not fall in one of
      PTR_TO_BTF_ID or PTR_TO_SOCK* types. This will be used by future commits
      to get access to XDP and TC PTR_TO_CTX, and pass various data (flags,
      l4proto, netns_id, etc.) encoded in opts struct passed as pointer to
      kfunc.
      
      For PTR_TO_MEM support, arguments are currently limited to pointer to
      scalar, or pointer to struct composed of scalars. This is done so that
      unsafe scenarios (like passing PTR_TO_MEM where PTR_TO_BTF_ID of
      in-kernel valid structure is expected, which may have pointers) are
      avoided. Since the argument checking happens basd on argument register
      type, it is not easy to ascertain what the expected type is. In the
      future, support for PTR_TO_MEM for kfunc can be extended to serve other
      usecases. The struct type whose pointer is passed in may have maximum
      nesting depth of 4, all recursively composed of scalars or struct with
      scalars.
      
      Future commits will add negative tests that check whether these
      restrictions imposed for kfunc arguments are duly rejected by BPF
      verifier or not.
      
      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/20211217015031.1278167-4-memxor@gmail.com
      3363bd0c
    • Alexei Starovoitov's avatar
      Merge branch 'Introduce composable bpf types' · 7f16d2aa
      Alexei Starovoitov authored
      Hao Luo says:
      
      ====================
      This patch set consists of two changes:
      
       - a cleanup of arg_type, ret_type and reg_type which try to make those
         types composable. (patch 1/9 - patch 6/9)
       - a bug fix that prevents bpf programs from writing kernel memory.
         (patch 7/9 - patch 9/9)
      
      The purpose of the cleanup is to find a scalable way to express type
      nullness and read-onliness. This patchset introduces two flags that
      can be applied on all three types: PTR_MAYBE_NULL and MEM_RDONLY.
      Previous types such as ARG_XXX_OR_NULL can now be written as
      
       ARG_XXX | PTR_MAYBE_NULL
      
      Similarly, PTR_TO_RDONLY_BUF is now "PTR_TO_BUF | MEM_RDONLY".
      
      Flags can be composed, as ARGs can be both MEM_RDONLY and MAYBE_NULL.
      
       ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY
      
      Based on this new composable types, patch 7/9 applies MEM_RDONLY on
      PTR_TO_MEM, in order to tag the returned memory from per_cpu_ptr as
      read-only. Therefore fixing a previous bug that one can leverage
      per_cpu_ptr to modify kernel memory within BPF programs.
      
      Patch 8/9 generalizes the use of MEM_RDONLY further by tagging a set of
      helper arguments ARG_PTR_TO_MEM with MEM_RDONLY. Some helper functions
      may override their arguments, such as bpf_d_path, bpf_snprintf. In this
      patch, we narrow the ARG_PTR_TO_MEM to be compatible with only a subset
      of memory types. This prevents these helpers from writing read-only
      memories. For the helpers that do not write its arguments, we add tag
      MEM_RDONLY to allow taking a RDONLY memory as argument.
      
      Changes since v1:
       - use %u to print base_type(type) instead of %lu. (Andrii, patch 3/9)
       - improve reg_type_str() by appending '_or_null' and prepending 'rdonly_'.
         use preallocated buffer in 'bpf_env'.
       - unified handling of the previous XXX_OR_NULL in adjust_ptr_min_max_vals
         (Andrii, patch 4/9)
       - move PTR_TO_MAP_KEY up to PTR_TO_MAP_VALUE so that we don't have
         to change to drivers that assume the numeric values of bpf_reg.
         (patch 4/9)
       - reintroduce the typo from previous commits in fixes tags (Andrii, patch 7/9)
       - extensive comments on the reason behind folding flags in
         check_reg_type (Andrii, patch 8/9)
      
      Changes since RFC v2:
       - renamed BPF_BASE_TYPE to a more succinct name base_type and move its
         definition to bpf_verifier.h. Same for BPF_TYPE_FLAG. (Alexei)
       - made checking MEM_RDONLY in check_reg_type() universal (Alexei)
       - ran through majority of test_progs and fixed bugs in RFC v2:
         - fixed incorrect BPF_BASE_TYPE_MASK. The high bit of GENMASK should
           be BITS - 1, rather than BITS. patch 1/9.
         - fixed incorrect conditions when checking ARG_PTR_TO_MAP_VALUE in
           check_func_arg(). See patch 2/9.
         - fixed a bug where PTR_TO_BTF_ID may be combined with MEM_RDONLY,
           causing the check in check_mem_access() to fall through to the
           'else' branch. See check_helper_call() in patch 7/9.
       - fixed build failure on netronome driver. Entries in bpf_reg_type have
         been ordered. patch 4/9.
       - fixed build warnings of using '%d' to print base_type. patch 4/9
       - unify arg_type_may_be_null() and reg_type_may_be_null() into a single
         type_may_be_null().
      
      Previous versions:
      
       v1:
         https://lwn.net/Articles/877938/
      
       RFC v2:
         https://lwn.net/Articles/877171/
      
       RFC v1:
         https://lore.kernel.org/bpf/20211109003052.3499225-1-haoluo@google.com/T/
         https://lore.kernel.org/bpf/20211109021624.1140446-8-haoluo@google.com/T/
      
      
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      7f16d2aa
    • Hao Luo's avatar
      bpf/selftests: Test PTR_TO_RDONLY_MEM · 9497c458
      Hao Luo authored
      
      
      This test verifies that a ksym of non-struct can not be directly
      updated.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-10-haoluo@google.com
      9497c458
    • Hao Luo's avatar
      bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem. · 216e3cd2
      Hao Luo authored
      
      
      Some helper functions may modify its arguments, for example,
      bpf_d_path, bpf_get_stack etc. Previously, their argument types
      were marked as ARG_PTR_TO_MEM, which is compatible with read-only
      mem types, such as PTR_TO_RDONLY_BUF. Therefore it's legitimate,
      but technically incorrect, to modify a read-only memory by passing
      it into one of such helper functions.
      
      This patch tags the bpf_args compatible with immutable memory with
      MEM_RDONLY flag. The arguments that don't have this flag will be
      only compatible with mutable memory types, preventing the helper
      from modifying a read-only memory. The bpf_args that have
      MEM_RDONLY are compatible with both mutable memory and immutable
      memory.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-9-haoluo@google.com
      216e3cd2
    • Hao Luo's avatar
      bpf: Make per_cpu_ptr return rdonly PTR_TO_MEM. · 34d3a78c
      Hao Luo authored
      
      
      Tag the return type of {per, this}_cpu_ptr with RDONLY_MEM. The
      returned value of this pair of helpers is kernel object, which
      can not be updated by bpf programs. Previously these two helpers
      return PTR_OT_MEM for kernel objects of scalar type, which allows
      one to directly modify the memory. Now with RDONLY_MEM tagging,
      the verifier will reject programs that write into RDONLY_MEM.
      
      Fixes: 63d9b80d ("bpf: Introducte bpf_this_cpu_ptr()")
      Fixes: eaa6bcb7 ("bpf: Introduce bpf_per_cpu_ptr()")
      Fixes: 4976b718 ("bpf: Introduce pseudo_btf_id")
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-8-haoluo@google.com
      34d3a78c
    • Hao Luo's avatar
      bpf: Convert PTR_TO_MEM_OR_NULL to composable types. · cf9f2f8d
      Hao Luo authored
      
      
      Remove PTR_TO_MEM_OR_NULL and replace it with PTR_TO_MEM combined with
      flag PTR_MAYBE_NULL.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-7-haoluo@google.com
      cf9f2f8d
    • Hao Luo's avatar
      bpf: Introduce MEM_RDONLY flag · 20b2aff4
      Hao Luo authored
      
      
      This patch introduce a flag MEM_RDONLY to tag a reg value
      pointing to read-only memory. It makes the following changes:
      
      1. PTR_TO_RDWR_BUF -> PTR_TO_BUF
      2. PTR_TO_RDONLY_BUF -> PTR_TO_BUF | MEM_RDONLY
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-6-haoluo@google.com
      20b2aff4
    • Hao Luo's avatar
      bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL · c25b2ae1
      Hao Luo authored
      
      
      We have introduced a new type to make bpf_reg composable, by
      allocating bits in the type to represent flags.
      
      One of the flags is PTR_MAYBE_NULL which indicates a pointer
      may be NULL. This patch switches the qualified reg_types to
      use this flag. The reg_types changed in this patch include:
      
      1. PTR_TO_MAP_VALUE_OR_NULL
      2. PTR_TO_SOCKET_OR_NULL
      3. PTR_TO_SOCK_COMMON_OR_NULL
      4. PTR_TO_TCP_SOCK_OR_NULL
      5. PTR_TO_BTF_ID_OR_NULL
      6. PTR_TO_MEM_OR_NULL
      7. PTR_TO_RDONLY_BUF_OR_NULL
      8. PTR_TO_RDWR_BUF_OR_NULL
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/r/20211217003152.48334-5-haoluo@google.com
      c25b2ae1
    • Hao Luo's avatar
      bpf: Replace RET_XXX_OR_NULL with RET_XXX | PTR_MAYBE_NULL · 3c480732
      Hao Luo authored
      
      
      We have introduced a new type to make bpf_ret composable, by
      reserving high bits to represent flags.
      
      One of the flag is PTR_MAYBE_NULL, which indicates a pointer
      may be NULL. When applying this flag to ret_types, it means
      the returned value could be a NULL pointer. This patch
      switches the qualified arg_types to use this flag.
      The ret_types changed in this patch include:
      
      1. RET_PTR_TO_MAP_VALUE_OR_NULL
      2. RET_PTR_TO_SOCKET_OR_NULL
      3. RET_PTR_TO_TCP_SOCK_OR_NULL
      4. RET_PTR_TO_SOCK_COMMON_OR_NULL
      5. RET_PTR_TO_ALLOC_MEM_OR_NULL
      6. RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL
      7. RET_PTR_TO_BTF_ID_OR_NULL
      
      This patch doesn't eliminate the use of these names, instead
      it makes them aliases to 'RET_PTR_TO_XXX | PTR_MAYBE_NULL'.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-4-haoluo@google.com
      3c480732
    • Hao Luo's avatar
      bpf: Replace ARG_XXX_OR_NULL with ARG_XXX | PTR_MAYBE_NULL · 48946bd6
      Hao Luo authored
      
      
      We have introduced a new type to make bpf_arg composable, by
      reserving high bits of bpf_arg to represent flags of a type.
      
      One of the flags is PTR_MAYBE_NULL which indicates a pointer
      may be NULL. When applying this flag to an arg_type, it means
      the arg can take NULL pointer. This patch switches the
      qualified arg_types to use this flag. The arg_types changed
      in this patch include:
      
      1. ARG_PTR_TO_MAP_VALUE_OR_NULL
      2. ARG_PTR_TO_MEM_OR_NULL
      3. ARG_PTR_TO_CTX_OR_NULL
      4. ARG_PTR_TO_SOCKET_OR_NULL
      5. ARG_PTR_TO_ALLOC_MEM_OR_NULL
      6. ARG_PTR_TO_STACK_OR_NULL
      
      This patch does not eliminate the use of these arg_types, instead
      it makes them an alias to the 'ARG_XXX | PTR_MAYBE_NULL'.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-3-haoluo@google.com
      48946bd6
    • Hao Luo's avatar
      bpf: Introduce composable reg, ret and arg types. · d639b9d1
      Hao Luo authored
      
      
      There are some common properties shared between bpf reg, ret and arg
      values. For instance, a value may be a NULL pointer, or a pointer to
      a read-only memory. Previously, to express these properties, enumeration
      was used. For example, in order to test whether a reg value can be NULL,
      reg_type_may_be_null() simply enumerates all types that are possibly
      NULL. The problem of this approach is that it's not scalable and causes
      a lot of duplication. These properties can be combined, for example, a
      type could be either MAYBE_NULL or RDONLY, or both.
      
      This patch series rewrites the layout of reg_type, arg_type and
      ret_type, so that common properties can be extracted and represented as
      composable flag. For example, one can write
      
       ARG_PTR_TO_MEM | PTR_MAYBE_NULL
      
      which is equivalent to the previous
      
       ARG_PTR_TO_MEM_OR_NULL
      
      The type ARG_PTR_TO_MEM are called "base type" in this patch. Base
      types can be extended with flags. A flag occupies the higher bits while
      base types sits in the lower bits.
      
      This patch in particular sets up a set of macro for this purpose. The
      following patches will rewrite arg_types, ret_types and reg_types
      respectively.
      
      Signed-off-by: default avatarHao Luo <haoluo@google.com>
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Link: https://lore.kernel.org/bpf/20211217003152.48334-2-haoluo@google.com
      d639b9d1
  2. Dec 18, 2021
  3. Dec 17, 2021
  4. Dec 15, 2021
  5. Dec 14, 2021