Commit Graph

364 Commits

Author SHA1 Message Date
Jerome Marchand a66aab6d44 bpf: Fix memory leak in bpf_core_apply
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 45126b155e3b5201179cdc038504bf93a8ccd921
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Mon Oct 7 18:09:58 2024 +0200

    bpf: Fix memory leak in bpf_core_apply

    We need to free specs properly.

    Fixes: 3d2786d65aaa ("bpf: correctly handle malformed BPF_CORE_TYPE_ID_LOCAL relos")
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/bpf/20241007160958.607434-1-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:07 +01:00
Jerome Marchand c3259cbfec bpf: Call the missed kfree() when there is no special field in btf
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 986deb297d48ae7039ab975f00c14f0bfe069125
Author: Hou Tao <houtao1@huawei.com>
Date:   Thu Sep 12 09:28:45 2024 +0800

    bpf: Call the missed kfree() when there is no special field in btf

    Call the missed kfree() in btf_parse_struct_metas() when there is no
    special field in btf, otherwise will get the following kmemleak report:

    unreferenced object 0xffff888101033620 (size 8):
      comm "test_progs", pid 604, jiffies 4295127011
      ......
      backtrace (crc e77dc444):
        [<00000000186f90f3>] kmemleak_alloc+0x4b/0x80
        [<00000000ac8e9c4d>] __kmalloc_cache_noprof+0x2a1/0x310
        [<00000000d99d68d6>] btf_new_fd+0x72d/0xe90
        [<00000000f010b7f8>] __sys_bpf+0xec3/0x2410
        [<00000000e077ed6f>] __x64_sys_bpf+0x1f/0x30
        [<00000000a12f9e55>] x64_sys_call+0x199/0x9f0
        [<00000000f3029ea6>] do_syscall_64+0x3b/0xc0
        [<000000005640913a>] entry_SYSCALL_64_after_hwframe+0x4b/0x53

    Fixes: 7a851ecb1806 ("bpf: Search for kptrs in prog BTF structs")
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20240912012845.3458483-3-houtao@huaweicloud.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:07 +01:00
Jerome Marchand 46a120a12a bpf: Support __nullable argument suffix for tp_btf
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 8aeaed21befc90f27f4fca6dd190850d97d2e9e3
Author: Philo Lu <lulie@linux.alibaba.com>
Date:   Wed Sep 11 11:37:15 2024 +0800

    bpf: Support __nullable argument suffix for tp_btf

    Pointers passed to tp_btf were trusted to be valid, but some tracepoints
    do take NULL pointer as input, such as trace_tcp_send_reset(). Then the
    invalid memory access cannot be detected by verifier.

    This patch fix it by add a suffix "__nullable" to the unreliable
    argument. The suffix is shown in btf, and PTR_MAYBE_NULL will be added
    to nullable arguments. Then users must check the pointer before use it.

    A problem here is that we use "btf_trace_##call" to search func_proto.
    As it is a typedef, argument names as well as the suffix are not
    recorded. To solve this, I use bpf_raw_event_map to find
    "__bpf_trace##template" from "btf_trace_##call", and then we can see the
    suffix.

    Suggested-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Philo Lu <lulie@linux.alibaba.com>
    Link: https://lore.kernel.org/r/20240911033719.91468-2-lulie@linux.alibaba.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:04 +01:00
Jerome Marchand 7668b8d4d2 bpf: allow kfuncs within tracepoint and perf event programs
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit bc638d8cb5be813d4eeb9f63cce52caaa18f3960
Author: JP Kobryn <inwardvessel@gmail.com>
Date:   Thu Sep 5 15:38:11 2024 -0700

    bpf: allow kfuncs within tracepoint and perf event programs

    Associate tracepoint and perf event program types with the kfunc tracing
    hook. This allows calling kfuncs within these types of programs.

    Signed-off-by: JP Kobryn <inwardvessel@gmail.com>
    Link: https://lore.kernel.org/r/20240905223812.141857-2-inwardvessel@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:04 +01:00
Jerome Marchand 39a4da7429 bpf: Use kvmemdup to simplify the code
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit c6d9dafb595564ae44ef2e25e3ace2973aa867f3
Author: Hongbo Li <lihongbo22@huawei.com>
Date:   Wed Aug 28 14:21:28 2024 +0800

    bpf: Use kvmemdup to simplify the code

    Use kvmemdup instead of kvmalloc() + memcpy() to simplify the
    code.

    No functional change intended.

    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
    Link: https://lore.kernel.org/r/20240828062128.1223417-1-lihongbo22@huawei.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:01 +01:00
Jerome Marchand 40b0b170c3 bpf: Search for kptrs in prog BTF structs
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 7a851ecb180686f9e50110247b0fcd537e772e63
Author: Dave Marchevsky <davemarchevsky@fb.com>
Date:   Tue Aug 13 21:24:21 2024 +0000

    bpf: Search for kptrs in prog BTF structs

    Currently btf_parse_fields is used in two places to create struct
    btf_record's for structs: when looking at mapval type, and when looking
    at any struct in program BTF. The former looks for kptr fields while the
    latter does not. This patch modifies the btf_parse_fields call made when
    looking at prog BTF struct types to search for kptrs as well.

    Before this series there was no reason to search for kptrs in non-mapval
    types: a referenced kptr needs some owner to guarantee resource cleanup,
    and map values were the only owner that supported this. If a struct with
    a kptr field were to have some non-kptr-aware owner, the kptr field
    might not be properly cleaned up and result in resources leaking. Only
    searching for kptr fields in mapval was a simple way to avoid this
    problem.

    In practice, though, searching for BPF_KPTR when populating
    struct_meta_tab does not expose us to this risk, as struct_meta_tab is
    only accessed through btf_find_struct_meta helper, and that helper is
    only called in contexts where recognizing the kptr field is safe:

      * PTR_TO_BTF_ID reg w/ MEM_ALLOC flag
        * Such a reg is a local kptr and must be free'd via bpf_obj_drop,
          which will correctly handle kptr field

      * When handling specific kfuncs which either expect MEM_ALLOC input or
        return MEM_ALLOC output (obj_{new,drop}, percpu_obj_{new,drop},
        list+rbtree funcs, refcount_acquire)
         * Will correctly handle kptr field for same reasons as above

      * When looking at kptr pointee type
         * Called by functions which implement "correct kptr resource
           handling"

      * In btf_check_and_fixup_fields
         * Helper that ensures no ownership loops for lists and rbtrees,
           doesn't care about kptr field existence

    So we should be able to find BPF_KPTR fields in all prog BTF structs
    without leaking resources.

    Further patches in the series will build on this change to support
    kptr_xchg into non-mapval local kptr. Without this change there would be
    no kptr field found in such a type.

    Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
    Acked-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
    Signed-off-by: Amery Hung <amery.hung@bytedance.com>
    Link: https://lore.kernel.org/r/20240813212424.2871455-3-amery.hung@bytedance.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:00 +01:00
