1. 14 10月, 2022 5 次提交
    • S
      libbpf: Fix null-pointer dereference in find_prog_by_sec_insn() · d0d382f9
      Shung-Hsi Yu 提交于
      When there are no program sections, obj->programs is left unallocated,
      and find_prog_by_sec_insn()'s search lands on &obj->programs[0] == NULL,
      and will cause null-pointer dereference in the following access to
      prog->sec_idx.
      
      Guard the search with obj->nr_programs similar to what's being done in
      __bpf_program__iter() to prevent null-pointer access from happening.
      
      Fixes: db2b8b06 ("libbpf: Support CO-RE relocations for multi-prog sections")
      Signed-off-by: NShung-Hsi Yu <shung-hsi.yu@suse.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20221012022353.7350-4-shung-hsi.yu@suse.com
      d0d382f9
    • S
      libbpf: Deal with section with no data gracefully · 35a85550
      Shung-Hsi Yu 提交于
      ELF section data pointer returned by libelf may be NULL (if section has
      SHT_NOBITS), so null check section data pointer before attempting to
      copy license and kversion section.
      
      Fixes: cb1e5e96 ("bpf tools: Collect version and license from ELF sections")
      Signed-off-by: NShung-Hsi Yu <shung-hsi.yu@suse.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20221012022353.7350-3-shung-hsi.yu@suse.com
      35a85550
    • S
      libbpf: Use elf_getshdrnum() instead of e_shnum · 51deedc9
      Shung-Hsi Yu 提交于
      This commit replace e_shnum with the elf_getshdrnum() helper to fix two
      oss-fuzz-reported heap-buffer overflow in __bpf_object__open. Both
      reports are incorrectly marked as fixed and while still being
      reproducible in the latest libbpf.
      
        # clusterfuzz-testcase-minimized-bpf-object-fuzzer-5747922482888704
        libbpf: loading object 'fuzz-object' from buffer
        libbpf: sec_cnt is 0
        libbpf: elf: section(1) .data, size 0, link 538976288, flags 2020202020202020, type=2
        libbpf: elf: section(2) .data, size 32, link 538976288, flags 202020202020ff20, type=1
        =================================================================
        ==13==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000c0 at pc 0x0000005a7b46 bp 0x7ffd12214af0 sp 0x7ffd12214ae8
        WRITE of size 4 at 0x6020000000c0 thread T0
        SCARINESS: 46 (4-byte-write-heap-buffer-overflow-far-from-bounds)
            #0 0x5a7b45 in bpf_object__elf_collect /src/libbpf/src/libbpf.c:3414:24
            #1 0x5733c0 in bpf_object_open /src/libbpf/src/libbpf.c:7223:16
            #2 0x5739fd in bpf_object__open_mem /src/libbpf/src/libbpf.c:7263:20
            ...
      
      The issue lie in libbpf's direct use of e_shnum field in ELF header as
      the section header count. Where as libelf implemented an extra logic
      that, when e_shnum == 0 && e_shoff != 0, will use sh_size member of the
      initial section header as the real section header count (part of ELF
      spec to accommodate situation where section header counter is larger
      than SHN_LORESERVE).
      
      The above inconsistency lead to libbpf writing into a zero-entry calloc
      area. So intead of using e_shnum directly, use the elf_getshdrnum()
      helper provided by libelf to retrieve the section header counter into
      sec_cnt.
      
      Fixes: 0d6988e1 ("libbpf: Fix section counting logic")
      Fixes: 25bbbd7a ("libbpf: Remove assumptions about uniqueness of .rodata/.data/.bss maps")
      Signed-off-by: NShung-Hsi Yu <shung-hsi.yu@suse.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=40868
      Link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=40957
      Link: https://lore.kernel.org/bpf/20221012022353.7350-2-shung-hsi.yu@suse.com
      51deedc9
    • X
      libbpf: Fix memory leak in parse_usdt_arg() · 0dc9254e
      Xu Kuohai 提交于
      In the arm64 version of parse_usdt_arg(), when sscanf returns 2, reg_name
      is allocated but not freed. Fix it.
      
      Fixes: 0f861992 ("libbpf: Usdt aarch64 arg parsing support")
      Signed-off-by: NXu Kuohai <xukuohai@huawei.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/bpf/20221011120108.782373-3-xukuohai@huaweicloud.com
      0dc9254e
    • X
      libbpf: Fix use-after-free in btf_dump_name_dups · 93c660ca
      Xu Kuohai 提交于
      ASAN reports an use-after-free in btf_dump_name_dups:
      
      ERROR: AddressSanitizer: heap-use-after-free on address 0xffff927006db at pc 0xaaaab5dfb618 bp 0xffffdd89b890 sp 0xffffdd89b928
      READ of size 2 at 0xffff927006db thread T0
          #0 0xaaaab5dfb614 in __interceptor_strcmp.part.0 (test_progs+0x21b614)
          #1 0xaaaab635f144 in str_equal_fn tools/lib/bpf/btf_dump.c:127
          #2 0xaaaab635e3e0 in hashmap_find_entry tools/lib/bpf/hashmap.c:143
          #3 0xaaaab635e72c in hashmap__find tools/lib/bpf/hashmap.c:212
          #4 0xaaaab6362258 in btf_dump_name_dups tools/lib/bpf/btf_dump.c:1525
          #5 0xaaaab636240c in btf_dump_resolve_name tools/lib/bpf/btf_dump.c:1552
          #6 0xaaaab6362598 in btf_dump_type_name tools/lib/bpf/btf_dump.c:1567
          #7 0xaaaab6360b48 in btf_dump_emit_struct_def tools/lib/bpf/btf_dump.c:912
          #8 0xaaaab6360630 in btf_dump_emit_type tools/lib/bpf/btf_dump.c:798
          #9 0xaaaab635f720 in btf_dump__dump_type tools/lib/bpf/btf_dump.c:282
          #10 0xaaaab608523c in test_btf_dump_incremental tools/testing/selftests/bpf/prog_tests/btf_dump.c:236
          #11 0xaaaab6097530 in test_btf_dump tools/testing/selftests/bpf/prog_tests/btf_dump.c:875
          #12 0xaaaab6314ed0 in run_one_test tools/testing/selftests/bpf/test_progs.c:1062
          #13 0xaaaab631a0a8 in main tools/testing/selftests/bpf/test_progs.c:1697
          #14 0xffff9676d214 in __libc_start_main ../csu/libc-start.c:308
          #15 0xaaaab5d65990  (test_progs+0x185990)
      
      0xffff927006db is located 11 bytes inside of 16-byte region [0xffff927006d0,0xffff927006e0)
      freed by thread T0 here:
          #0 0xaaaab5e2c7c4 in realloc (test_progs+0x24c7c4)
          #1 0xaaaab634f4a0 in libbpf_reallocarray tools/lib/bpf/libbpf_internal.h:191
          #2 0xaaaab634f840 in libbpf_add_mem tools/lib/bpf/btf.c:163
          #3 0xaaaab636643c in strset_add_str_mem tools/lib/bpf/strset.c:106
          #4 0xaaaab6366560 in strset__add_str tools/lib/bpf/strset.c:157
          #5 0xaaaab6352d70 in btf__add_str tools/lib/bpf/btf.c:1519
          #6 0xaaaab6353e10 in btf__add_field tools/lib/bpf/btf.c:2032
          #7 0xaaaab6084fcc in test_btf_dump_incremental tools/testing/selftests/bpf/prog_tests/btf_dump.c:232
          #8 0xaaaab6097530 in test_btf_dump tools/testing/selftests/bpf/prog_tests/btf_dump.c:875
          #9 0xaaaab6314ed0 in run_one_test tools/testing/selftests/bpf/test_progs.c:1062
          #10 0xaaaab631a0a8 in main tools/testing/selftests/bpf/test_progs.c:1697
          #11 0xffff9676d214 in __libc_start_main ../csu/libc-start.c:308
          #12 0xaaaab5d65990  (test_progs+0x185990)
      
      previously allocated by thread T0 here:
          #0 0xaaaab5e2c7c4 in realloc (test_progs+0x24c7c4)
          #1 0xaaaab634f4a0 in libbpf_reallocarray tools/lib/bpf/libbpf_internal.h:191
          #2 0xaaaab634f840 in libbpf_add_mem tools/lib/bpf/btf.c:163
          #3 0xaaaab636643c in strset_add_str_mem tools/lib/bpf/strset.c:106
          #4 0xaaaab6366560 in strset__add_str tools/lib/bpf/strset.c:157
          #5 0xaaaab6352d70 in btf__add_str tools/lib/bpf/btf.c:1519
          #6 0xaaaab6353ff0 in btf_add_enum_common tools/lib/bpf/btf.c:2070
          #7 0xaaaab6354080 in btf__add_enum tools/lib/bpf/btf.c:2102
          #8 0xaaaab6082f50 in test_btf_dump_incremental tools/testing/selftests/bpf/prog_tests/btf_dump.c:162
          #9 0xaaaab6097530 in test_btf_dump tools/testing/selftests/bpf/prog_tests/btf_dump.c:875
          #10 0xaaaab6314ed0 in run_one_test tools/testing/selftests/bpf/test_progs.c:1062
          #11 0xaaaab631a0a8 in main tools/testing/selftests/bpf/test_progs.c:1697
          #12 0xffff9676d214 in __libc_start_main ../csu/libc-start.c:308
          #13 0xaaaab5d65990  (test_progs+0x185990)
      
      The reason is that the key stored in hash table name_map is a string
      address, and the string memory is allocated by realloc() function, when
      the memory is resized by realloc() later, the old memory may be freed,
      so the address stored in name_map references to a freed memory, causing
      use-after-free.
      
      Fix it by storing duplicated string address in name_map.
      
      Fixes: 919d2b1d ("libbpf: Allow modification of BTF and add btf__add_str API")
      Signed-off-by: NXu Kuohai <xukuohai@huawei.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NMartin KaFai Lau <martin.lau@kernel.org>
      Link: https://lore.kernel.org/bpf/20221011120108.782373-2-xukuohai@huaweicloud.com
      93c660ca
  2. 11 10月, 2022 5 次提交
  3. 06 10月, 2022 1 次提交
  4. 01 10月, 2022 1 次提交
  5. 28 9月, 2022 1 次提交
  6. 27 9月, 2022 1 次提交
    • J
      libbpf: Fix the case of running as non-root with capabilities · 6a4ab886
      Jon Doron 提交于
      When running rootless with special capabilities like:
      FOWNER / DAC_OVERRIDE / DAC_READ_SEARCH
      
      The "access" API will not make the proper check if there is really
      access to a file or not.
      
      >From the access man page:
      "
      The check is done using the calling process's real UID and GID, rather
      than the effective IDs as is done when actually attempting an operation
      (e.g., open(2)) on the file.  Similarly, for the root user, the check
      uses the set of permitted capabilities  rather than the set of effective
      capabilities; ***and for non-root users, the check uses an empty set of
      capabilities.***
      "
      
      What that means is that for non-root user the access API will not do the
      proper validation if the process really has permission to a file or not.
      
      To resolve this this patch replaces all the access API calls with
      faccessat with AT_EACCESS flag.
      Signed-off-by: NJon Doron <jond@wiz.io>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220925070431.1313680-1-arilou@gmail.com
      6a4ab886
  7. 24 9月, 2022 2 次提交
  8. 22 9月, 2022 4 次提交
    • T
      libbpf: Support raw BTF placed in the default search path · 01f2e36c
      Tao Chen 提交于
      Currently, the default vmlinux files at '/boot/vmlinux-*',
      '/lib/modules/*/vmlinux-*' etc. are parsed with 'btf__parse_elf()' to
      extract BTF. It is possible that these files are actually raw BTF files
      similar to /sys/kernel/btf/vmlinux. So parse these files with
      'btf__parse' which tries both raw format and ELF format.
      
      This might be useful in some scenarios where users put their custom BTF
      into known locations and don't want to specify btf_custom_path option.
      Signed-off-by: NTao Chen <chentao.kernel@linux.alibaba.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/bpf/3f59fb5a345d2e4f10e16fe9e35fbc4c03ecaa3e.1662999860.git.chentao.kernel@linux.alibaba.com
      01f2e36c
    • Y
      libbpf: Improve BPF_PROG2 macro code quality and description · 9f2f5d78
      Yonghong Song 提交于
      Commit 34586d29 ("libbpf: Add new BPF_PROG2 macro") added BPF_PROG2
      macro for trampoline based programs with struct arguments. Andrii
      made a few suggestions to improve code quality and description.
      This patch implemented these suggestions including better internal
      macro name, consistent usage pattern for __builtin_choose_expr(),
      simpler macro definition for always-inline func arguments and
      better macro description.
      Signed-off-by: NYonghong Song <yhs@fb.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/bpf/20220910025214.1536510-1-yhs@fb.com
      9f2f5d78
    • D
      bpf: Add libbpf logic for user-space ring buffer · b66ccae0
      David Vernet 提交于
      Now that all of the logic is in place in the kernel to support user-space
      produced ring buffers, we can add the user-space logic to libbpf. This
      patch therefore adds the following public symbols to libbpf:
      
      struct user_ring_buffer *
      user_ring_buffer__new(int map_fd,
      		      const struct user_ring_buffer_opts *opts);
      void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
      void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
                                               __u32 size, int timeout_ms);
      void user_ring_buffer__submit(struct user_ring_buffer *rb, void *sample);
      void user_ring_buffer__discard(struct user_ring_buffer *rb,
      void user_ring_buffer__free(struct user_ring_buffer *rb);
      
      A user-space producer must first create a struct user_ring_buffer * object
      with user_ring_buffer__new(), and can then reserve samples in the
      ring buffer using one of the following two symbols:
      
      void *user_ring_buffer__reserve(struct user_ring_buffer *rb, __u32 size);
      void *user_ring_buffer__reserve_blocking(struct user_ring_buffer *rb,
                                               __u32 size, int timeout_ms);
      
      With user_ring_buffer__reserve(), a pointer to a 'size' region of the ring
      buffer will be returned if sufficient space is available in the buffer.
      user_ring_buffer__reserve_blocking() provides similar semantics, but will
      block for up to 'timeout_ms' in epoll_wait if there is insufficient space
      in the buffer. This function has the guarantee from the kernel that it will
      receive at least one event-notification per invocation to
      bpf_ringbuf_drain(), provided that at least one sample is drained, and the
      BPF program did not pass the BPF_RB_NO_WAKEUP flag to bpf_ringbuf_drain().
      
      Once a sample is reserved, it must either be committed to the ring buffer
      with user_ring_buffer__submit(), or discarded with
      user_ring_buffer__discard().
      Signed-off-by: NDavid Vernet <void@manifault.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220920000100.477320-4-void@manifault.com
      b66ccae0
    • D
      bpf: Define new BPF_MAP_TYPE_USER_RINGBUF map type · 583c1f42
      David Vernet 提交于
      We want to support a ringbuf map type where samples are published from
      user-space, to be consumed by BPF programs. BPF currently supports a
      kernel -> user-space circular ring buffer via the BPF_MAP_TYPE_RINGBUF
      map type.  We'll need to define a new map type for user-space -> kernel,
      as none of the helpers exported for BPF_MAP_TYPE_RINGBUF will apply
      to a user-space producer ring buffer, and we'll want to add one or
      more helper functions that would not apply for a kernel-producer
      ring buffer.
      
      This patch therefore adds a new BPF_MAP_TYPE_USER_RINGBUF map type
      definition. The map type is useless in its current form, as there is no
      way to access or use it for anything until we one or more BPF helpers. A
      follow-on patch will therefore add a new helper function that allows BPF
      programs to run callbacks on samples that are published to the ring
      buffer.
      Signed-off-by: NDavid Vernet <void@manifault.com>
      Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
      Acked-by: NAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20220920000100.477320-2-void@manifault.com
      583c1f42
  9. 21 9月, 2022 1 次提交
  10. 17 9月, 2022 2 次提交
  11. 09 9月, 2022 1 次提交
    • D
      libbpf: Remove gcc support for bpf_tail_call_static for now · 665f5d35
      Daniel Borkmann 提交于
      This reverts commit 14e5ce79 ("libbpf: Add GCC support for
      bpf_tail_call_static"). Reason is that gcc invented their own BPF asm
      which is not conform with LLVM one, and going forward this would be
      more painful to maintain here and in other areas of the library. Thus
      remove it; ask to gcc folks is to align with LLVM one to use exact
      same syntax.
      
      Fixes: 14e5ce79 ("libbpf: Add GCC support for bpf_tail_call_static")
      Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
      Cc: James Hilliard <james.hilliard1@gmail.com>
      Cc: Jose E. Marchesi <jose.marchesi@oracle.com>
      665f5d35
  12. 07 9月, 2022 1 次提交
    • Y
      libbpf: Add new BPF_PROG2 macro · 34586d29
      Yonghong Song 提交于
      To support struct arguments in trampoline based programs,
      existing BPF_PROG doesn't work any more since
      the type size is needed to find whether a parameter
      takes one or two registers. So this patch added a new
      BPF_PROG2 macro to support such trampoline programs.
      
      The idea is suggested by Andrii. For example, if the
      to-be-traced function has signature like
        typedef struct {
             void *x;
             int t;
        } sockptr;
        int blah(sockptr x, char y);
      
      In the new BPF_PROG2 macro, the argument can be
      represented as
        __bpf_prog_call(
           ({ union {
                struct { __u64 x, y; } ___z;
                sockptr x;
              } ___tmp = { .___z = { ctx[0], ctx[1] }};
              ___tmp.x;
           }),
           ({ union {
                struct { __u8 x; } ___z;
                char y;
              } ___tmp = { .___z = { ctx[2] }};
              ___tmp.y;
           }));
      In the above, the values stored on the stack are properly
      assigned to the actual argument type value by using 'union'
      magic. Note that the macro also works even if no arguments
      are with struct types.
      
      Note that new BPF_PROG2 works for both llvm16 and pre-llvm16
      compilers where llvm16 supports bpf target passing value
      with struct up to 16 byte size and pre-llvm16 will pass
      by reference by storing values on the stack. With static functions
      with struct argument as always inline, the compiler is able
      to optimize and remove additional stack saving of struct values.
      Signed-off-by: NYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20220831152707.2079473-1-yhs@fb.comSigned-off-by: NAlexei Starovoitov <ast@kernel.org>
      34586d29
  13. 01 9月, 2022 1 次提交
  14. 26 8月, 2022 1 次提交
  15. 18 8月, 2022 4 次提交
  16. 16 8月, 2022 1 次提交
  17. 12 8月, 2022 1 次提交
  18. 11 8月, 2022 2 次提交
  19. 09 8月, 2022 1 次提交
  20. 08 8月, 2022 1 次提交
  21. 05 8月, 2022 3 次提交