Commit Graph

8 Commits

Author SHA1 Message Date
Jerome Marchand 84681bc21d bpf: Disallow bpf programs call prog_run command.
Bugzilla: https://bugzilla.redhat.com/2120966

Conflicts:
Code change from missing commit e384c7b7b46d ("bpf, x86: Create
bpf_tramp_run_ctx on the caller thread's stack")

commit 86f44fcec22ce2979507742bc53db8400e454f46
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Aug 8 20:58:09 2022 -0700

    bpf: Disallow bpf programs call prog_run command.

    The verifier cannot perform sufficient validation of bpf_attr->test.ctx_in
    pointer, therefore bpf programs should not be allowed to call BPF_PROG_RUN
    command from within the program.
    To fix this issue split bpf_sys_bpf() bpf helper into normal kern_sys_bpf()
    kernel function that can only be used by the kernel light skeleton directly.

    Reported-by: YiFei Zhu <zhuyifei@google.com>
    Fixes: b1d18a7574d0 ("bpf: Extend sys_bpf commands for bpf_syscall programs.")
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:58:10 +02:00
Jerome Marchand e168d821de libbpf: Prepare light skeleton for the kernel.
Bugzilla: https://bugzilla.redhat.com/2120966

Conflicts:
Context change from missing commit e32cb12ff52a ("bpf, mips: Fix build
errors about __NR_bpf undeclared")

commit 6fe65f1b4db3fff305896e997c2804b7b42236ce
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Wed Feb 9 15:19:58 2022 -0800

    libbpf: Prepare light skeleton for the kernel.

    Prepare light skeleton to be used in the kernel module and in the user space.
    The look and feel of lskel.h is mostly the same with the difference that for
    user space the skel->rodata is the same pointer before and after skel_load
    operation, while in the kernel the skel->rodata after skel_open and the
    skel->rodata after skel_load are different pointers.
    Typical usage of skeleton remains the same for kernel and user space:
    skel = my_bpf__open();
    skel->rodata->my_global_var = init_val;
    err = my_bpf__load(skel);
    err = my_bpf__attach(skel);
    // access skel->rodata->my_global_var;
    // access skel->bss->another_var;

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Yonghong Song <yhs@fb.com>
    Acked-by: Andrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20220209232001.27490-3-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:49 +02:00
Jerome Marchand 691f6ba70b libbpf: Open code raw_tp_open and link_create commands.
Bugzilla: https://bugzilla.redhat.com/2120966

commit c69f94a33d12a9c49f1800c54838ee19447ac176
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Jan 31 14:05:24 2022 -0800

    libbpf: Open code raw_tp_open and link_create commands.

    Open code raw_tracepoint_open and link_create used by light skeleton
    to be able to avoid full libbpf eventually.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20220131220528.98088-4-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:45 +02:00
Jerome Marchand e0cfac101a libbpf: Open code low level bpf commands.
Bugzilla: https://bugzilla.redhat.com/2120966

commit e981f41fd029d37b3e1c8aad2d72d3fe57a104d6
Author: Alexei Starovoitov <ast@kernel.org>
Date:   Mon Jan 31 14:05:23 2022 -0800

    libbpf: Open code low level bpf commands.

    Open code low level bpf commands used by light skeleton to
    be able to avoid full libbpf eventually.

    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Acked-by: Martin KaFai Lau <kafai@fb.com>
    Link: https://lore.kernel.org/bpf/20220131220528.98088-3-alexei.starovoitov@gmail.com

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
2022-10-25 14:57:45 +02:00
Artem Savkov bf3557ee93 libbpf: Use bpf_map_create() consistently internally
Bugzilla: https://bugzilla.redhat.com/2069046

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

commit a9606f405f2c8f24751b0a7326655a657a63ad60
Author: Andrii Nakryiko <andrii@kernel.org>
Date:   Wed Nov 24 11:32:31 2021 -0800

    libbpf: Use bpf_map_create() consistently internally

    Remove all the remaining uses of to-be-deprecated bpf_create_map*() APIs.

    Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
    Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
    Link: https://lore.kernel.org/bpf/20211124193233.3115996-3-andrii@kernel.org

Signed-off-by: Artem Savkov <asavkov@redhat.com>
2022-08-24 12:53:39 +02:00
Yauheni Kaliuta e03f57186d libbpf: Fix skel_internal.h to set errno on loader retval < 0
Bugzilla: http://bugzilla.redhat.com/2069045

commit e68ac0082787f4e8ee6ae5b19076ec7709ce715b
Author: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Date:   Mon Sep 27 20:29:39 2021 +0530

    libbpf: Fix skel_internal.h to set errno on loader retval < 0
    
    When the loader indicates an internal error (result of a checked bpf
    system call), it returns the result in attr.test.retval. However, tests
    that rely on ASSERT_OK_PTR on NULL (returned from light skeleton) may
    miss that NULL denotes an error if errno is set to 0. This would result
    in skel pointer being NULL, while ASSERT_OK_PTR returning 1, leading to
    a SEGV on dereference of skel, because libbpf_get_error relies on the
    assumption that errno is always set in case of error for ptr == NULL.
    
    In particular, this was observed for the ksyms_module test. When
    executed using `./test_progs -t ksyms`, prior tests manipulated errno
    and the test didn't crash when it failed at ksyms_module load, while
    using `./test_progs -t ksyms_module` crashed due to errno being
    untouched.
    
    Fixes: 6723474373 (libbpf: Generate loader program out of BPF ELF file.)
    Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
    Signed-off-by: Alexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210927145941.1383001-11-memxor@gmail.com

Signed-off-by: Yauheni Kaliuta <ykaliuta@redhat.com>
2022-06-03 17:23:54 +03:00
Alexei Starovoitov 5d67f34959 bpf: Add cmd alias BPF_PROG_RUN
Add BPF_PROG_RUN command as an alias to BPF_RPOG_TEST_RUN to better
indicate the full range of use cases done by the command.

Suggested-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20210519014032.20908-1-alexei.starovoitov@gmail.com
2021-05-19 15:35:12 +02:00
Alexei Starovoitov 6723474373 libbpf: Generate loader program out of BPF ELF file.
The BPF program loading process performed by libbpf is quite complex
and consists of the following steps:
"open" phase:
- parse elf file and remember relocations, sections
- collect externs and ksyms including their btf_ids in prog's BTF
- patch BTF datasec (since llvm couldn't do it)
- init maps (old style map_def, BTF based, global data map, kconfig map)
- collect relocations against progs and maps
"load" phase:
- probe kernel features
- load vmlinux BTF
- resolve externs (kconfig and ksym)
- load program BTF
- init struct_ops
- create maps
- apply CO-RE relocations
- patch ld_imm64 insns with src_reg=PSEUDO_MAP, PSEUDO_MAP_VALUE, PSEUDO_BTF_ID
- reposition subprograms and adjust call insns
- sanitize and load progs

During this process libbpf does sys_bpf() calls to load BTF, create maps,
populate maps and finally load programs.
Instead of actually doing the syscalls generate a trace of what libbpf
would have done and represent it as the "loader program".
The "loader program" consists of single map with:
- union bpf_attr(s)
- BTF bytes
- map value bytes
- insns bytes
and single bpf program that passes bpf_attr(s) and data into bpf_sys_bpf() helper.
Executing such "loader program" via bpf_prog_test_run() command will
replay the sequence of syscalls that libbpf would have done which will result
the same maps created and programs loaded as specified in the elf file.
The "loader program" removes libelf and majority of libbpf dependency from
program loading process.

kconfig, typeless ksym, struct_ops and CO-RE are not supported yet.

The order of relocate_data and relocate_calls had to change, so that
bpf_gen__prog_load() can see all relocations for a given program with
correct insn_idx-es.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210514003623.28033-15-alexei.starovoitov@gmail.com
2021-05-19 00:39:40 +02:00