Jerome Marchand dfaf1f971e bpf: Let callers of btf_parse_kptr() track life cycle of prog btf
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit c5ef53420f46c9ca6badca4f4cabacd76de8091e
Author: Amery Hung <amery.hung@bytedance.com>
Date:   Tue Aug 13 21:24:20 2024 +0000

    bpf: Let callers of btf_parse_kptr() track life cycle of prog btf

    btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
    respectively when working on btf_record in program and map if there are
    kptr fields. If the kptr is from program BTF, since both callers has
    already tracked the life cycle of program BTF, it is safe to remove the
    btf_get() and btf_put().

    This change prevents memory leak of program BTF later when we start
    searching for kptr fields when building btf_record for program. It can
    happen when the btf fd is closed. The btf_put() corresponding to the
    btf_get() in btf_parse_kptr() was supposed to be called by
    btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
    it will never happen since the invocation of btf_free() depends on the
    refcount of the btf to become 0 in the first place.

    Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
    Acked-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Amery Hung <amery.hung@bytedance.com>
    Link: https://lore.kernel.org/r/20240813212424.2871455-2-amery.hung@bytedance.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:00 +01:00
Jerome Marchand 66e2366392 bpf: correctly handle malformed BPF_CORE_TYPE_ID_LOCAL relos
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 3d2786d65aaa954ebd3fcc033ada433e10da21c4
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Thu Aug 22 01:01:23 2024 -0700

    bpf: correctly handle malformed BPF_CORE_TYPE_ID_LOCAL relos

    In case of malformed relocation record of kind BPF_CORE_TYPE_ID_LOCAL
    referencing a non-existing BTF type, function bpf_core_calc_relo_insn
    would cause a null pointer deference.

    Fix this by adding a proper check upper in call stack, as malformed
    relocation records could be passed from user space.

    Simplest reproducer is a program:

        r0 = 0
        exit

    With a single relocation record:

        .insn_off = 0,          /* patch first instruction */
        .type_id = 100500,      /* this type id does not exist */
        .access_str_off = 6,    /* offset of string "0" */
        .kind = BPF_CORE_TYPE_ID_LOCAL,

    See the link for original reproducer or next commit for a test case.

    Fixes: 74753e1462e7 ("libbpf: Replace btf__type_by_id() with btf_type_by_id().")
    Reported-by: Liu RuiTong <cnitlrt@gmail.com>
    Closes: https://lore.kernel.org/bpf/CAK55_s6do7C+DVwbwY_7nKfUz0YLDoiA1v6X3Y9+p0sWzipFSA@mail.gmail.com/
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240822080124.2995724-2-eddyz87@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:24:25 +01:00
Jerome Marchand 4c3d89b176 bpf: extract iterator argument type and name validation logic
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 496ddd19a0fad22f250fc7a7b7a8000155418934
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Aug 8 16:22:28 2024 -0700

    bpf: extract iterator argument type and name validation logic

    Verifier enforces that all iterator structs are named `bpf_iter_<name>`
    and that whenever iterator is passed to a kfunc it's passed as a valid PTR ->
    STRUCT chain (with potentially const modifiers in between).

    We'll need this check for upcoming changes, so instead of duplicating
    the logic, extract it into a helper function.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240808232230.2848712-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:24:24 +01:00
Jerome Marchand 7c6ab9c4b7 bpf: Enable generic kfuncs for BPF_CGROUP_* programs
JIRA: https://issues.redhat.com/browse/RHEL-63880

