Commit Graph

355 Commits

Author SHA1 Message Date
Jerome Marchand e89d6fd8b8 bpf,perf: Fix invalid prog_array access in perf_event_detach_bpf_prog
JIRA: https://issues.redhat.com/browse/RHEL-63880

Conflicts: Context change due to missing commit ef1b808e3b7c ("bpf:
Fix UAF via mismatching bpf_prog/attachment RCU flavors")

commit 978c4486cca5c7b9253d3ab98a88c8e769cb9bbd
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Sun Dec 8 15:25:07 2024 +0100

    bpf,perf: Fix invalid prog_array access in perf_event_detach_bpf_prog

    Syzbot reported [1] crash that happens for following tracing scenario:

      - create tracepoint perf event with attr.inherit=1, attach it to the
        process and set bpf program to it
      - attached process forks -> chid creates inherited event

        the new child event shares the parent's bpf program and tp_event
        (hence prog_array) which is global for tracepoint

      - exit both process and its child -> release both events
      - first perf_event_detach_bpf_prog call will release tp_event->prog_array
        and second perf_event_detach_bpf_prog will crash, because
        tp_event->prog_array is NULL

    The fix makes sure the perf_event_detach_bpf_prog checks prog_array
    is valid before it tries to remove the bpf program from it.

    [1] https://lore.kernel.org/bpf/Z1MR6dCIKajNS6nU@krava/T/#m91dbf0688221ec7a7fc95e896a7ef9ff93b0b8ad

    Fixes: 0ee288e69d03 ("bpf,perf: Fix perf_event_detach_bpf_prog error handling")
    Reported-by: syzbot+2e0d2840414ce817aaac@syzkaller.appspotmail.com
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20241208142507.1207698-1-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:09 +01:00
Jerome Marchand 4ca838bdf9 bpf,perf: Fix perf_event_detach_bpf_prog error handling
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 0ee288e69d033850bc87abe0f9cc3ada24763d7f
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Oct 23 22:03:52 2024 +0200

    bpf,perf: Fix perf_event_detach_bpf_prog error handling

    Peter reported that perf_event_detach_bpf_prog might skip to release
    the bpf program for -ENOENT error from bpf_prog_array_copy.

    This can't happen because bpf program is stored in perf event and is
    detached and released only when perf event is freed.

    Let's drop the -ENOENT check and make sure the bpf program is released
    in any case.

    Fixes: 170a7e3ea0 ("bpf: bpf_prog_array_copy() should return -ENOENT if exclude_prog not found")
    Reported-by: Peter Zijlstra <peterz@infradead.org>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20241023200352.3488610-1-jolsa@kernel.org

    Closes: https://lore.kernel.org/lkml/20241022111638.GC16066@noisy.programming.kicks-ass.net/

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:09 +01:00
Jerome Marchand 4275e2f620 bpf: Add MEM_WRITE attribute
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 6fad274f06f038c29660aa53fbad14241c9fd976
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Mon Oct 21 17:28:05 2024 +0200

    bpf: Add MEM_WRITE attribute

    Add a MEM_WRITE attribute for BPF helper functions which can be used in
    bpf_func_proto to annotate an argument type in order to let the verifier
    know that the helper writes into the memory passed as an argument. In
    the past MEM_UNINIT has been (ab)used for this function, but the latter
    merely tells the verifier that the passed memory can be uninitialized.

    There have been bugs with overloading the latter but aside from that
    there are also cases where the passed memory is read + written which
    currently cannot be expressed, see also 4b3786a6c539 ("bpf: Zero former
    ARG_PTR_TO_{LONG,INT} args in case of error").

    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Link: https://lore.kernel.org/r/20241021152809.33343-1-daniel@iogearbox.net
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:08 +01:00
Jerome Marchand df344c9e50 bpf: Fix unpopulated path_size when uprobe_multi fields unset
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit ad6b5b6ea9b764018249285a4fe0a2226bef4caa
Author: Tyrone Wu <wudevelops@gmail.com>
Date:   Fri Oct 11 00:08:02 2024 +0000

    bpf: Fix unpopulated path_size when uprobe_multi fields unset

    Previously when retrieving `bpf_link_info.uprobe_multi` with `path` and
    `path_size` fields unset, the `path_size` field is not populated
    (remains 0). This behavior was inconsistent with how other input/output
    string buffer fields work, as the field should be populated in cases
    when:
    - both buffer and length are set (currently works as expected)
    - both buffer and length are unset (not working as expected)

    This patch now fills the `path_size` field when `path` and `path_size`
    are unset.

    Fixes: e56fdbfb06e2 ("bpf: Add link_info support for uprobe multi link")
    Signed-off-by: Tyrone Wu <wudevelops@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20241011000803.681190-1-wudevelops@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:08 +01:00
Jerome Marchand e12894e8b8 bpf: Fix helper writes to read-only maps
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 32556ce93bc45c730829083cb60f95a2728ea48b
Author: Daniel Borkmann <daniel@iogearbox.net>
Date:   Fri Sep 13 21:17:48 2024 +0200

    bpf: Fix helper writes to read-only maps

    Lonial found an issue that despite user- and BPF-side frozen BPF map
    (like in case of .rodata), it was still possible to write into it from
    a BPF program side through specific helpers having ARG_PTR_TO_{LONG,INT}
    as arguments.

    In check_func_arg() when the argument is as mentioned, the meta->raw_mode
    is never set. Later, check_helper_mem_access(), under the case of
    PTR_TO_MAP_VALUE as register base type, it assumes BPF_READ for the
    subsequent call to check_map_access_type() and given the BPF map is
    read-only it succeeds.

    The helpers really need to be annotated as ARG_PTR_TO_{LONG,INT} | MEM_UNINIT
    when results are written into them as opposed to read out of them. The
    latter indicates that it's okay to pass a pointer to uninitialized memory
    as the memory is written to anyway.

    However, ARG_PTR_TO_{LONG,INT} is a special case of ARG_PTR_TO_FIXED_SIZE_MEM
    just with additional alignment requirement. So it is better to just get
    rid of the ARG_PTR_TO_{LONG,INT} special cases altogether and reuse the
    fixed size memory types. For this, add MEM_ALIGNED to additionally ensure
    alignment given these helpers write directly into the args via *<ptr> = val.
    The .arg*_size has been initialized reflecting the actual sizeof(*<ptr>).

    MEM_ALIGNED can only be used in combination with MEM_FIXED_SIZE annotated
    argument types, since in !MEM_FIXED_SIZE cases the verifier does not know
    the buffer size a priori and therefore cannot blindly write *<ptr> = val.

    Fixes: 57c3bb725a ("bpf: Introduce ARG_PTR_TO_{INT,LONG} arg types")
    Reported-by: Lonial Con <kongln9170@gmail.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
    Link: https://lore.kernel.org/r/20240913191754.13290-3-daniel@iogearbox.net
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:06 +01:00
Jerome Marchand 9d3ea5ae64 bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit d4dd9775ec242425576af93daadb80a34083a53c
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Aug 29 10:42:31 2024 -0700

    bpf: wire up sleepable bpf_get_stack() and bpf_get_task_stack() helpers

    Add sleepable implementations of bpf_get_stack() and
    bpf_get_task_stack() helpers and allow them to be used from sleepable
    BPF program (e.g., sleepable uprobes).

    Note, the stack trace IPs capturing itself is not sleepable (that would
    need to be a separate project), only build ID fetching is sleepable and
    thus more reliable, as it will wait for data to be paged in, if
    necessary. For that we make use of sleepable build_id_parse()
    implementation.

    Now that build ID related internals in kernel/bpf/stackmap.c can be used
    both in sleepable and non-sleepable contexts, we need to add additional
    rcu_read_lock()/rcu_read_unlock() protection around fetching
    perf_callchain_entry, but with the refactoring in previous commit it's
    now pretty straightforward. We make sure to do rcu_read_unlock (in
    sleepable mode only) right before stack_map_get_build_id_offset() call
    which can sleep. By that time we don't have any more use of
    perf_callchain_entry.

    Note, bpf_get_task_stack() will fail for user mode if task != current.
    And for kernel mode build ID are irrelevant. So in that sense adding
    sleepable bpf_get_task_stack() implementation is a no-op. It feel right
    to wire this up for symmetry and completeness, but I'm open to just
    dropping it until we support `user && crosstask` condition.

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:05 +01:00
Jerome Marchand 3bb54e2c72 bpf: Fix uprobe multi pid filter check
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 900f362e2062e4a23a53bf89fd3b248cd021f5af
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Thu Sep 5 14:51:21 2024 +0300

    bpf: Fix uprobe multi pid filter check

    Uprobe multi link does its own process (thread leader) filtering before
    running the bpf program by comparing task's vm pointers.

    But as Oleg pointed out there can be processes sharing the vm (CLONE_VM),
    so we can't just compare task->vm pointers, but instead we need to use
    same_thread_group call.

    Suggested-by: Oleg Nesterov <oleg@redhat.com>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Oleg Nesterov <oleg@redhat.com>
    Link: https://lore.kernel.org/bpf/20240905115124.1503998-2-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:27:03 +01:00
Jerome Marchand b936aa2a1f bpf: Allow bpf_current_task_under_cgroup() with BPF_CGROUP_*
JIRA: https://issues.redhat.com/browse/RHEL-63880

commit 7f6287417baf57754f47687c6ea1a749a0686ab0
Author: Matteo Croce <teknoraver@meta.com>
Date:   Mon Aug 19 18:28:05 2024 +0200

    bpf: Allow bpf_current_task_under_cgroup() with BPF_CGROUP_*

    The helper bpf_current_task_under_cgroup() currently is only allowed for
    tracing programs, allow its usage also in the BPF_CGROUP_* program types.

    Move the code from kernel/trace/bpf_trace.c to kernel/bpf/helpers.c,
    so it compiles also without CONFIG_BPF_EVENTS.

    This will be used in systemd-networkd to monitor the sysctl writes,
    and filter it's own writes from others:
    https://github.com/systemd/systemd/pull/32212

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

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2025-01-21 11:24:24 +01:00
Viktor Malik b41752a1a7
bpf: treewide: Align kfunc signatures to prog point-of-view
JIRA: https://issues.redhat.com/browse/RHEL-30774

Conflicts: Omitting bits for kfuncs and tests missing in RHEL due to
           unbackported upstream commits:
           67814c00de316 ("bpf, fsverity: Add kfunc bpf_get_fsverity_digest")
           3e1c6f35409f9 ("bpf: make common crypto API for TC/XDP programs")
           ac9c05e0e453c ("bpf: Add kfunc bpf_get_file_xattr")
           53e380d214419 ("bpf: Add bpf_sock_addr_set_sun_path() to allow writing unix sockaddr from bpf")
           e472f88891abb ("bpf: tcp: Support arbitrary SYN Cookie.")
           c313eae739b9a ("bpf: selftests: Add defrag selftests")

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

    bpf: treewide: Align kfunc signatures to prog point-of-view

    Previously, kfunc declarations in bpf_kfuncs.h (and others) used "user
    facing" types for kfuncs prototypes while the actual kfunc definitions
    used "kernel facing" types. More specifically: bpf_dynptr vs
    bpf_dynptr_kern, __sk_buff vs sk_buff, and xdp_md vs xdp_buff.

    It wasn't an issue before, as the verifier allows aliased types.
    However, since we are now generating kfunc prototypes in vmlinux.h (in
    addition to keeping bpf_kfuncs.h around), this conflict creates
    compilation errors.

    Fix this conflict by using "user facing" types in kfunc definitions.
    This results in more casts, but otherwise has no additional runtime
    cost.

    Note, similar to 5b268d1ebcdc ("bpf: Have bpf_rdonly_cast() take a const
    pointer"), we also make kfuncs take const arguments where appropriate in
    order to make the kfunc more permissive.

    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Link: https://lore.kernel.org/r/b58346a63a0e66bc9b7504da751b526b0b189a67.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
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
Viktor Malik 7dbd0166d9
bpf: Change bpf_session_cookie return value to __u64 *
JIRA: https://issues.redhat.com/browse/RHEL-30773

Conflicts: changed context due to missing upstream commit
           1030e9154258 ("selftests/bpf: Add test that uses fsverity and
           xattr to sign a file")

commit 717d6313bba1b3179f0bf1026aaec6b7e26f484e
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Jun 19 10:16:24 2024 +0200

    bpf: Change bpf_session_cookie return value to __u64 *

    This reverts [1] and changes return value for bpf_session_cookie
    in bpf selftests. Having long * might lead to problems on 32-bit
    architectures.

    Fixes: 2b8dd87332cd ("bpf: Make bpf_session_cookie() kfunc return long *")
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240619081624.1620152-1-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-19 07:39:26 +01:00
Viktor Malik ee112ac3a8
bpf: Fix use-after-free in bpf_uprobe_multi_link_attach()
JIRA: https://issues.redhat.com/browse/RHEL-63330

CVE: CVE-2024-47675

Conflicts: missing upstream commit
           3c83a9ad0295 ("uprobes: make uprobe_register() return struct uprobe *")

commit 5fe6e308abaea082c20fbf2aa5df8e14495622cf
Author: Oleg Nesterov <oleg@redhat.com>
Date:   Tue Aug 13 17:25:24 2024 +0200

    bpf: Fix use-after-free in bpf_uprobe_multi_link_attach()

    If bpf_link_prime() fails, bpf_uprobe_multi_link_attach() goes to the
    error_free label and frees the array of bpf_uprobe's without calling
    bpf_uprobe_unregister().

    This leaks bpf_uprobe->uprobe and worse, this frees bpf_uprobe->consumer
    without removing it from the uprobe->consumers list.

    Fixes: 89ae89f53d20 ("bpf: Add multi uprobe link")
    Closes: https://lore.kernel.org/all/000000000000382d39061f59f2dd@google.com/
    Reported-by: syzbot+f7a1c2c2711e4a780f19@syzkaller.appspotmail.com
    Signed-off-by: Oleg Nesterov <oleg@redhat.com>
    Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Tested-by: syzbot+f7a1c2c2711e4a780f19@syzkaller.appspotmail.com
    Cc: stable@vger.kernel.org
    Link: https://lore.kernel.org/r/20240813152524.GA7292@redhat.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-15 12:24:01 +01:00
Viktor Malik f0c27c8811
bpf: Make bpf_session_cookie() kfunc return long *
JIRA: https://issues.redhat.com/browse/RHEL-30773

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

    bpf: Make bpf_session_cookie() kfunc return long *
    
    We will soon be generating kfunc prototypes from BTF. As part of that,
    we need to align the manual signatures in bpf_kfuncs.h with the actual
    kfunc definitions. There is currently a conflicting signature for
    bpf_session_cookie() w.r.t. return type.
    
    The original intent was to return long * and not __u64 *. You can see
    evidence of that intent in a3a5113393cc ("selftests/bpf: Add kprobe
    session cookie test").
    
    Fix conflict by changing kfunc definition.
    
    Fixes: 5c919acef851 ("bpf: Add support for kprobe session cookie")
    Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
    Link: https://lore.kernel.org/r/7043e1c251ab33151d6e3830f8ea1902ed2604ac.1718207789.git.dxu@dxuuu.xyz
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-13 09:39:22 +01:00
Viktor Malik 61b3a70380
bpf: Fix bpf_session_cookie BTF_ID in special_kfunc_set list
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit aeb8fe0283d4d3b0f27a87c5f5c938e7324f7d8f
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri May 31 21:45:00 2024 +0200

    bpf: Fix bpf_session_cookie BTF_ID in special_kfunc_set list
    
    The bpf_session_cookie is unavailable for !CONFIG_FPROBE as reported
    by Sebastian [1].
    
    To fix that we remove CONFIG_FPROBE ifdef for session kfuncs, which
    is fine, because there's filter for session programs.
    
    Then based on bpf_trace.o dependency:
      obj-$(CONFIG_BPF_EVENTS) += bpf_trace.o
    
    we add bpf_session_cookie BTF_ID in special_kfunc_set list dependency
    on CONFIG_BPF_EVENTS.
    
    [1] https://lore.kernel.org/bpf/20240531071557.MvfIqkn7@linutronix.de/T/#m71c6d5ec71db2967288cb79acedc15cc5dbfeec5
    Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    Tested-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
    Suggested-by: Alexei Starovoitov <ast@kernel.org>
    Fixes: 5c919acef8514 ("bpf: Add support for kprobe session cookie")
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20240531194500.2967187-1-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-13 09:39:21 +01:00
Viktor Malik e8c1905058
bpf: remove unnecessary rcu_read_{lock,unlock}() in multi-uprobe attach logic
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 4a8f635a60540888dab3804992e86410360339c8
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue May 21 09:33:58 2024 -0700

    bpf: remove unnecessary rcu_read_{lock,unlock}() in multi-uprobe attach logic
    
    get_pid_task() internally already calls rcu_read_lock() and
    rcu_read_unlock(), so there is no point to do this one extra time.
    
    This is a drive-by improvement and has no correctness implications.
    
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240521163401.3005045-3-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-13 09:39:20 +01:00
Viktor Malik 3397e120e6
bpf: fix multi-uprobe PID filtering logic
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 46ba0e49b64232adac35a2bc892f1710c5b0fb7f
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue May 21 09:33:57 2024 -0700

    bpf: fix multi-uprobe PID filtering logic
    
    Current implementation of PID filtering logic for multi-uprobes in
    uprobe_prog_run() is filtering down to exact *thread*, while the intent
    for PID filtering it to filter by *process* instead. The check in
    uprobe_prog_run() also differs from the analogous one in
    uprobe_multi_link_filter() for some reason. The latter is correct,
    checking task->mm, not the task itself.
    
    Fix the check in uprobe_prog_run() to perform the same task->mm check.
    
    While doing this, we also update get_pid_task() use to use PIDTYPE_TGID
    type of lookup, given the intent is to get a representative task of an
    entire process. This doesn't change behavior, but seems more logical. It
    would hold task group leader task now, not any random thread task.
    
    Last but not least, given multi-uprobe support is half-broken due to
    this PID filtering logic (depending on whether PID filtering is
    important or not), we need to make it easy for user space consumers
    (including libbpf) to easily detect whether PID filtering logic was
    already fixed.
    
    We do it here by adding an early check on passed pid parameter. If it's
    negative (and so has no chance of being a valid PID), we return -EINVAL.
    Previous behavior would eventually return -ESRCH ("No process found"),
    given there can't be any process with negative PID. This subtle change
    won't make any practical change in behavior, but will allow applications
    to detect PID filtering fixes easily. Libbpf fixes take advantage of
    this in the next patch.
    
    Cc: stable@vger.kernel.org
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Fixes: b733eeade420 ("bpf: Add pid filter support for uprobe_multi link")
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240521163401.3005045-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-13 09:39:19 +01:00
Viktor Malik 7e24c53dc0
bpf: Add support for kprobe session cookie
JIRA: https://issues.redhat.com/browse/RHEL-30773

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

    bpf: Add support for kprobe session cookie
    
    Adding support for cookie within the session of kprobe multi
    entry and return program.
    
    The session cookie is u64 value and can be retrieved be new
    kfunc bpf_session_cookie, which returns pointer to the cookie
    value. The bpf program can use the pointer to store (on entry)
    and load (on return) the value.
    
    The cookie value is implemented via fprobe feature that allows
    to share values between entry and return ftrace fprobe callbacks.
    
    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-4-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:56 +01: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 2fe8fa12a2
bpf: Add support for kprobe session attach
JIRA: https://issues.redhat.com/browse/RHEL-30773

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

    bpf: Add support for kprobe session attach
    
    Adding support to attach bpf program for entry and return probe
    of the same function. This is common use case which at the moment
    requires to create two kprobe multi links.
    
    Adding new BPF_TRACE_KPROBE_SESSION attach type that instructs
    kernel to attach single link program to both entry and exit probe.
    
    It's possible to control execution of the bpf program on return
    probe simply by returning zero or non zero from the entry bpf
    program execution to execute or not the bpf program on return
    probe respectively.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20240430112830.1184228-2-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-11 07:44:56 +01:00
Viktor Malik 5569b490fb
bpf: make bpf_get_branch_snapshot() architecture-agnostic
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 5e6a3c1ee693da1793739bb378b224bcf33d7f14
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Apr 3 17:26:39 2024 -0700

    bpf: make bpf_get_branch_snapshot() architecture-agnostic
    
    perf_snapshot_branch_stack is set up in an architecture-agnostic way, so
    there is no reason for BPF subsystem to keep track of which
    architectures do support LBR or not. E.g., it looks like ARM64 might soon
    get support for BRBE ([0]), which (with proper integration) should be
    possible to utilize using this BPF helper.
    
    perf_snapshot_branch_stack static call will point to
    __static_call_return0() by default, which just returns zero, which will
    lead to -ENOENT, as expected. So no need to guard anything here.
    
      [0] https://lore.kernel.org/linux-arm-kernel/20240125094119.2542332-1-anshuman.khandual@arm.com/
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/r/20240404002640.1774210-2-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:44 +01:00
Viktor Malik 7e6016eca6
bpf: Avoid get_kernel_nofault() to fetch kprobe entry IP
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit a8497506cd2c0fc90a64f6f5d2744a0ddb2c81eb
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Mar 19 14:20:13 2024 -0700

    bpf: Avoid get_kernel_nofault() to fetch kprobe entry IP
    
    get_kernel_nofault() (or, rather, underlying copy_from_kernel_nofault())
    is not free and it does pop up in performance profiles when
    kprobes are heavily utilized with CONFIG_X86_KERNEL_IBT=y config.
    
    Let's avoid using it if we know that fentry_ip - 4 can't cross page
    boundary. We do that by masking lowest 12 bits and checking if they are
    
    Another benefit (and actually what caused a closer look at this part of
    code) is that now LBR record is (typically) not wasted on
    copy_from_kernel_nofault() call and code, which helps tools like
    retsnoop that grab LBR records from inside BPF code in kretprobes.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
    Link: https://lore.kernel.org/bpf/20240319212013.1046779-1-andrii@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:34 +01:00
Viktor Malik 6cd5a45b58
bpf: support BPF cookie in raw tracepoint (raw_tp, tp_btf) programs
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 68ca5d4eebb8c4de246ee5f634eee26bc689562d
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Mar 19 16:38:50 2024 -0700

    bpf: support BPF cookie in raw tracepoint (raw_tp, tp_btf) programs
    
    Wire up BPF cookie for raw tracepoint programs (both BTF and non-BTF
    aware variants). This brings them up to part w.r.t. BPF cookie usage
    with classic tracepoint and fentry/fexit programs.
    
    Acked-by: Stanislav Fomichev <sdf@google.com>
    Acked-by: Eduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Message-ID: <20240319233852.1977493-4-andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:31 +01:00
Viktor Malik c1ad04e563
bpf: pass whole link instead of prog when triggering raw tracepoint
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit d4dfc5700e867b22ab94f960f9a9972696a637d5
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Mar 19 16:38:49 2024 -0700

    bpf: pass whole link instead of prog when triggering raw tracepoint
    
    Instead of passing prog as an argument to bpf_trace_runX() helpers, that
    are called from tracepoint triggering calls, store BPF link itself
    (struct bpf_raw_tp_link for raw tracepoints). This will allow to pass
    extra information like BPF cookie into raw tracepoint registration.
    
    Instead of replacing `struct bpf_prog *prog = __data;` with
    corresponding `struct bpf_raw_tp_link *link = __data;` assignment in
    `__bpf_trace_##call` I just passed `__data` through into underlying
    bpf_trace_runX() call. This works well because we implicitly cast `void *`,
    and it also avoids naming clashes with arguments coming from
    tracepoint's "proto" list. We could have run into the same problem with
    "prog", we just happened to not have a tracepoint that has "prog" input
    argument. We are less lucky with "link", as there are tracepoints using
    "link" argument name already. So instead of trying to avoid naming
    conflicts, let's just remove intermediate local variable. It doesn't
    hurt readibility, it's either way a bit of a maze of calls and macros,
    that requires careful reading.
    
    Acked-by: Stanislav Fomichev <sdf@google.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Message-ID: <20240319233852.1977493-3-andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:31 +01:00
Viktor Malik 861c794fe5
bpf: flatten bpf_probe_register call chain
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit 6b9c2950c912780ce113079c9c52041b1e2a611a
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Mar 19 16:38:48 2024 -0700

    bpf: flatten bpf_probe_register call chain
    
    bpf_probe_register() and __bpf_probe_register() have identical
    signatures and bpf_probe_register() just redirect to
    __bpf_probe_register(). So get rid of this extra function call step to
    simplify following the source code.
    
    It has no difference at runtime due to inlining, of course.
    
    Acked-by: Stanislav Fomichev <sdf@google.com>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Message-ID: <20240319233852.1977493-2-andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:31 +01:00
Viktor Malik bfe07a51a2
bpf: Allow helper bpf_get_[ns_]current_pid_tgid() for all prog types
JIRA: https://issues.redhat.com/browse/RHEL-30773

commit eb166e522c77699fc19bfa705652327a1e51a117
Author: Yonghong Song <yonghong.song@linux.dev>
Date:   Fri Mar 15 11:48:54 2024 -0700

    bpf: Allow helper bpf_get_[ns_]current_pid_tgid() for all prog types
    
    Currently bpf_get_current_pid_tgid() is allowed in tracing, cgroup
    and sk_msg progs while bpf_get_ns_current_pid_tgid() is only allowed
    in tracing progs.
    
    We have an internal use case where for an application running
    in a container (with pid namespace), user wants to get
    the pid associated with the pid namespace in a cgroup bpf
    program. Currently, cgroup bpf progs already allow
    bpf_get_current_pid_tgid(). Let us allow bpf_get_ns_current_pid_tgid()
    as well.
    
    With auditing the code, bpf_get_current_pid_tgid() is also used
    by sk_msg prog. But there are no side effect to expose these two
    helpers to all prog types since they do not reveal any kernel specific
    data. The detailed discussion is in [1].
    
    So with this patch, both bpf_get_current_pid_tgid() and bpf_get_ns_current_pid_tgid()
    are put in bpf_base_func_proto(), making them available to all
    program types.
    
      [1] https://lore.kernel.org/bpf/20240307232659.1115872-1-yonghong.song@linux.dev/
    
    Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/bpf/20240315184854.2975190-1-yonghong.song@linux.dev

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-11-07 13:58:30 +01:00
Rado Vrbovsky daac70a746 Merge: fprobe: backport prerequisites for BPF session probes
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5586

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

BPF kprobe session probes, which are coming in BPF 6.10 rebase for RHEL 9.6, use fprobe as the underlying tracing mechanism. This MR backports the fprobe patches which are required for session probes to work. Since there were several conflicts in the backport, I'm posting these separately (rather than taking them as a part of the BPF rebase) to get better review and testing.

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

Approved-by: Chris von Recklinghausen <crecklin@redhat.com>
Approved-by: Rafael Aquini <raquini@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-11-05 11:34:58 +00:00
Viktor Malik cbe464fbfc
fprobe: Pass return address to the handlers
JIRA: https://issues.redhat.com/browse/RHEL-64700

Conflicts: small change due to upstream commit
           2752741080f8 ("fprobe: add recursion detection in fprobe_exit_handler")
           previously backported out-of-order, aligning with upstream
           code to prevent future conflicts

commit cb16330d12741f6dae56aad5acf62f5be3a06c4e
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Date:   Tue Jun 6 21:39:55 2023 +0900

    fprobe: Pass return address to the handlers

    Pass return address as 'ret_ip' to the fprobe entry and return handlers
    so that the fprobe user handler can get the reutrn address without
    analyzing arch-dependent pt_regs.

    Link: https://lore.kernel.org/all/168507467664.913472.11642316698862778600.stgit@mhiramat.roam.corp.google.com/

    Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-10-25 09:07:32 +02:00
Viktor Malik cd03f46e77
fprobe: Skip exit_handler if entry_handler returns !0
JIRA: https://issues.redhat.com/browse/RHEL-64700

Conflicts: several conflicts in kernel/trace/fprobe.c due to commits
           2752741080f8 ("fprobe: add recursion detection in fprobe_exit_handler")
           3cc4e2c5fbae ("fprobe: make fprobe_kprobe_handler recursion free")
           previously backported out-of-order.

commit 39d954200bf6ad503c722e44d0be80c7b826fa42
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Date:   Thu Feb 2 00:56:38 2023 +0900

    fprobe: Skip exit_handler if entry_handler returns !0

    Skip hooking function return and calling exit_handler if the
    entry_handler() returns !0.

    Link: https://lkml.kernel.org/r/167526699798.433354.10998365726830117303.stgit@mhiramat.roam.corp.google.com

    Cc: Florent Revest <revest@chromium.org>
    Cc: Mark Rutland <mark.rutland@arm.com>
    Cc: Will Deacon <will@kernel.org>
    Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
    Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-10-25 09:07:22 +02:00
Viktor Malik b2df8a0635
fprobe: Pass entry_data to handlers
JIRA: https://issues.redhat.com/browse/RHEL-64700

Conflicts: several conflicts in kernel/trace/fprobe.c due to commits
           2752741080f8 ("fprobe: add recursion detection in fprobe_exit_handler")
           3cc4e2c5fbae ("fprobe: make fprobe_kprobe_handler recursion free")
           previously backported out-of-order.

commit 76d0de5729c0569c4071e7f21fcab394e502f03a
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Date:   Thu Feb 2 00:56:01 2023 +0900

    fprobe: Pass entry_data to handlers

    Pass the private entry_data to the entry and exit handlers so that
    they can share the context data, something like saved function
    arguments etc.
    User must specify the private entry_data size by @entry_data_size
    field before registering the fprobe.

    Link: https://lkml.kernel.org/r/167526696173.433354.17408372048319432574.stgit@mhiramat.roam.corp.google.com

    Cc: Florent Revest <revest@chromium.org>
    Cc: Mark Rutland <mark.rutland@arm.com>
    Cc: Will Deacon <will@kernel.org>
    Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
    Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-10-25 09:06:39 +02:00
Jerome Marchand d340b4ed1c bpf: support deferring bpf_link dealloc to after RCU grace period
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 1a80dbcb2dbaf6e4c216e62e30fa7d3daa8001ce
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Mar 27 22:24:26 2024 -0700

    bpf: support deferring bpf_link dealloc to after RCU grace period

    BPF link for some program types is passed as a "context" which can be
    used by those BPF programs to look up additional information. E.g., for
    multi-kprobes and multi-uprobes, link is used to fetch BPF cookie values.

    Because of this runtime dependency, when bpf_link refcnt drops to zero
    there could still be active BPF programs running accessing link data.

    This patch adds generic support to defer bpf_link dealloc callback to
    after RCU GP, if requested. This is done by exposing two different
    deallocation callbacks, one synchronous and one deferred. If deferred
    one is provided, bpf_link_free() will schedule dealloc_deferred()
    callback to happen after RCU GP.

    BPF is using two flavors of RCU: "classic" non-sleepable one and RCU
    tasks trace one. The latter is used when sleepable BPF programs are
    used. bpf_link_free() accommodates that by checking underlying BPF
    program's sleepable flag, and goes either through normal RCU GP only for
    non-sleepable, or through RCU tasks trace GP *and* then normal RCU GP
    (taking into account rcu_trace_implies_rcu_gp() optimization), if BPF
    program is sleepable.

    We use this for multi-kprobe and multi-uprobe links, which dereference
    link during program run. We also preventively switch raw_tp link to use
    deferred dealloc callback, as upcoming changes in bpf-next tree expose
    raw_tp link data (specifically, cookie value) to BPF program at runtime
    as well.

    Fixes: 0dcac2725406 ("bpf: Add multi kprobe link")
    Fixes: 89ae89f53d20 ("bpf: Add multi uprobe link")
    Reported-by: syzbot+981935d9485a560bfbcb@syzkaller.appspotmail.com
    Reported-by: syzbot+2cb5a6c573e98db598cc@syzkaller.appspotmail.com
    Reported-by: syzbot+62d8b26793e8a2bd0516@syzkaller.appspotmail.com
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Link: https://lore.kernel.org/r/20240328052426.3042617-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:17 +02:00
Jerome Marchand d868076f8e bpf: put uprobe link's path and task in release callback
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit e9c856cabefb71d47b2eeb197f72c9c88e9b45b0
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Mar 27 22:24:25 2024 -0700

    bpf: put uprobe link's path and task in release callback

    There is no need to delay putting either path or task to deallocation
    step. It can be done right after bpf_uprobe_unregister. Between release
    and dealloc, there could be still some running BPF programs, but they
    don't access either task or path, only data in link->uprobes, so it is
    safe to do.

    On the other hand, doing path_put() in dealloc callback makes this
    dealloc sleepable because path_put() itself might sleep. Which is
    problematic due to the need to call uprobe's dealloc through call_rcu(),
    which is what is done in the next bug fix patch. So solve the problem by
    releasing these resources early.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20240328052426.3042617-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:17 +02: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 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 d1c16d1138 bpf: Take into account BPF token when fetching helper protos
JIRA: https://issues.redhat.com/browse/RHEL-23649

Conflicts: Context change due to missing commit 9a675ba55a96 ("net,
bpf: Add a warning if NAPI cb missed xdp_do_flush().")

commit bbc1d24724e110b86a1a7c3c1724ce0d62cc1e2e
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Jan 23 18:21:04 2024 -0800

    bpf: Take into account BPF token when fetching helper protos

    Instead of performing unconditional system-wide bpf_capable() and
    perfmon_capable() calls inside bpf_base_func_proto() function (and other
    similar ones) to determine eligibility of a given BPF helper for a given
    program, use previously recorded BPF token during BPF_PROG_LOAD command
    handling to inform the decision.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20240124022127.2379740-8-andrii@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:03 +02:00
Jerome Marchand 5bc5673dba bpf: Store cookies in kprobe_multi bpf_link_info data
JIRA: https://issues.redhat.com/browse/RHEL-23649

commit 9fd112b1f82b587ffb12fb67dd032f551fdb571a
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Jan 19 12:04:59 2024 +0100

    bpf: Store cookies in kprobe_multi bpf_link_info data

    Storing cookies in kprobe_multi bpf_link_info data. The cookies
    field is optional and if provided it needs to be an array of
    __u64 with kprobe_multi.count length.

    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20240119110505.400573-3-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-10-15 10:49:01 +02:00
Viktor Malik 9680ef97a0
Revert BPF token-related functionality
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit d17aff807f845cf93926c28705216639c7279110
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Tue Dec 19 07:37:35 2023 -0800

    Revert BPF token-related functionality

    This patch includes the following revert (one  conflicting BPF FS
    patch and three token patch sets, represented by merge commits):
      - revert 0f5d5454c723 "Merge branch 'bpf-fs-mount-options-parsing-follow-ups'";
      - revert 750e785796bb "bpf: Support uid and gid when mounting bpffs";
      - revert 733763285acf "Merge branch 'bpf-token-support-in-libbpf-s-bpf-object'";
      - revert c35919dcce28 "Merge branch 'bpf-token-and-bpf-fs-based-delegation'".

    Link: https://lore.kernel.org/bpf/CAHk-=wg7JuFYwGy=GOMbRCtOL+jwSQsdUaBsRWkDVYbxipbM5A@mail.gmail.com
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:29 +02:00
Viktor Malik 813481f347
bpf: Fail uprobe multi link with negative offset
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 3983c00281d96af2ba611254d679107b5c390627
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Sun Dec 17 22:55:37 2023 +0100

    bpf: Fail uprobe multi link with negative offset
    
    Currently the __uprobe_register will return 0 (success) when called with
    negative offset. The reason is that the call to register_for_each_vma and
    then build_map_info won't return error for negative offset. They just won't
    do anything - no matching vma is found so there's no registered breakpoint
    for the uprobe.
    
    I don't think we can change the behaviour of __uprobe_register and fail
    for negative uprobe offset, because apps might depend on that already.
    
    But I think we can still make the change and check for it on bpf multi
    link syscall level.
    
    Also moving the __get_user call and check for the offsets to the top of
    loop, to fail early without extra __get_user calls for ref_ctr_offset
    and cookie arrays.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/bpf/20231217215538.3361991-2-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 11:07:28 +02:00
Viktor Malik 97a0bb6d2d
bpf: Limit the number of kprobes when attaching program to multiple kprobes
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit d6d1e6c17cab2dcb7b8530c599f00e7de906d380
Author: Hou Tao <houtao1@huawei.com>
Date:   Fri Dec 15 18:07:05 2023 +0800

    bpf: Limit the number of kprobes when attaching program to multiple kprobes
    
    An abnormally big cnt may also be assigned to kprobe_multi.cnt when
    attaching multiple kprobes. It will trigger the following warning in
    kvmalloc_node():
    
    	if (unlikely(size > INT_MAX)) {
    	    WARN_ON_ONCE(!(flags & __GFP_NOWARN));
    	    return NULL;
    	}
    
    Fix the warning by limiting the maximal number of kprobes in
    bpf_kprobe_multi_link_attach(). If the number of kprobes is greater than
    MAX_KPROBE_MULTI_CNT, the attachment will fail and return -E2BIG.
    
    Fixes: 0dcac2725406 ("bpf: Add multi kprobe link")
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20231215100708.2265609-3-houtao@huaweicloud.com

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:29 +02:00
Viktor Malik 3e424bf42b
bpf: take into account BPF token when fetching helper protos
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 4cbb270e115bc197ff2046aeb54cc951666b16ec
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Thu Nov 30 10:52:19 2023 -0800

    bpf: take into account BPF token when fetching helper protos
    
    Instead of performing unconditional system-wide bpf_capable() and
    perfmon_capable() calls inside bpf_base_func_proto() function (and other
    similar ones) to determine eligibility of a given BPF helper for a given
    program, use previously recorded BPF token during BPF_PROG_LOAD command
    handling to inform the decision.
    
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/r/20231130185229.2688956-8-andrii@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:52:09 +02:00
Viktor Malik 9241735a2d
bpf: Add link_info support for uprobe multi link
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit e56fdbfb06e26a7066b070967badef4148528df2
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Sat Nov 25 20:31:27 2023 +0100

    bpf: Add link_info support for uprobe multi link
    
    Adding support to get uprobe_link details through bpf_link_info
    interface.
    
    Adding new struct uprobe_multi to struct bpf_link_info to carry
    the uprobe_multi link details.
    
    The uprobe_multi.count is passed from user space to denote size
    of array fields (offsets/ref_ctr_offsets/cookies). The actual
    array size is stored back to uprobe_multi.count (allowing user
    to find out the actual array size) and array fields are populated
    up to the user passed size.
    
    All the non-array fields (path/count/flags/pid) are always set.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Yonghong Song <yonghong.song@linux.dev>
    Link: https://lore.kernel.org/bpf/20231125193130.834322-4-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:58 +02:00
Viktor Malik 34b3c6326b
bpf: Store ref_ctr_offsets values in bpf_uprobe array
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 4930b7f53a298533bc31d7540b6ea8b79a000331
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Sat Nov 25 20:31:26 2023 +0100

    bpf: Store ref_ctr_offsets values in bpf_uprobe array
    
    We will need to return ref_ctr_offsets values through link_info
    interface in following change, so we need to keep them around.
    
    Storing ref_ctr_offsets values directly into bpf_uprobe array.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/bpf/20231125193130.834322-3-jolsa@kernel.org

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:58 +02:00
Viktor Malik 467d71bf83
bpf: Add __bpf_dynptr_data* for in kernel use
JIRA: https://issues.redhat.com/browse/RHEL-23644

commit 74523c06ae20b83c5508a98af62393ac34913362
Author: Song Liu <song@kernel.org>
Date:   Mon Nov 6 20:57:23 2023 -0800

    bpf: Add __bpf_dynptr_data* for in kernel use
    
    Different types of bpf dynptr have different internal data storage.
    Specifically, SKB and XDP type of dynptr may have non-continuous data.
    Therefore, it is not always safe to directly access dynptr->data.
    
    Add __bpf_dynptr_data and __bpf_dynptr_data_rw to replace direct access to
    dynptr->data.
    
    Update bpf_verify_pkcs7_signature to use __bpf_dynptr_data instead of
    dynptr->data.
    
    Signed-off-by: Song Liu <song@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
    Link: https://lore.kernel.org/bpf/20231107045725.2278852-2-song@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Viktor Malik <vmalik@redhat.com>
2024-06-25 10:51:40 +02:00
Artem Savkov 1e9cbbe0f6 bpf: Add __bpf_kfunc_{start,end}_defs macros
JIRA: https://issues.redhat.com/browse/RHEL-23643

Upstream Status: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Conflicts: missing xdp commits, missing vma_task iterator

commit 391145ba2accc48b596f3d438af1a6255b62a555
Author: Dave Marchevsky <davemarchevsky@fb.com>
Date:   Tue Oct 31 14:56:24 2023 -0700

    bpf: Add __bpf_kfunc_{start,end}_defs macros

    BPF kfuncs are meant to be called from BPF programs. Accordingly, most
    kfuncs are not called from anywhere in the kernel, which the
    -Wmissing-prototypes warning is unhappy about. We've peppered
    __diag_ignore_all("-Wmissing-prototypes", ... everywhere kfuncs are
    defined in the codebase to suppress this warning.

    This patch adds two macros meant to bound one or many kfunc definitions.
    All existing kfunc definitions which use these __diag calls to suppress
    -Wmissing-prototypes are migrated to use the newly-introduced macros.
    A new __diag_ignore_all - for "-Wmissing-declarations" - is added to the
    __bpf_kfunc_start_defs macro based on feedback from Andrii on an earlier
    version of this patch [0] and another recent mailing list thread [1].

    In the future we might need to ignore different warnings or do other
    kfunc-specific things. This change will make it easier to make such
    modifications for all kfunc defs.

      [0]: https://lore.kernel.org/bpf/CAEf4BzaE5dRWtK6RPLnjTW-MW9sx9K3Fn6uwqCTChK2Dcb1Xig@mail.gmail.com/
      [1]: https://lore.kernel.org/bpf/ZT+2qCc%2FaXep0%2FLf@krava/

    Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
    Suggested-by: Andrii Nakryiko <andrii@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Cc: Jiri Olsa <olsajiri@gmail.com>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: David Vernet <void@manifault.com>
    Acked-by: Yafang Shao <laoar.shao@gmail.com>
    Link: https://lore.kernel.org/r/20231031215625.2343848-1-davemarchevsky@fb.com
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 11:23:42 +01:00
Artem Savkov c10287edfe bpf: Add missed value to kprobe perf link info
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit 3acf8ace68230e9558cf916847f1cc9f208abdf1
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:39 2023 +0200

    bpf: Add missed value to kprobe perf link info
    
    Add missed value to kprobe attached through perf link info to
    hold the stats of missed kprobe handler execution.
    
    The kprobe's missed counter gets incremented when kprobe handler
    is not executed due to another kprobe running on the same cpu.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-4-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 11:23:41 +01:00
Artem Savkov bc13cf8119 bpf: Count missed stats in trace_call_bpf
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit dd8657894c11b03c6eb0fd53fe9d7fec2072d18b
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:40 2023 +0200

    bpf: Count missed stats in trace_call_bpf
    
    Increase misses stats in case bpf array execution is skipped
    because of recursion check in trace_call_bpf.
    
    Adding bpf_prog_inc_misses_counters that increase misses
    counts for all bpf programs in bpf_prog_array.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Song Liu <song@kernel.org>
    Reviewed-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-5-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:51 +01:00
Artem Savkov 876a81c76d bpf: Add missed value to kprobe_multi link info
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit e2b2cd592adbd303bcc02451d32fedd511000fb0
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:38 2023 +0200

    bpf: Add missed value to kprobe_multi link info
    
    Add missed value to kprobe_multi link info to hold the stats of missed
    kprobe_multi probe.
    
    The missed counter gets incremented when fprobe fails the recursion
    check or there's no rethook available for return probe. In either
    case the attached bpf program is not executed.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Song Liu <song@kernel.org>
    Reviewed-by: Song Liu <song@kernel.org>
    Acked-by: Hou Tao <houtao1@huawei.com>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-3-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:51 +01:00
Artem Savkov e6410c16d2 bpf: Count stats for kprobe_multi programs
JIRA: https://issues.redhat.com/browse/RHEL-23643

commit f915fcb38553eb9150a918348d932fd292de71dc
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Wed Sep 20 23:31:37 2023 +0200

    bpf: Count stats for kprobe_multi programs
    
    Adding support to gather missed stats for kprobe_multi
    programs due to bpf_prog_active protection.
    
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Tested-by: Song Liu <song@kernel.org>
    Reviewed-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/bpf/20230920213145.1941596-2-jolsa@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2024-03-27 10:27:51 +01:00
Jerome Marchand 36efef37bf bpf: Limit the number of uprobes when attaching program to multiple uprobes
JIRA: https://issues.redhat.com/browse/RHEL-21411

commit 8b2efe51ba85ca83460941672afac6fca4199df6
Author: Hou Tao <houtao1@huawei.com>
Date:   Fri Dec 15 18:07:04 2023 +0800

    bpf: Limit the number of uprobes when attaching program to multiple uprobes

    An abnormally big cnt may be passed to link_create.uprobe_multi.cnt,
    and it will trigger the following warning in kvmalloc_node():

            if (unlikely(size > INT_MAX)) {
                    WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                    return NULL;
            }

    Fix the warning by limiting the maximal number of uprobes in
    bpf_uprobe_multi_link_attach(). If the number of uprobes is greater than
    MAX_UPROBE_MULTI_CNT, the attachment will return -E2BIG.

    Fixes: 89ae89f53d20 ("bpf: Add multi uprobe link")
    Reported-by: Xingwei Lee <xrivendell7@gmail.com>
    Signed-off-by: Hou Tao <houtao1@huawei.com>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Closes: https://lore.kernel.org/bpf/CABOYnLwwJY=yFAGie59LFsUsBAgHfroVqbzZ5edAXbFE3YiNVA@mail.gmail.com
    Link: https://lore.kernel.org/bpf/20231215100708.2265609-2-houtao@huaweicloud.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2024-01-16 11:03:58 +01:00
Jerome Marchand e316e7ef3a bpf: Fix uprobe_multi get_pid_task error path
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 57eb5e1c5c57972c95e8efab6bc81b87161b0b07
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Fri Sep 15 12:14:20 2023 +0200

    bpf: Fix uprobe_multi get_pid_task error path

    Dan reported Smatch static checker warning due to missing error
    value set in uprobe multi link's get_pid_task error path.

    Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
    Closes: https://lore.kernel.org/bpf/c5ffa7c0-6b06-40d5-aca2-63833b5cd9af@moroto.mountain/
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Reviewed-by: Song Liu <song@kernel.org>
    Link: https://lore.kernel.org/r/20230915101420.1193800-1-jolsa@kernel.org
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:04 +01:00
Jerome Marchand 48d0b6b0f2 bpf: Add override check to kprobe multi link attach
JIRA: https://issues.redhat.com/browse/RHEL-10691

commit 41bc46c12a8053a1b3279a379bd6b5e87b045b85
Author: Jiri Olsa <jolsa@kernel.org>
Date:   Thu Sep 7 22:06:51 2023 +0200

    bpf: Add override check to kprobe multi link attach

    Currently the multi_kprobe link attach does not check error
    injection list for programs with bpf_override_return helper
    and allows them to attach anywhere. Adding the missing check.

    Fixes: 0dcac2725406 ("bpf: Add multi kprobe link")
    Signed-off-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Reviewed-by: Alan Maguire <alan.maguire@oracle.com>
    Cc: stable@vger.kernel.org
    Link: https://lore.kernel.org/bpf/20230907200652.926951-1-jolsa@kernel.org

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2023-12-15 09:29:03 +01:00