1. 25 Aug, 2023 3 commits
    • Dave Marchevsky's avatar
      bpf: Use bpf_mem_free_rcu when bpf_obj_dropping refcounted nodes · 7e26cd12
      Dave Marchevsky authored
      This is the final fix for the use-after-free scenario described in
      commit 7793fc3b ("bpf: Make bpf_refcount_acquire fallible for
      non-owning refs"). That commit, by virtue of changing
      bpf_refcount_acquire's refcount_inc to a refcount_inc_not_zero, fixed
      the "refcount incr on 0" splat. The not_zero check in
      refcount_inc_not_zero, though, still occurs on memory that could have
      been free'd and reused, so the commit didn't properly fix the root
      cause.
      
      This patch actually fixes the issue by free'ing using the recently-added
      bpf_mem_free_rcu, which ensures that the memory is not reused until
      RCU grace period has elapsed. If that has happened then
      there are no non-owning references alive that point to the
      recently-free'd memory, so it can be safely reused.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20230821193311.3290257-4-davemarchevsky@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      7e26cd12
    • Dave Marchevsky's avatar
      bpf: Consider non-owning refs trusted · 2a6d50b5
      Dave Marchevsky authored
      Recent discussions around default kptr "trustedness" led to changes such
      as commit 6fcd486b ("bpf: Refactor RCU enforcement in the
      verifier."). One of the conclusions of those discussions, as expressed
      in code and comments in that patch, is that we'd like to move away from
      'raw' PTR_TO_BTF_ID without some type flag or other register state
      indicating trustedness. Although PTR_TRUSTED and PTR_UNTRUSTED flags mark
      this state explicitly, the verifier currently considers trustedness
      implied by other register state. For example, owning refs to graph
      collection nodes must have a nonzero ref_obj_id, so they pass the
      is_trusted_reg check despite having no explicit PTR_{UN}TRUSTED flag.
      This patch makes trustedness of non-owning refs to graph collection
      nodes explicit as well.
      
      By definition, non-owning refs are currently trusted. Although the ref
      has no control over pointee lifetime, due to non-owning ref clobbering
      rules (see invalidate_non_owning_refs) dereferencing a non-owning ref is
      safe in the critical section controlled by bpf_spin_lock associated with
      its owning collection.
      
      Note that the previous statement does not hold true for nodes with shared
      ownership due to the use-after-free issue that this series is
      addressing. True shared ownership was disabled by commit 7deca5ea
      ("bpf: Disable bpf_refcount_acquire kfunc calls until race conditions are fixed"),
      though, so the statement holds for now. Further patches in the series will change
      the trustedness state of non-owning refs before re-enabling
      bpf_refcount_acquire.
      
      Let's add NON_OWN_REF type flag to BPF_REG_TRUSTED_MODIFIERS such that a
      non-owning ref reg state would pass is_trusted_reg check. Somewhat
      surprisingly, this doesn't result in any change to user-visible
      functionality elsewhere in the verifier: graph collection nodes are all
      marked MEM_ALLOC, which tends to be handled in separate codepaths from
      "raw" PTR_TO_BTF_ID. Regardless, let's be explicit here and document the
      current state of things before changing it elsewhere in the series.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20230821193311.3290257-3-davemarchevsky@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2a6d50b5
    • Dave Marchevsky's avatar
      bpf: Ensure kptr_struct_meta is non-NULL for collection insert and refcount_acquire · f0d991a0
      Dave Marchevsky authored
      It's straightforward to prove that kptr_struct_meta must be non-NULL for
      any valid call to these kfuncs:
      
        * btf_parse_struct_metas in btf.c creates a btf_struct_meta for any
          struct in user BTF with a special field (e.g. bpf_refcount,
          {rb,list}_node). These are stored in that BTF's struct_meta_tab.
      
        * __process_kf_arg_ptr_to_graph_node in verifier.c ensures that nodes
          have {rb,list}_node field and that it's at the correct offset.
          Similarly, check_kfunc_args ensures bpf_refcount field existence for
          node param to bpf_refcount_acquire.
      
        * So a btf_struct_meta must have been created for the struct type of
          node param to these kfuncs
      
        * That BTF and its struct_meta_tab are guaranteed to still be around.
          Any arbitrary {rb,list} node the BPF program interacts with either:
          came from bpf_obj_new or a collection removal kfunc in the same
          program, in which case the BTF is associated with the program and
          still around; or came from bpf_kptr_xchg, in which case the BTF was
          associated with the map and is still around
      
      Instead of silently continuing with NULL struct_meta, which caused
      confusing bugs such as those addressed by commit 2140a6e3 ("bpf: Set
      kptr_struct_meta for node param to list and rbtree insert funcs"), let's
      error out. Then, at runtime, we can confidently say that the
      implementations of these kfuncs were given a non-NULL kptr_struct_meta,
      meaning that special-field-specific functionality like
      bpf_obj_free_fields and the bpf_obj_drop change introduced later in this
      series are guaranteed to execute.
      
      This patch doesn't change functionality, just makes it easier to reason
      about existing functionality.
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/r/20230821193311.3290257-2-davemarchevsky@fb.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f0d991a0
  2. 24 Aug, 2023 21 commits
  3. 23 Aug, 2023 3 commits
  4. 22 Aug, 2023 6 commits
  5. 21 Aug, 2023 7 commits