Conflicts: Context change due to missing commit 53e380d21441 ("bpf:
Add bpf_sock_addr_set_sun_path() to allow writing unix sockaddr from
bpf")

commit 67666479edf1e2b732f4d0ac797885e859a78de4
Author: Matteo Croce <teknoraver@meta.com>
Date:   Mon Aug 19 18:28:04 2024 +0200

    bpf: Enable generic kfuncs for BPF_CGROUP_* programs

    These kfuncs are enabled even in BPF_PROG_TYPE_TRACING, so they
    should be safe also in BPF_CGROUP_* programs.
    Since all BPF_CGROUP_* programs share the same hook,
    call register_btf_kfunc_id_set() only once.

    In enum btf_kfunc_hook, rename BTF_KFUNC_HOOK_CGROUP_SKB to a more
    generic BTF_KFUNC_HOOK_CGROUP, since it's used for all the cgroup
    related program types.

    Signed-off-by: Matteo Croce <teknoraver@meta.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240819162805.78235-2-technoboy85@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:24:24 +01:00
Jerome Marchand b082ad8cdb bpf: Remove __btf_name_valid() and change to btf_name_valid_identifier()
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit febb6f3e3ac196602cca5738d8a189d57392324a
Author: Jeongjun Park <aha310510@gmail.com>
Date:   Wed Aug 7 23:31:10 2024 +0900

    bpf: Remove __btf_name_valid() and change to btf_name_valid_identifier()

    __btf_name_valid() can be completely replaced with
    btf_name_valid_identifier, and since most of the time you already call
    btf_name_valid_identifier instead of __btf_name_valid , it would be
    appropriate to rename the __btf_name_valid function to
    btf_name_valid_identifier and remove __btf_name_valid.

    Signed-off-by: Jeongjun Park <aha310510@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/bpf/20240807143110.181497-1-aha310510@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:24:24 +01:00
Viktor Malik 68caf76b9f
bpf: Check the remaining info_cnt before repeating btf fields
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 797d73ee232dd1833dec4824bc53a22032e97c1c
Author: Hou Tao <houtao1@huawei.com>
Date:   Tue Oct 8 15:11:13 2024 +0800

    bpf: Check the remaining info_cnt before repeating btf fields
    
    When trying to repeat the btf fields for array of nested struct, it
    doesn't check the remaining info_cnt. The following splat will be
    reported when the value of ret * nelems is greater than BTF_FIELDS_MAX:
    
      ------------[ cut here ]------------
      UBSAN: array-index-out-of-bounds in ../kernel/bpf/btf.c:3951:49
      index 11 is out of range for type 'btf_field_info [11]'
      CPU: 6 UID: 0 PID: 411 Comm: test_progs ...... 6.11.0-rc4+ #1
      Tainted: [O]=OOT_MODULE
      Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS ...
      Call Trace:
       <TASK>
       dump_stack_lvl+0x57/0x70
       dump_stack+0x10/0x20
       ubsan_epilogue+0x9/0x40
       __ubsan_handle_out_of_bounds+0x6f/0x80
       ? kallsyms_lookup_name+0x48/0xb0
       btf_parse_fields+0x992/0xce0
       map_create+0x591/0x770
       __sys_bpf+0x229/0x2410
       __x64_sys_bpf+0x1f/0x30
       x64_sys_call+0x199/0x9f0
       do_syscall_64+0x3b/0xc0
       entry_SYSCALL_64_after_hwframe+0x4b/0x53
      RIP: 0033:0x7fea56f2cc5d
      ......
       </TASK>
      ---[ end trace ]---
    
    Fix it by checking the remaining info_cnt in btf_repeat_fields() before
    repeating the btf fields.
    
    Fixes: 64e8ee814819 ("bpf: look into the types of the fields of a struct type recursively.")
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20241008071114.3718177-2-houtao@huaweicloud.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:20 +01:00
Viktor Malik 474cc3f506
bpf: Fix a crash when btf_parse_base() returns an error pointer
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit b408473ea01b2e499d23503e2bf898416da9d7ac
Author: Martin KaFai Lau <martin.lau@kernel.org>
Date:   Thu Aug 29 18:22:14 2024 -0700

    bpf: Fix a crash when btf_parse_base() returns an error pointer
    
    The pointer returned by btf_parse_base could be an error pointer.
    IS_ERR() check is needed before calling btf_free(base_btf).
    
    Fixes: 8646db238997 ("libbpf,bpf: Share BTF relocate-related code with kernel")
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/bpf/20240830012214.1646005-1-martin.lau@linux.dev

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:20 +01:00
Viktor Malik cdc574cae2
bpf: Eliminate remaining "make W=1" warnings in kernel/bpf/btf.o
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 2454075f8e2915cebbe52a1195631bc7efe2b7e1
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Fri Jul 12 10:28:59 2024 +0100

    bpf: Eliminate remaining "make W=1" warnings in kernel/bpf/btf.o
    
    As reported by Mirsad [1] we still see format warnings in kernel/bpf/btf.o
    at W=1 warning level:
    
      CC      kernel/bpf/btf.o
    ./kernel/bpf/btf.c: In function ‘btf_type_seq_show_flags’:
    ./kernel/bpf/btf.c:7553:21: warning: assignment left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format]
     7553 |         sseq.showfn = btf_seq_show;
          |                     ^
    ./kernel/bpf/btf.c: In function ‘btf_type_snprintf_show’:
    ./kernel/bpf/btf.c:7604:31: warning: assignment left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format]
     7604 |         ssnprintf.show.showfn = btf_snprintf_show;
          |                               ^
    
    Combined with CONFIG_WERROR=y these can halt the build.
    
    The fix (annotating the structure field with __printf())
    suggested by Mirsad resolves these. Apologies I missed this last time.
    No other W=1 warnings were observed in kernel/bpf after this fix.
    
    [1] https://lore.kernel.org/bpf/92c9d047-f058-400c-9c7d-81d4dc1ef71b@gmail.com/
    
    Fixes: b3470da314fd ("bpf: annotate BTF show functions with __printf")
    Reported-by: Mirsad Todorovac <mtodorovac69@gmail.com>
    Suggested-by: Mirsad Todorovac <mtodorovac69@gmail.com>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20240712092859.1390960-1-alan.maguire@oracle.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:17 +01:00
Viktor Malik 244e63f1b6
bpf: annotate BTF show functions with __printf
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit b3470da314fd8018ee237e382000c4154a942420
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Thu Jul 11 19:23:21 2024 +0100

    bpf: annotate BTF show functions with __printf
    
    -Werror=suggest-attribute=format warns about two functions
    in kernel/bpf/btf.c [1]; add __printf() annotations to silence
    these warnings since for CONFIG_WERROR=y they will trigger
    build failures.
    
    [1] https://lore.kernel.org/bpf/a8b20c72-6631-4404-9e1f-0410642d7d20@gmail.com/
    
    Fixes: 31d0bc8163 ("bpf: Move to generic BTF show support, apply it to seq files/strings")
    Reported-by: Mirsad Todorovac <mtodorovac69@gmail.com>
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Tested-by: Mirsad Todorovac <mtodorovac69@yahoo.com>
    Link: https://lore.kernel.org/r/20240711182321.963667-1-alan.maguire@oracle.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:17 +01:00
