1. 20 Apr, 2024 6 commits
  2. 18 Apr, 2024 1 commit
  3. 17 Apr, 2024 2 commits
    • Quentin Deslandes's avatar
      libbpf: Fix dump of subsequent char arrays · e739e01d
      Quentin Deslandes authored
      When dumping a character array, libbpf will watch for a '\0' and set
      is_array_terminated=true if found. This prevents libbpf from printing
      the remaining characters of the array, treating it as a nul-terminated
      string.
      
      However, once this flag is set, it's never reset, leading to subsequent
      characters array not being printed properly:
      
      .str_multi = (__u8[2][16])[
          [
              'H',
              'e',
              'l',
          ],
      ],
      
      This patch saves the is_array_terminated flag and restores its
      default (false) value before looping over the elements of an array,
      then restores it afterward. This way, libbpf's behavior is unchanged
      when dumping the characters of an array, but subsequent arrays are
      printed properly:
      
      .str_multi = (__u8[2][16])[
          [
              'H',
              'e',
              'l',
          ],
          [
              'l',
              'o',
          ],
      ],
      Signed-off-by: default avatarQuentin Deslandes <qde@naccy.de>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240413211258.134421-3-qde@naccy.de
      e739e01d
    • Quentin Deslandes's avatar
      libbpf: Fix misaligned array closing bracket · 9213e529
      Quentin Deslandes authored
      In btf_dump_array_data(), libbpf will call btf_dump_dump_type_data() for
      each element. For an array of characters, each element will be
      processed the following way:
      
      - btf_dump_dump_type_data() is called to print the character
      - btf_dump_data_pfx() prefixes the current line with the proper number
        of indentations
      - btf_dump_int_data() is called to print the character
      - After the last character is printed, btf_dump_dump_type_data() calls
        btf_dump_data_pfx() before writing the closing bracket
      
      However, for an array containing characters, btf_dump_int_data() won't
      print any '\0' and subsequent characters. This leads to situations where
      the line prefix is written, no character is added, then the prefix is
      written again before adding the closing bracket:
      
      (struct sk_metadata){
          .str_array = (__u8[14])[
              'H',
              'e',
              'l',
              'l',
              'o',
                      ],
      
      This change solves this issue by printing the '\0' character, which
      has two benefits:
      
      - The bracket closing the array is properly aligned
      - It's clear from a user point of view that libbpf uses '\0' as a
        terminator for arrays of characters.
      Signed-off-by: default avatarQuentin Deslandes <qde@naccy.de>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240413211258.134421-2-qde@naccy.de
      9213e529
  4. 16 Apr, 2024 5 commits
  5. 12 Apr, 2024 2 commits
  6. 11 Apr, 2024 11 commits
  7. 09 Apr, 2024 2 commits
  8. 08 Apr, 2024 1 commit
  9. 06 Apr, 2024 4 commits
  10. 05 Apr, 2024 6 commits
    • Andrii Nakryiko's avatar
      Merge branch 'bpf-allow-invoking-kfuncs-from-bpf_prog_type_syscall-progs' · d564ffde
      Andrii Nakryiko authored
      David Vernet says:
      
      ====================
      bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs
      
      Currently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*,
      bpf_cpumask_*, etc) cannot be invoked from BPF_PROG_TYPE_SYSCALL
      programs. The whitelist approach taken for enabling kfuncs makes sense:
      it not safe to call these kfuncs from every program type. For example,
      it may not be safe to call bpf_task_acquire() in an fentry to
      free_task().
      
      BPF_PROG_TYPE_SYSCALL, on the other hand, is a perfectly safe program
      type from which to invoke these kfuncs, as it's a very controlled
      environment, and we should never be able to run into any of the typical
      problems such as recursive invoations, acquiring references on freeing
      kptrs, etc. Being able to invoke these kfuncs would be useful, as
      BPF_PROG_TYPE_SYSCALL can be invoked with BPF_PROG_RUN, and would
      therefore enable user space programs to synchronously call into BPF to
      manipulate these kptrs.
      ---
      
      v1: https://lore.kernel.org/all/20240404010308.334604-1-void@manifault.com/
      v1 -> v2:
      
      - Create new verifier_kfunc_prog_types testcase meant to specifically
        validate calling core kfuncs from various program types. Remove the
        macros and testcases that had been added to the task, cgrp, and
        cpumask kfunc testcases (Andrii and Yonghong)
      ====================
      
      Link: https://lore.kernel.org/r/20240405143041.632519-1-void@manifault.comSigned-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      d564ffde
    • David Vernet's avatar
      selftests/bpf: Verify calling core kfuncs from BPF_PROG_TYPE_SYCALL · 1bc724af
      David Vernet authored
      Now that we can call some kfuncs from BPF_PROG_TYPE_SYSCALL progs, let's
      add some selftests that verify as much. As a bonus, let's also verify
      that we can't call the progs from raw tracepoints. Do do this, we add a
      new selftest suite called verifier_kfunc_prog_types.
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/bpf/20240405143041.632519-3-void@manifault.com
      1bc724af
    • David Vernet's avatar
      bpf: Allow invoking kfuncs from BPF_PROG_TYPE_SYSCALL progs · a8e03b6b
      David Vernet authored
      Currently, a set of core BPF kfuncs (e.g. bpf_task_*, bpf_cgroup_*,
      bpf_cpumask_*, etc) cannot be invoked from BPF_PROG_TYPE_SYSCALL
      programs. The whitelist approach taken for enabling kfuncs makes sense:
      it not safe to call these kfuncs from every program type. For example,
      it may not be safe to call bpf_task_acquire() in an fentry to
      free_task().
      
      BPF_PROG_TYPE_SYSCALL, on the other hand, is a perfectly safe program
      type from which to invoke these kfuncs, as it's a very controlled
      environment, and we should never be able to run into any of the typical
      problems such as recursive invoations, acquiring references on freeing
      kptrs, etc. Being able to invoke these kfuncs would be useful, as
      BPF_PROG_TYPE_SYSCALL can be invoked with BPF_PROG_RUN, and would
      therefore enable user space programs to synchronously call into BPF to
      manipulate these kptrs.
      
      This patch therefore enables invoking the aforementioned core kfuncs
      from BPF_PROG_TYPE_SYSCALL progs.
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarYonghong Song <yonghong.song@linux.dev>
      Link: https://lore.kernel.org/bpf/20240405143041.632519-2-void@manifault.com
      a8e03b6b
    • Dave Thaler's avatar
      bpf, docs: Editorial nits in instruction-set.rst · 00d5d22a
      Dave Thaler authored
      This patch addresses a number of editorial nits including
      spelling, punctuation, grammar, and wording consistency issues
      in instruction-set.rst.
      Signed-off-by: default avatarDave Thaler <dthaler1968@gmail.com>
      Acked-by: default avatarDavid Vernet <void@manifault.com>
      Link: https://lore.kernel.org/r/20240405155245.3618-1-dthaler1968@gmail.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      00d5d22a
    • Kui-Feng Lee's avatar
      selftests/bpf: Make sure libbpf doesn't enforce the signature of a func pointer. · ba0cbe2b
      Kui-Feng Lee authored
      The verifier in the kernel ensures that the struct_ops operators behave
      correctly by checking that they access parameters and context
      appropriately. The verifier will approve a program as long as it correctly
      accesses the context/parameters, regardless of its function signature. In
      contrast, libbpf should not verify the signature of function pointers and
      functions to enable flexibility in loading various implementations of an
      operator even if the signature of the function pointer does not match those
      in the implementations or the kernel.
      
      With this flexibility, user space applications can adapt to different
      kernel versions by loading a specific implementation of an operator based
      on feature detection.
      
      This is a follow-up of the commit c911fc61 ("libbpf: Skip zeroed or
      null fields if not found in the kernel type.")
      Signed-off-by: default avatarKui-Feng Lee <thinker.li@gmail.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20240404232342.991414-1-thinker.li@gmail.com
      ba0cbe2b
    • Alexei Starovoitov's avatar
      Merge branch 'bpf-allow-bpf_for_each_map_elem-helper-with-different-input-maps' · 27095479
      Alexei Starovoitov authored
      Philo Lu says:
      
      ====================
      bpf: allow bpf_for_each_map_elem() helper with different input maps
      
      Currently, taking different maps within a single bpf_for_each_map_elem
      call is not allowed. For example the following codes cannot pass the
      verifier (with error "tail_call abusing map_ptr"):
      ```
      static void test_by_pid(int pid)
      {
      	if (pid <= 100)
      		bpf_for_each_map_elem(&map1, map_elem_cb, NULL, 0);
      	else
      		bpf_for_each_map_elem(&map2, map_elem_cb, NULL, 0);
      }
      ```
      
      This is because during bpf_for_each_map_elem verifying,
      bpf_insn_aux_data->map_ptr_state is expected as map_ptr (instead of poison
      state), which is then needed by set_map_elem_callback_state. However, as
      there are two different map ptr input, map_ptr_state is marked as
      BPF_MAP_PTR_POISON, and thus the second map_ptr would be lost.
      BPF_MAP_PTR_POISON is also needed by bpf_for_each_map_elem to skip
      retpoline optimization in do_misc_fixups(). Therefore, map_ptr_state and
      map_ptr are both needed for bpf_for_each_map_elem.
      
      This patchset solves it by transform bpf_insn_aux_data->map_ptr_state as a
      new struct, storing poison/unpriv state and map pointer together without
      additional memory overhead. Then bpf_for_each_map_elem works well with
      different input maps. It also makes map_ptr_state logic clearer.
      
      A test case is added to selftest, which would fail to load without this
      patchset.
      
      Changelogs
      -> v1:
      - PATCH 1/3:
        - make the commit log clearer
        - change poison and unpriv to bool in struct bpf_map_ptr_state, also the
          return value in bpf_map_ptr_poisoned() and bpf_map_ptr_unpriv()
      - PATCH 2/3:
        - change the comments in set_map_elem_callback_state()
      - PATCH 3/3:
        - remove the "skipping the last element" logic during map updating
        - change if() to ASSERT_OK()
      
      Please review, thanks.
      ====================
      
      Link: https://lore.kernel.org/r/20240405025536.18113-1-lulie@linux.alibaba.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      27095479