Commit Graph

19 Commits

Author SHA1 Message Date
Viktor Malik c0905ae221
bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops.
JIRA: https://issues.redhat.com/browse/RHEL-30774

commit 73287fe228721b05690e671adbcccc6cf5435be6
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Wed May 29 23:59:39 2024 -0700

    bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops.
    
    Pass an additional pointer of bpf_struct_ops_link to callback function reg,
    unreg, and update provided by subsystems defined in bpf_struct_ops. A
    bpf_struct_ops_map can be registered for multiple links. Passing a pointer
    of bpf_struct_ops_link helps subsystems to distinguish them.
    
    This pointer will be used in the later patches to let the subsystem
    initiate a detachment on a link that was registered to it previously.
    
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240530065946.979330-2-thinker.li@gmail.com
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-26 14:40:01 +01:00
Viktor Malik a1b5a2aabc
bpf: use type_may_be_null() helper for nullable-param check
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 1ae497c78f01855f3695b58481311ffdd429b028
Author: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Date:   Thu Sep 5 13:52:32 2024 +0800

    bpf: use type_may_be_null() helper for nullable-param check
    
    Commit 980ca8ceeae6 ("bpf: check bpf_dummy_struct_ops program params for
    test runs") does bitwise AND between reg_type and PTR_MAYBE_NULL, which
    is correct, but due to type difference the compiler complains:
    
      net/bpf/bpf_dummy_struct_ops.c:118:31: warning: bitwise operation between different enumeration types ('const enum bpf_reg_type' and 'enum bpf_type_flag') [-Wenum-enum-conversion]
        118 |                 if (info && (info->reg_type & PTR_MAYBE_NULL))
            |                              ~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~
    
    Workaround the warning by moving the type_may_be_null() helper from
    verifier.c into bpf_verifier.h, and reuse it here to check whether param
    is nullable.
    
    Fixes: 980ca8ceeae6 ("bpf: check bpf_dummy_struct_ops program params for test runs")
    Reported-by: kernel test robot <lkp@intel.com>
    Closes: https://lore.kernel.org/oe-kbuild-all/202404241956.HEiRYwWq-lkp@intel.com/
    Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240905055233.70203-1-shung-hsi.yu@suse.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-19 07:40:49 +01:00
Viktor Malik 3c0d24cb7a
bpf: check bpf_dummy_struct_ops program params for test runs
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 980ca8ceeae69ddf362870ea9183f389ae26324a
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Tue Apr 23 18:28:20 2024 -0700

    bpf: check bpf_dummy_struct_ops program params for test runs
    
    When doing BPF_PROG_TEST_RUN for bpf_dummy_struct_ops programs,
    reject execution when NULL is passed for non-nullable params.
    For programs with non-nullable params verifier assumes that
    such params are never NULL and thus might optimize out NULL checks.
    
    Suggested-by: Kui-Feng Lee <sinquersw@gmail.com>
    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240424012821.595216-5-eddyz87@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:53 +01:00
Viktor Malik 3486e1d057
bpf: mark bpf_dummy_struct_ops.test_1 parameter as nullable
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 1479eaff1f16983d8fda7c5a08a586c21891087d
Author: Eduard Zingerman <eddyz87@gmail.com>
Date:   Tue Apr 23 18:28:17 2024 -0700

    bpf: mark bpf_dummy_struct_ops.test_1 parameter as nullable
    
    Test case dummy_st_ops/dummy_init_ret_value passes NULL as the first
    parameter of the test_1() function. Mark this parameter as nullable to
    make verifier aware of such possibility.
    Otherwise, NULL check in the test_1() code:
    
          SEC("struct_ops/test_1")
          int BPF_PROG(test_1, struct bpf_dummy_ops_state *state)
          {
                if (!state)
                        return ...;
    
                ... access state ...
          }
    
    Might be removed by verifier, thus triggering NULL pointer dereference
    under certain conditions.
    
    Reported-by: Jose E. Marchesi <jemarch@gnu.org>
    Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
    Link: https://lore.kernel.org/r/20240424012821.595216-2-eddyz87@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:52 +01:00
Viktor Malik 6985f7b84f
bpf: Check return from set_memory_rox()
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit c733239f8f530872a1f80d8c45dcafbaff368737
Author: Christophe Leroy <christophe.leroy@csgroup.eu>
Date:   Sat Mar 16 08:35:41 2024 +0100

    bpf: Check return from set_memory_rox()
    
    arch_protect_bpf_trampoline() and alloc_new_pack() call
    set_memory_rox() which can fail, leading to unprotected memory.
    
    Take into account return from set_memory_rox() function and add
    __must_check flag to arch_protect_bpf_trampoline().
    
    Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
    Reviewed-by: Kees Cook <keescook@chromium.org>
    Link: https://lore.kernel.org/r/fe1c163c83767fde5cab31d209a4a6be3ddb3a73.1710574353.git.christophe.leroy@csgroup.eu
    Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:29 +01:00
Jerome Marchand 0803f1f621 bpf: move sleepable flag from bpf_prog_aux to bpf_prog
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 66c8473135c62f478301a0e5b3012f203562dfa6
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Fri Mar 8 16:47:39 2024 -0800

    bpf: move sleepable flag from bpf_prog_aux to bpf_prog

    prog->aux->sleepable is checked very frequently as part of (some) BPF
    program run hot paths. So this extra aux indirection seems wasteful and
    on busy systems might cause unnecessary memory cache misses.

    Let's move sleepable flag into prog itself to eliminate unnecessary
    pointer dereference.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Message-ID: <20240309004739.2961431-1-andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:16 +02:00
Jerome Marchand ef2edd603f bpf: struct_ops supports more than one page for trampolines.
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 187e2af05abe6bf80581490239c449456627d17a
Author: Kui-Feng Lee <thinker.li@gmail.com>
Date:   Sat Feb 24 14:34:17 2024 -0800

    bpf: struct_ops supports more than one page for trampolines.

    The BPF struct_ops previously only allowed one page of trampolines.
    Each function pointer of a struct_ops is implemented by a struct_ops
    bpf program. Each struct_ops bpf program requires a trampoline.
    The following selftest patch shows each page can hold a little more
    than 20 trampolines.

    While one page is more than enough for the tcp-cc usecase,
    the sched_ext use case shows that one page is not always enough and hits
    the one page limit. This patch overcomes the one page limit by allocating
    another page when needed and it is limited to a total of
    MAX_IMAGE_PAGES (8) pages which is more than enough for
    reasonable usages.

    The variable st_map->image has been changed to st_map->image_pages, and
    its type has been changed to an array of pointers to pages.

    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240224223418.526631-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:12 +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 1df8e22024 bpf, net: introduce bpf_struct_ops_desc.
JIRA: https://issues.redhat.com/browse/RHEL-23649

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

    bpf, net: introduce bpf_struct_ops_desc.

    Move some of members of bpf_struct_ops to bpf_struct_ops_desc.  type_id is
    unavailabe in bpf_struct_ops anymore. Modules should get it from the btf
    received by kmod's init function.

    Cc: netdev@vger.kernel.org
    Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
    Link: https://lore.kernel.org/r/20240119225005.668602-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:01 +02:00
Viktor Malik dfc3259578
x86/cfi,bpf: Fix bpf_struct_ops CFI
JIRA: https://issues.redhat.com/browse/RHEL-23644

Omitted-fix: 1732ebc4a261 ("riscv, bpf: Fix unpredictable kernel crash about RV64 struct_ops")
             Unsupported arch.

commit 2cd3e3772e41377f32d6eea643e0590774e9187c
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Fri Dec 15 10:12:20 2023 +0100

    x86/cfi,bpf: Fix bpf_struct_ops CFI

    BPF struct_ops uses __arch_prepare_bpf_trampoline() to write
    trampolines for indirect function calls. These tramplines much have
    matching CFI.

    In order to obtain the correct CFI hash for the various methods, add a
    matching structure that contains stub functions, the compiler will
    generate correct CFI which we can pilfer for the trampolines.

    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lore.kernel.org/r/20231215092707.566977112@infradead.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:31 +02:00
Viktor Malik dd807f7b47
bpf: Add helpers for trampoline image management
JIRA: https://issues.redhat.com/browse/RHEL-23644

Conflicts: replaces code containing a conflict introduced by
           1206412454 ("bpf: Create links for BPF struct_ops maps.")
           due to then-missing upstream commit d48567c9a0d1 ("mm:
           Introduce set_memory_rox()").

commit 82583daa2efc2e336962b231a46bad03a280b3e0
Author: Song Liu <song@kernel.org>
Date:   Wed Dec 6 14:40:50 2023 -0800

    bpf: Add helpers for trampoline image management

    As BPF trampoline of different archs moves from bpf_jit_[alloc|free]_exec()
    to bpf_prog_pack_[alloc|free](), we need to use different _alloc, _free for
    different archs during the transition. Add the following helpers for this
    transition:

    void *arch_alloc_bpf_trampoline(unsigned int size);
    void arch_free_bpf_trampoline(void *image, unsigned int size);
    void arch_protect_bpf_trampoline(void *image, unsigned int size);
    void arch_unprotect_bpf_trampoline(void *image, unsigned int size);

    The fallback version of these helpers require size <= PAGE_SIZE, but they
    are only called with size == PAGE_SIZE. They will be called with size <
    PAGE_SIZE when arch_bpf_trampoline_size() helper is introduced later.

    Signed-off-by: Song Liu <song@kernel.org>
    Acked-by: Ilya Leoshkevich <iii@linux.ibm.com>
    Tested-by: Ilya Leoshkevich <iii@linux.ibm.com>  # on s390x
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20231206224054.492250-4-song@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:14 +02:00
Prarit Bhargava b79789281d mm: Introduce set_memory_rox()
JIRA: https://issues.redhat.com/browse/RHEL-25415

Conflicts: Minor drift issues, and not worried about unsupported arches.
Changes to arch/arm/mach-omap[12] are made in arch/arm/plat-omap which
is unified in RHEL9.

commit d48567c9a0d1e605639f8a8705a61bbb55fb4e84
Author: Peter Zijlstra <peterz@infradead.org>
Date:   Wed Oct 26 12:13:03 2022 +0200

    mm: Introduce set_memory_rox()

    Because endlessly repeating:

            set_memory_ro()
            set_memory_x()

    is getting tedious.

    Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Link: https://lkml.kernel.org/r/Y1jek64pXOsougmz@hirez.programming.kicks-ass.net

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
2024-03-20 09:42:51 -04:00
Artem Savkov 82a51a691b bpf: Remove unused arguments from btf_struct_access().
Bugzilla: https://bugzilla.redhat.com/2221599

commit b7e852a9ec96635168c04204fb7cf1f7390b9a8c
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Apr 3 21:50:23 2023 -0700

    bpf: Remove unused arguments from btf_struct_access().
    
    Remove unused arguments from btf_struct_access() callback.
    
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/bpf/20230404045029.82870-3-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:26 +02:00
Artem Savkov c3875c666d bpf: Invoke btf_struct_access() callback only for writes.
Bugzilla: https://bugzilla.redhat.com/2221599

commit 7d64c513284408fee5178a0953a686e9410f2399
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Apr 3 21:50:22 2023 -0700

    bpf: Invoke btf_struct_access() callback only for writes.
    
    Remove duplicated if (atype == BPF_READ) btf_struct_access() from
    btf_struct_access() callback and invoke it only for writes. This is
    possible to do because currently btf_struct_access() custom callback
    always delegates to generic btf_struct_access() helper for BPF_READ
    accesses.
    
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/bpf/20230404045029.82870-2-alexei.starovoitov@gmail.com

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2023-09-22 09:12:25 +02:00
Viktor Malik d2c8b170e7 bpf/selftests: Verify struct_ops prog sleepable behavior
Bugzilla: https://bugzilla.redhat.com/2178930

commit 7dd880592a88799f3ef48fda507849a75f11cbf0
Author: David Vernet <void@manifault.com>
Date:   Wed Jan 25 10:47:35 2023 -0600

    bpf/selftests: Verify struct_ops prog sleepable behavior
    
    In a set of prior changes, we added the ability for struct_ops programs
    to be sleepable. This patch enhances the dummy_st_ops selftest suite to
    validate this behavior by adding a new sleepable struct_ops entry to
    dummy_st_ops.
    
    Signed-off-by: David Vernet <void@manifault.com>
    Link: https://lore.kernel.org/r/20230125164735.785732-5-void@manifault.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2023-06-13 22:44:53 +02:00
Jerome Marchand 39fc2fdfd6 bpf: Refactor btf_struct_access
Bugzilla: https://bugzilla.redhat.com/2177177

commit 6728aea7216c0c06c98e2e58d753a5e8b2ae1c6f
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Tue Nov 15 00:45:28 2022 +0530

    bpf: Refactor btf_struct_access

    Instead of having to pass multiple arguments that describe the register,
    pass the bpf_reg_state into the btf_struct_access callback. Currently,
    all call sites simply reuse the btf and btf_id of the reg they want to
    check the access of. The only exception to this pattern is the callsite
    in check_ptr_to_map_access, hence for that case create a dummy reg to
    simulate PTR_TO_BTF_ID access.

    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20221114191547.1694267-8-memxor@gmail.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-04-28 11:43:04 +02:00
Yauheni Kaliuta 503bec2387 bpf, x86: Generate trampolines from bpf_tramp_links
Bugzilla: https://bugzilla.redhat.com/2120968
Conflicts: already applied
  1d5f82d9dd47 ("bpf, x86: fix freeing of not-finalized bpf_prog_pack")

commit f7e0beaf39d3868dc700d4954b26cf8443c5d423
Author: Kui-Feng Lee <kuifeng@fb.com>
Date:   Tue May 10 13:59:19 2022 -0700

    bpf, x86: Generate trampolines from bpf_tramp_links

    Replace struct bpf_tramp_progs with struct bpf_tramp_links to collect
    struct bpf_tramp_link(s) for a trampoline.  struct bpf_tramp_link
    extends bpf_link to act as a linked list node.

    arch_prepare_bpf_trampoline() accepts a struct bpf_tramp_links to
    collects all bpf_tramp_link(s) that a trampoline should call.

    Change BPF trampoline and bpf_struct_ops to pass bpf_tramp_links
    instead of bpf_tramp_progs.

    Signed-off-by: Kui-Feng Lee <kuifeng@fb.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220510205923.3206889-2-kuifeng@fb.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-11-30 12:47:03 +02:00
Jerome Marchand 7b7955f647 bpf: reject program if a __user tagged memory accessed in kernel way
Bugzilla: https://bugzilla.redhat.com/2120966

commit c6f1bfe89ac95dc829dcb4ed54780da134ac5fce
Author: Yonghong Song <yhs@fb.com>
Date:   Thu Jan 27 07:46:06 2022 -0800

    bpf: reject program if a __user tagged memory accessed in kernel way

    BPF verifier supports direct memory access for BPF_PROG_TYPE_TRACING type
    of bpf programs, e.g., a->b. If "a" is a pointer
    pointing to kernel memory, bpf verifier will allow user to write
    code in C like a->b and the verifier will translate it to a kernel
    load properly. If "a" is a pointer to user memory, it is expected
    that bpf developer should be bpf_probe_read_user() helper to
    get the value a->b. Without utilizing BTF __user tagging information,
    current verifier will assume that a->b is a kernel memory access
    and this may generate incorrect result.

    Now BTF contains __user information, it can check whether the
    pointer points to a user memory or not. If it is, the verifier
    can reject the program and force users to use bpf_probe_read_user()
    helper explicitly.

    In the future, we can easily extend btf_add_space for other
    address space tagging, for example, rcu/percpu etc.

    Signed-off-by: Yonghong Song <yhs@fb.com>
    Link: https://lore.kernel.org/r/20220127154606.654961-1-yhs@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:44 +02:00
Yauheni Kaliuta 84f284d078 bpf: Add dummy BPF STRUCT_OPS for test purpose
Bugzilla: http://bugzilla.redhat.com/2069045

commit c196906d50e360d82ed9aa5596a9d0ce89b7ab78
Author: Hou Tao <houtao1@huawei.com>
Date:   Mon Oct 25 14:40:24 2021 +0800

    bpf: Add dummy BPF STRUCT_OPS for test purpose
    
    Currently the test of BPF STRUCT_OPS depends on the specific bpf
    implementation of tcp_congestion_ops, but it can not cover all
    basic functionalities (e.g, return value handling), so introduce
    a dummy BPF STRUCT_OPS for test purpose.
    
    Loading a bpf_dummy_ops implementation from userspace is prohibited,
    and its only purpose is to run BPF_PROG_TYPE_STRUCT_OPS program
    through bpf(BPF_PROG_TEST_RUN). Now programs for test_1() & test_2()
    are supported. The following three cases are exercised in
    bpf_dummy_struct_ops_test_run():
    
    (1) test and check the value returned from state arg in test_1(state)
    The content of state is copied from userspace pointer and copied back
    after calling test_1(state). The user pointer is saved in an u64 array
    and the array address is passed through ctx_in.
    
    (2) test and check the return value of test_1(NULL)
    Just simulate the case in which an invalid input argument is passed in.
    
    (3) test multiple arguments passing in test_2(state, ...)
    5 arguments are passed through ctx_in in form of u64 array. The first
    element of array is userspace pointer of state and others 4 arguments
    follow.
    
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20211025064025.2567443-4-houtao1@huawei.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:49 +03:00