Viktor Malik d1cea34684
bpf: fix build when CONFIG_DEBUG_INFO_BTF[_MODULES] is undefined
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 5a532459aa919d055d822d8db4ea2c5c8d511568
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Sun Jun 23 14:52:24 2024 +0100

    bpf: fix build when CONFIG_DEBUG_INFO_BTF[_MODULES] is undefined
    
    Kernel test robot reports that kernel build fails with
    resilient split BTF changes.
    
    Examining the associated config and code we see that
    btf_relocate_id() is defined under CONFIG_DEBUG_INFO_BTF_MODULES.
    Moving it outside the #ifdef solves the issue.
    
    Reported-by: kernel test robot <lkp@intel.com>
    Closes: https://lore.kernel.org/oe-kbuild-all/202406221742.d2srFLVI-lkp@intel.com/
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Link: https://lore.kernel.org/r/20240623135224.27981-1-alan.maguire@oracle.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:11 +01:00
Viktor Malik c32a0e6b18
libbpf,bpf: Share BTF relocate-related code with kernel
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 8646db238997df36c6ad71a9d7e0b52ceee221b2
Author: Alan Maguire <alan.maguire@oracle.com>
Date:   Thu Jun 20 10:17:31 2024 +0100

    libbpf,bpf: Share BTF relocate-related code with kernel
    
    Share relocation implementation with the kernel.  As part of this,
    we also need the type/string iteration functions so also share
    btf_iter.c file. Relocation code in kernel and userspace is identical
    save for the impementation of the reparenting of split BTF to the
    relocated base BTF and retrieval of the BTF header from "struct btf";
    these small functions need separate user-space and kernel implementations
    for the separate "struct btf"s they operate upon.
    
    One other wrinkle on the kernel side is we have to map .BTF.ids in
    modules as they were generated with the type ids used at BTF encoding
    time. btf_relocate() optionally returns an array mapping from old BTF
    ids to relocated ids, so we use that to fix up these references where
    needed for kfuncs.
    
    Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/bpf/20240620091733.1967885-5-alan.maguire@oracle.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 15:55:10 +01:00
Viktor Malik ae345359c0
bpf: verifier: Relax caller requirements for kfunc projection type args
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit ec209ad86324de84ef66990f0e9df0851e45e054
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Wed Jun 12 09:58:32 2024 -0600

    bpf: verifier: Relax caller requirements for kfunc projection type args
    
    Currently, if a kfunc accepts a projection type as an argument (eg
    struct __sk_buff *), the caller must exactly provide exactly the same
    type with provable provenance.
    
    However in practice, kfuncs that accept projection types _must_ cast to
    the underlying type before use b/c projection type layouts are
    completely made up. Thus, it is ok to relax the verifier rules around
    implicit conversions.
    
    We will use this functionality in the next commit when we align kfuncs
    to user-facing types.
    
    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Link: https://lore.kernel.org/r/e2c025cb09ccfd4af1ec9e18284dc3cecff7514d.1718207789.git.dxu@dxuuu.xyz
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:07 +01:00
Viktor Malik c4cef02954
bpf: limit the number of levels of a nested struct type.
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit f19caf57d80f4432acea61d858d45ce194444389
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu May 23 10:41:59 2024 -0700

    bpf: limit the number of levels of a nested struct type.
    
    Limit the number of levels looking into struct types to avoid running out
    of stack space.
    
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240523174202.461236-7-thinker.li@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:03 +01:00
Viktor Malik 19d98aac0b
bpf: look into the types of the fields of a struct type recursively.
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 64e8ee814819f21beeeda00d4119221443d77992
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu May 23 10:41:58 2024 -0700

    bpf: look into the types of the fields of a struct type recursively.
    
    The verifier has field information for specific special types, such as
    kptr, rbtree root, and list head. These types are handled
    differently. However, we did not previously examine the types of fields of
    a struct type variable. Field information records were not generated for
    the kptrs, rbtree roots, and linked_list heads that are not located at the
    outermost struct type of a variable.
    
    For example,
    
      struct A {
        struct task_struct __kptr * task;
      };
    
      struct B {
        struct A mem_a;
      }
    
      struct B var_b;
    
    It did not examine "struct A" so as not to generate field information for
    the kptr in "struct A" for "var_b".
    
    This patch enables BPF programs to define fields of these special types in
    a struct type other than the direct type of a variable or in a struct type
    that is the type of a field in the value type of a map.
    
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240523174202.461236-6-thinker.li@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:03 +01:00
Viktor Malik 2e7a608480
bpf: create repeated fields for arrays.
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 994796c0256c4001633488fd24c3d54691949f8d
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu May 23 10:41:57 2024 -0700

    bpf: create repeated fields for arrays.
    
    The verifier uses field information for certain special types, such as
    kptr, rbtree root, and list head. These types are treated
    differently. However, we did not previously support these types in
    arrays. This update examines arrays and duplicates field information the
    same number of times as the length of the array if the element type is one
    of the special types.
    
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240523174202.461236-5-thinker.li@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:03 +01:00
Viktor Malik f6af98aeb2
bpf: refactor btf_find_struct_field() and btf_find_datasec_var().
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit a7db0d4f872a869feb7c0201c0fa736c309192d5
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu May 23 10:41:56 2024 -0700

    bpf: refactor btf_find_struct_field() and btf_find_datasec_var().
    
    Move common code of the two functions to btf_find_field_one().
    
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240523174202.461236-4-thinker.li@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:03 +01:00
Viktor Malik 01b29b3c5f
bpf: Remove unnecessary call to btf_field_type_size().
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 482f7133791e894b94a57ab3251e03d4c98ea42b
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu May 23 10:41:55 2024 -0700

    bpf: Remove unnecessary call to btf_field_type_size().
    
    field->size has been initialized by bpf_parse_fields() with the value
    returned by btf_field_type_size(). Use it instead of calling
    btf_field_type_size() again.
    
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240523174202.461236-3-thinker.li@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:03 +01:00
Rado Vrbovsky 56ce48fda2 Merge: BPF 6.10 rebase
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5693

Rebase BPF subsystem to upstream version 6.10

JIRA: https://issues.redhat.com/browse/RHEL-30773  
JIRA: https://issues.redhat.com/browse/RHEL-64874

CVE: CVE-2024-38564

Omitted-fix: 2897b1e2a2f4 ("selftests/bpf: Fix arena_atomics failure due to llvm change")  
Fix for a bug caused by a not yet released version of LLVM. Will be backported with 6.12 rebase where it belongs.

Omitted-fix: ff9fbcafbaf1 ("selftests/hid: fix bpf_wq new API")  
The fixed test is not yet present in RHEL 9.

Signed-off-by: Viktor Malik <vmalik@redhat.com>

Approved-by: Chris von Recklinghausen <crecklin@redhat.com>
Approved-by: Derek Barbosa <debarbos@redhat.com>
Approved-by: Jerome Marchand <jmarchan@redhat.com>
Approved-by: Toke Høiland-Jørgensen <toke@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-22 09:24:52 +00:00
Rado Vrbovsky 89979ba29d Merge: bpf, lsm: Add check for BPF LSM return value
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5791

JIRA: https://issues.redhat.com/browse/RHEL-63343

CVE: CVE-2024-47703
    
    commit 5d99e198be279045e6ecefe220f5c52f8ce9bfd5
    Author: Xu Kuohai <xukuohai@huawei.com>
    Date:   Fri Jul 19 19:00:52 2024 +0800
    
        bpf, lsm: Add check for BPF LSM return value
    
        A bpf prog returning a positive number attached to file_alloc_security
        hook makes kernel panic.
    
        This happens because file system can not filter out the positive number
        returned by the LSM prog using IS_ERR, and misinterprets this positive
        number as a file pointer.
    
        Given that hook file_alloc_security never returned positive number
        before the introduction of BPF LSM, and other BPF LSM hooks may
        encounter similar issues, this patch adds LSM return value check
        in verifier, to ensure no unexpected value is returned.
    
        Fixes: 520b7aa00d ("bpf: lsm: Initialize the BPF LSM hooks")
        Reported-by: Xin Liu <liuxin350@huawei.com>
        Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
        Acked-by: Eduard Zingerman <eddyz87@gmail.com>
        Link: https://lore.kernel.org/r/20240719110059.797546-3-xukuohai@huaweicloud.com
        Signed-off-by: Alexei Starovoitov <ast@kernel.org>
        Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    
Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>  
Signed-off-by: Viktor Malik <vmalik@redhat.com>

Approved-by: Luis Claudio R. Goncalves <lgoncalv@redhat.com>
Approved-by: Derek Barbosa <debarbos@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-22 09:15:56 +00:00
Viktor Malik db6122a3e5
bpf: Add support for kprobe session context
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit adf46d88ae4b2557f7e2e02547a25fb866935711
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Tue Apr 30 13:28:25 2024 +0200

    bpf: Add support for kprobe session context
    
    Adding struct bpf_session_run_ctx object to hold session related
    data, which is atm is_return bool and data pointer coming in
    following changes.
    
    Placing bpf_session_run_ctx layer in between bpf_run_ctx and
    bpf_kprobe_multi_run_ctx so the session data can be retrieved
    regardless of if it's kprobe_multi or uprobe_multi link, which
    support is coming in future. This way both kprobe_multi and
    uprobe_multi can use same kfuncs to access the session data.
    
    Adding bpf_session_is_return kfunc that returns true if the
    bpf program is executed from the exit probe of the kprobe multi
    link attached in wrapper mode. It returns false otherwise.
    
    Adding new kprobe hook for kprobe program type.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240430112830.1184228-3-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:56 +01:00
Viktor Malik 109d9b01f8
bpf: add support for bpf_wq user type
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit d56b63cf0c0f71e1b2e04dd8220b408f049e67ff
Author: Benjamin Tissoires <bentiss@kernel.org>
Date:   Sat Apr 20 11:09:05 2024 +0200

    bpf: add support for bpf_wq user type
    
    Mostly a copy/paste from the bpf_timer API, without the initialization
    and free, as they will be done in a separate patch.
    
    Signed-off-by: Benjamin Tissoires <bentiss@kernel.org>
    Link: https://lore.kernel.org/r/20240420-bpf_wq-v2-5-6c986a5a741f@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:49 +01:00
Viktor Malik 18e3a2d83a
btf: Avoid weak external references
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit fc5eb4a84e4c063e75a6a6e92308e9533c0f19b5
Author: Ard Biesheuvel <ardb@kernel.org>
Date:   Mon Apr 15 18:20:45 2024 +0200

    btf: Avoid weak external references
    
    If the BTF code is enabled in the build configuration, the start/stop
    BTF markers are guaranteed to exist. Only when CONFIG_DEBUG_INFO_BTF=n,
    the references in btf_parse_vmlinux() will remain unsatisfied, relying
    on the weak linkage of the external references to avoid breaking the
    build.
    
    Avoid GOT based relocations to these markers in the final executable by
    dropping the weak attribute and instead, make btf_parse_vmlinux() return
    ERR_PTR(-ENOENT) directly if CONFIG_DEBUG_INFO_BTF is not enabled to
    begin with.  The compiler will drop any subsequent references to
    __start_BTF and __stop_BTF in that case, allowing the link to succeed.
    
    Note that Clang will notice that taking the address of __start_BTF can
    no longer yield NULL, so testing for that condition becomes unnecessary.
    
    Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Arnd Bergmann <arnd@arndb.de>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/bpf/20240415162041.2491523-8-ardb+git@google.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:47 +01:00
CKI Backport Bot 9ade42eda4 bpf, lsm: Add check for BPF LSM return value
JIRA: https://issues.redhat.com/browse/RHEL-63343
CVE: CVE-2024-47703

commit 5d99e198be279045e6ecefe220f5c52f8ce9bfd5
Author: Xu Kuohai <xukuohai@huawei.com>
Date:   Fri Jul 19 19:00:52 2024 +0800

    bpf, lsm: Add check for BPF LSM return value

    A bpf prog returning a positive number attached to file_alloc_security
    hook makes kernel panic.

    This happens because file system can not filter out the positive number
    returned by the LSM prog using IS_ERR, and misinterprets this positive
    number as a file pointer.

    Given that hook file_alloc_security never returned positive number
    before the introduction of BPF LSM, and other BPF LSM hooks may
    encounter similar issues, this patch adds LSM return value check
    in verifier, to ensure no unexpected value is returned.

    Fixes: 520b7aa00d ("bpf: lsm: Initialize the BPF LSM hooks")
    Reported-by: Xin Liu <liuxin350@huawei.com>
    Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240719110059.797546-3-xukuohai@huaweicloud.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-10-21 22:38:41 +00:00
Jerome Marchand 4bc7a98fa9 bpf: add check for invalid name in btf_name_valid_section()
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit bb6705c3f93bed2af03d43691743d4c43e3c8e6f
Author: Jeongjun Park <aha310510@gmail.com>
Date:   Sat Aug 31 14:47:02 2024 +0900

    bpf: add check for invalid name in btf_name_valid_section()

    If the length of the name string is 1 and the value of name[0] is NULL
    byte, an OOB vulnerability occurs in btf_name_valid_section() and the
    return value is true, so the invalid name passes the check.

    To solve this, you need to check if the first position is NULL byte and
    if the first character is printable.

    Suggested-by: Eduard Zingerman <eddyz87@gmail.com>
    Fixes: bd70a8fb7ca4 ("bpf: Allow all printable characters in BTF DATASEC names")
    Signed-off-by: Jeongjun Park <aha310510@gmail.com>
    Link: https://lore.kernel.org/r/20240831054702.364455-1-aha310510@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:19 +02:00
Jerome Marchand 654cc3dfc7 bpf: Recognize btf_decl_tag("arg: Arena") as PTR_TO_ARENA.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 2edc3de6fb650924a87fffebebc3b7572cbf6e38
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Thu Mar 7 17:08:04 2024 -0800

    bpf: Recognize btf_decl_tag("arg: Arena") as PTR_TO_ARENA.

    In global bpf functions recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA.

    Note, when the verifier sees:

    __weak void foo(struct bar *p)

    it recognizes 'p' as PTR_TO_MEM and 'struct bar' has to be a struct with scalars.
    Hence the only way to use arena pointers in global functions is to tag them with "arg:arena".

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/bpf/20240308010812.89848-7-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:15 +02:00
Jerome Marchand a9c2700834 bpf: Allow all printable characters in BTF DATASEC names
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit bd70a8fb7ca4fcb078086f4d96b048aaf1aa4786
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Wed Mar 6 12:45:28 2024 +0200

    bpf: Allow all printable characters in BTF DATASEC names

    The intent is to allow libbpf to use SEC("?.struct_ops") to identify
    struct_ops maps that are optional, e.g. like in the following BPF code:

        SEC("?.struct_ops")
        struct test_ops optional_map = { ... };

    Which yields the following BTF:

        ...
        [13] DATASEC '?.struct_ops' size=0 vlen=...
        ...

    To load such BTF libbpf rewrites DATASEC name before load.
    After this patch the rewrite won't be necessary.

    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240306104529.6453-15-eddyz87@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:14 +02:00
Jerome Marchand da5858be9e bpf: don't infer PTR_TO_CTX for programs with unnamed context type
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 879bbe7aa4afa80acf72a1cad7f52416ea78c52d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Feb 12 15:32:20 2024 -0800

    bpf: don't infer PTR_TO_CTX for programs with unnamed context type

    For program types that don't have named context type name (e.g., BPF
    iterator programs or tracepoint programs), ctx_tname will be a non-NULL
    empty string. For such programs it shouldn't be possible to have
    PTR_TO_CTX argument for global subprogs based on type name alone.
    arg:ctx tag is the only way to have PTR_TO_CTX passed into global
    subprog for such program types.

    Fix this loophole, which currently would assume PTR_TO_CTX whenever
    user uses a pointer to anonymous struct as an argument to their global
    subprogs. This happens in practice with the following (quite common, in
    practice) approach:

    typedef struct { /* anonymous */
        int x;
    } my_type_t;

    int my_subprog(my_type_t *arg) { ... }

    User's intent is to have PTR_TO_MEM argument for `arg`, but verifier
    will complain about expecting PTR_TO_CTX.

    This fix also closes unintended s390x-specific KPROBE handling of
    PTR_TO_CTX case. Selftest change is necessary to accommodate this.

    Fixes: 91cc1a9974 ("bpf: Annotate context types")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240212233221.2575350-4-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:10 +02:00
Jerome Marchand 1aa24db87d bpf: handle bpf_user_pt_regs_t typedef explicitly for PTR_TO_CTX global arg
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 824c58fb1090ae5e502284400682e30841280a87
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Feb 12 15:32:19 2024 -0800

    bpf: handle bpf_user_pt_regs_t typedef explicitly for PTR_TO_CTX global arg

    Expected canonical argument type for global function arguments
    representing PTR_TO_CTX is `bpf_user_pt_regs_t *ctx`. This currently
    works on s390x by accident because kernel resolves such typedef to
    underlying struct (which is anonymous on s390x), and erroneously
    accepting it as expected context type. We are fixing this problem next,
    which would break s390x arch, so we need to handle `bpf_user_pt_regs_t`
    case explicitly for KPROBE programs.

    Fixes: 91cc1a9974 ("bpf: Annotate context types")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240212233221.2575350-3-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:10 +02:00
Jerome Marchand 25bc3df1a1 bpf: simplify btf_get_prog_ctx_type() into btf_is_prog_ctx_type()
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit fb5b86cfd4ef21ea18966718f6bf6c8f1b9df12e
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Feb 12 15:32:18 2024 -0800

    bpf: simplify btf_get_prog_ctx_type() into btf_is_prog_ctx_type()

    Return result of btf_get_prog_ctx_type() is never used and callers only
    check NULL vs non-NULL case to determine if given type matches expected
    PTR_TO_CTX type. So rename function to `btf_is_prog_ctx_type()` and
    return a simple true/false. We'll use this simpler interface to handle
    kprobe program type's special typedef case in the next patch.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240212233221.2575350-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:10 +02:00
Jerome Marchand e56e6923fd bpf: Create argument information for nullable arguments.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 1611603537a4b88cec7993f32b70c03113801a46
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu Feb 8 18:37:49 2024 -0800

    bpf: Create argument information for nullable arguments.

    Collect argument information from the type information of stub functions to
    mark arguments of BPF struct_ops programs with PTR_MAYBE_NULL if they are
    nullable.  A nullable argument is annotated by suffixing "__nullable" at
    the argument name of stub function.

    For nullable arguments, this patch sets a struct bpf_ctx_arg_aux to label
    their reg_type with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This
    makes the verifier to check programs and ensure that they properly check
    the pointer. The programs should check if the pointer is null before
    accessing the pointed memory.

    The implementer of a struct_ops type should annotate the arguments that can
    be null. The implementer should define a stub function (empty) as a
    placeholder for each defined operator. The name of a stub function should
    be in the pattern "<st_op_type>__<operator name>". For example, for
    test_maybe_null of struct bpf_testmod_ops, it's stub function name should
    be "bpf_testmod_ops__test_maybe_null". You mark an argument nullable by
    suffixing the argument name with "__nullable" at the stub function.

    Since we already has stub functions for kCFI, we just reuse these stub
    functions with the naming convention mentioned earlier. These stub
    functions with the naming convention is only required if there are nullable
    arguments to annotate. For functions having not nullable arguments, stub
    functions are not necessary for the purpose of this patch.

    This patch will prepare a list of struct bpf_ctx_arg_aux, aka arg_info, for
    each member field of a struct_ops type.  "arg_info" will be assigned to
    "prog->aux->ctx_arg_info" of BPF struct_ops programs in
    check_struct_ops_btf_id() so that it can be used by btf_ctx_access() later
    to set reg_type properly for the verifier.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240209023750.1153905-4-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand 30fcd59f60 bpf: Move __kfunc_param_match_suffix() to btf.c.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 6115a0aeef01aef152ad7738393aad11422bfb82
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu Feb 8 18:37:48 2024 -0800

    bpf: Move __kfunc_param_match_suffix() to btf.c.

    Move __kfunc_param_match_suffix() to btf.c and rename it as
    btf_param_match_suffix(). It can be reused by bpf_struct_ops later.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240209023750.1153905-3-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand bfa533ab5b bpf: add btf pointer to struct bpf_ctx_arg_aux.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 77c0208e199ccb0986fb3612f2409c8cdcb036ad
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Thu Feb 8 18:37:47 2024 -0800

    bpf: add btf pointer to struct bpf_ctx_arg_aux.

    Enable the providers to use types defined in a module instead of in the
    kernel (btf_vmlinux).

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240209023750.1153905-2-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand 8e36bb244a bpf, btf: Check btf for register_bpf_struct_ops
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 947e56f82fd783a1ec1c9359b20b5699d09cae14
Author: Geliang Tang <tanggeliang@kylinos.cn>
Date:   Thu Feb 8 14:24:23 2024 +0800

    bpf, btf: Check btf for register_bpf_struct_ops

    Similar to the handling in the functions __register_btf_kfunc_id_set()
    and register_btf_id_dtor_kfuncs(), this patch uses the newly added
    helper check_btf_kconfigs() to handle module with its btf section
    stripped.

    While at it, the patch also adds the missed IS_ERR() check to fix the
    commit f6be98d19985 ("bpf, net: switch to dynamic registration")

    Fixes: f6be98d19985 ("bpf, net: switch to dynamic registration")
    Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
    Link: https://lore.kernel.org/r/69082b9835463fe36f9e354bddf2d0a97df39c2b.1707373307.git.tanggeliang@kylinos.cn
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand dc7a6ee8d2 bpf, btf: Add check_btf_kconfigs helper
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 9e60b0e02550aaf5f2301e49353641a5e3701674
Author: Geliang Tang <tanggeliang@kylinos.cn>
Date:   Thu Feb 8 14:24:22 2024 +0800

    bpf, btf: Add check_btf_kconfigs helper

    This patch extracts duplicate code on error path when btf_get_module_btf()
    returns NULL from the functions __register_btf_kfunc_id_set() and
    register_btf_id_dtor_kfuncs() into a new helper named check_btf_kconfigs()
    to check CONFIG_DEBUG_INFO_BTF and CONFIG_DEBUG_INFO_BTF_MODULES in it.

    Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/fa5537fc55f1e4d0bfd686598c81b7ab9dbd82b7.1707373307.git.tanggeliang@kylinos.cn
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand 722b02d5f2 bpf, btf: Fix return value of register_btf_id_dtor_kfuncs
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit b9a395f0f7af66fe8224450481b99d4f83b57207
Author: Geliang Tang <tanggeliang@kylinos.cn>
Date:   Thu Feb 8 14:24:21 2024 +0800

    bpf, btf: Fix return value of register_btf_id_dtor_kfuncs

    The same as __register_btf_kfunc_id_set(), to let the modules with
    stripped btf section loaded, this patch changes the return value of
    register_btf_id_dtor_kfuncs() too from -ENOENT to 0 when btf is NULL.

    Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
    Link: https://lore.kernel.org/r/eab65586d7fb0e72f2707d3747c7d4a5d60c823f.1707373307.git.tanggeliang@kylinos.cn
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:09 +02:00
Jerome Marchand 563e3eb7e7 bpf: treewide: Annotate BPF kfuncs in BTF
JIRA: https://issues.redhat.com/browse/RHEL-23649

Conflicts: Multiple conflicts due to missing kfuncs. All sections were
switched to use the new macro except bpf_mptcp_fmodret_ids which still
use BTF_SET8_* upstream. I don't know why. That might be an upstream
oversight.

commit 6f3189f38a3e995232e028a4c341164c4aca1b20
Author: Daniel Xu <dxu@dxuuu.xyz>
Date:   Sun Jan 28 18:24:08 2024 -0700

    bpf: treewide: Annotate BPF kfuncs in BTF

    This commit marks kfuncs as such inside the .BTF_ids section. The upshot
    of these annotations is that we'll be able to automatically generate
    kfunc prototypes for downstream users. The process is as follows:

    1. In source, use BTF_KFUNCS_START/END macro pair to mark kfuncs
    2. During build, pahole injects into BTF a "bpf_kfunc" BTF_DECL_TAG for
       each function inside BTF_KFUNCS sets
    3. At runtime, vmlinux or module BTF is made available in sysfs
    4. At runtime, bpftool (or similar) can look at provided BTF and
       generate appropriate prototypes for functions with "bpf_kfunc" tag

    To ensure future kfunc are similarly tagged, we now also return error
    inside kfunc registration for untagged kfuncs. For vmlinux kfuncs,
    we also WARN(), as initcall machinery does not handle errors.

    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Acked-by: Benjamin Tissoires <bentiss@kernel.org>
    Link: https://lore.kernel.org/r/e55150ceecbf0a5d961e608941165c0bee7bc943.1706491398.git.dxu@dxuuu.xyz
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:07 +02:00
Jerome Marchand b640b1df5c bpf: add arg:nullable tag to be combined with trusted pointers
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 8f2b44cd9d69ec36c9ce9623993978babb575ee8
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jan 29 16:06:46 2024 -0800

    bpf: add arg:nullable tag to be combined with trusted pointers

    Add ability to mark arg:trusted arguments with optional arg:nullable
    tag to mark it as PTR_TO_BTF_ID_OR_NULL variant, which will allow
    callers to pass NULL, and subsequently will force global subprog's code
    to do NULL check. This allows to have "optional" PTR_TO_BTF_ID values
    passed into global subprogs.

    For now arg:nullable cannot be combined with anything else.

    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240130000648.2144827-3-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:06 +02:00
Jerome Marchand 6258d980a2 bpf: add __arg_trusted global func arg tag
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit e2b3c4ff5d183da6d1863c2321413406a2752e7a
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Mon Jan 29 16:06:45 2024 -0800

    bpf: add __arg_trusted global func arg tag

    Add support for passing PTR_TO_BTF_ID registers to global subprogs.
    Currently only PTR_TRUSTED flavor of PTR_TO_BTF_ID is supported.
    Non-NULL semantics is assumed, so caller will be forced to prove
    PTR_TO_BTF_ID can't be NULL.

    Note, we disallow global subprogs to destroy passed in PTR_TO_BTF_ID
    arguments, even the trusted one. We achieve that by not setting
    ref_obj_id when validating subprog code. This basically enforces (in
    Rust terms) borrowing semantics vs move semantics. Borrowing semantics
    seems to be a better fit for isolated global subprog validation
    approach.

    Implementation-wise, we utilize existing logic for matching
    user-provided BTF type to kernel-side BTF type, used by BPF CO-RE logic
    and following same matching rules. We enforce a unique match for types.

    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240130000648.2144827-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:06 +02:00
Jerome Marchand 9cb5a54a05 bpf: move arg:ctx type enforcement check inside the main logic loop
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit add9c58cd44e88a15f285945e26bf0d9d81c5890
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Jan 25 12:55:06 2024 -0800

    bpf: move arg:ctx type enforcement check inside the main logic loop

    Now that bpf and bpf-next trees converged and we don't run the risk of
    merge conflicts, move btf_validate_prog_ctx_type() into its most logical
    place inside the main logic loop.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240125205510.3642094-4-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:05 +02:00
Jerome Marchand 1f6e29a1c7 bpf: export btf_ctx_access to modules.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 7c81c2490c73e614c6d48e4f339f4f224140b565
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Fri Jan 19 14:50:04 2024 -0800

    bpf: export btf_ctx_access to modules.

    The module requires the use of btf_ctx_access() to invoke
    bpf_tracing_btf_ctx_access() from a module. This function is valuable for
    implementing validation functions that ensure proper access to ctx.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240119225005.668602-14-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:02 +02:00
Jerome Marchand be602d71dd bpf, net: switch to dynamic registration
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit f6be98d19985411ca1f3d53413d94d5b7f41c200
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Fri Jan 19 14:50:02 2024 -0800

    bpf, net: switch to dynamic registration

    Replace the static list of struct_ops types with per-btf struct_ops_tab to
    enable dynamic registration.

    Both bpf_dummy_ops and bpf_tcp_ca now utilize the registration function
    instead of being listed in bpf_struct_ops_types.h.

    Cc: netdev@vger.kernel.org
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240119225005.668602-12-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:02 +02:00
Jerome Marchand f73d128215 bpf: add struct_ops_tab to btf.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit e61995111a76633376419d1bccede8696e94e6e5
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Fri Jan 19 14:49:55 2024 -0800

    bpf: add struct_ops_tab to btf.

    Maintain a registry of registered struct_ops types in the per-btf (module)
    struct_ops_tab. This registry allows for easy lookup of struct_ops types
    that are registered by a specific module.

    It is a preparation work for supporting kernel module struct_ops in a
    latter patch. Each struct_ops will be registered under its own kernel
    module btf and will be stored in the newly added btf->struct_ops_tab. The
    bpf verifier and bpf syscall (e.g. prog and map cmd) can find the
    struct_ops and its btf type/size/id... information from
    btf->struct_ops_tab.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240119225005.668602-5-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:01 +02:00
Jerome Marchand 538837b742 bpf: refactory struct_ops type initialization to a function.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 3b1f89e747cd4b24244f2798a35d28815b744303
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Fri Jan 19 14:49:52 2024 -0800

    bpf: refactory struct_ops type initialization to a function.

    Move the majority of the code to bpf_struct_ops_init_one(), which can then
    be utilized for the initialization of newly registered dynamically
    allocated struct_ops types in the following patches.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240119225005.668602-2-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:01 +02:00
Jerome Marchand 4022599fc8 bpf: support multiple tags per argument
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 522bb2c1f82b12eb7befaae815d1d959b8e6bba2
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Jan 4 16:09:05 2024 -0800

    bpf: support multiple tags per argument

    Add ability to iterate multiple decl_tag types pointed to the same
    function argument. Use this to support multiple __arg_xxx tags per
    global subprog argument.

    We leave btf_find_decl_tag_value() intact, but change its implementation
    to use a new btf_find_next_decl_tag() which can be straightforwardly
    used to find next BTF type ID of a matching btf_decl_tag type.
    btf_prepare_func_args() is switched from btf_find_decl_tag_value() to
    btf_find_next_decl_tag() to gain multiple tags per argument support.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240105000909.2818934-5-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:48:59 +02:00