1. 24 Apr, 2024 23 commits
  2. 22 Apr, 2024 2 commits
  3. 21 Apr, 2024 2 commits
  4. 20 Apr, 2024 7 commits
  5. 18 Apr, 2024 1 commit
  6. 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
  7. 16 Apr, 2024 3 commits
    • Quentin Monnet's avatar
      bpftool: Address minor issues in bash completion · ad2d22b6
      Quentin Monnet authored
      This commit contains a series of clean-ups and fixes for bpftool's bash
      completion file:
      
      - Make sure all local variables are declared as such.
      - Make sure variables are initialised before being read.
      - Update ELF section ("maps" -> ".maps") for looking up map names in
        object files.
      - Fix call to _init_completion.
      - Move definition for MAP_TYPE and PROG_TYPE higher up in the scope to
        avoid defining them multiple times, reuse MAP_TYPE where relevant.
      - Simplify completion for "duration" keyword in "bpftool prog profile".
      - Fix completion for "bpftool struct_ops register" and "bpftool link
        (pin|detach)" where we would repeatedly suggest file names instead of
        suggesting just one name.
      - Fix completion for "bpftool iter pin ... map MAP" to account for the
        "map" keyword.
      - Add missing "detach" suggestion for "bpftool link".
      Signed-off-by: default avatarQuentin Monnet <qmo@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240413011427.14402-3-qmo@kernel.org
      ad2d22b6
    • Quentin Monnet's avatar
      bpftool: Update documentation where progs/maps can be passed by name · 986e7663
      Quentin Monnet authored
      When using references to BPF programs, bpftool supports passing programs
      by name on the command line. The manual pages for "bpftool prog" and
      "bpftool map" (for prog_array updates) mention it, but we have a few
      additional subcommands that support referencing programs by name but do
      not mention it in their documentation. Let's update the pages for
      subcommands "btf", "cgroup", and "net".
      
      Similarly, we can reference maps by name when passing them to "bpftool
      prog load", so we update the page for "bpftool prog" as well.
      Signed-off-by: default avatarQuentin Monnet <qmo@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240413011427.14402-2-qmo@kernel.org
      986e7663
    • Harishankar Vishwanathan's avatar
      bpf: Harden and/or/xor value tracking in verifier · 1f586614
      Harishankar Vishwanathan authored
      This patch addresses a latent unsoundness issue in the
      scalar(32)_min_max_and/or/xor functions. While it is not a bugfix,
      it ensures that the functions produce sound outputs for all inputs.
      
      The issue occurs in these functions when setting signed bounds. The
      following example illustrates the issue for scalar_min_max_and(),
      but it applies to the other functions.
      
      In scalar_min_max_and() the following clause is executed when ANDing
      positive numbers:
      
        /* ANDing two positives gives a positive, so safe to
         * cast result into s64.
         */
        dst_reg->smin_value = dst_reg->umin_value;
        dst_reg->smax_value = dst_reg->umax_value;
      
      However, if umin_value and umax_value of dst_reg cross the sign boundary
      (i.e., if (s64)dst_reg->umin_value > (s64)dst_reg->umax_value), then we
      will end up with smin_value > smax_value, which is unsound.
      
      Previous works [1, 2] have discovered and reported this issue. Our tool
      Agni [2, 3] consideres it a false positive. This is because, during the
      verification of the abstract operator scalar_min_max_and(), Agni restricts
      its inputs to those passing through reg_bounds_sync(). This mimics
      real-world verifier behavior, as reg_bounds_sync() is invariably executed
      at the tail of every abstract operator. Therefore, such behavior is
      unlikely in an actual verifier execution.
      
      However, it is still unsound for an abstract operator to set signed bounds
      such that smin_value > smax_value. This patch fixes it, making the abstract
      operator sound for all (well-formed) inputs.
      
      It is worth noting that while the previous code updated the signed bounds
      (using the output unsigned bounds) only when the *input signed* bounds
      were positive, the new code updates them whenever the *output unsigned*
      bounds do not cross the sign boundary.
      
      An alternative approach to fix this latent unsoundness would be to
      unconditionally set the signed bounds to unbounded [S64_MIN, S64_MAX], and
      let reg_bounds_sync() refine the signed bounds using the unsigned bounds
      and the tnum. We found that our approach produces more precise (tighter)
      bounds.
      
      For example, consider these inputs to BPF_AND:
      
        /* dst_reg */
        var_off.value: 8608032320201083347
        var_off.mask: 615339716653692460
        smin_value: 8070450532247928832
        smax_value: 8070450532247928832
        umin_value: 13206380674380886586
        umax_value: 13206380674380886586
        s32_min_value: -2110561598
        s32_max_value: -133438816
        u32_min_value: 4135055354
        u32_max_value: 4135055354
      
        /* src_reg */
        var_off.value: 8584102546103074815
        var_off.mask: 9862641527606476800
        smin_value: 2920655011908158522
        smax_value: 7495731535348625717
        umin_value: 7001104867969363969
        umax_value: 8584102543730304042
        s32_min_value: -2097116671
        s32_max_value: 71704632
        u32_min_value: 1047457619
        u32_max_value: 4268683090
      
      After going through tnum_and() -> scalar32_min_max_and() ->
      scalar_min_max_and() -> reg_bounds_sync(), our patch produces the following
      bounds for s32:
      
        s32_min_value: -1263875629
        s32_max_value: -159911942
      
      Whereas, setting the signed bounds to unbounded in scalar_min_max_and()
      produces:
      
        s32_min_value: -1263875629
        s32_max_value: -1
      
      As observed, our patch produces a tighter s32 bound. We also confirmed
      using Agni and SMT verification that our patch always produces signed
      bounds that are equal to or more precise than setting the signed bounds to
      unbounded in scalar_min_max_and().
      
        [1] https://sanjit-bhat.github.io/assets/pdf/ebpf-verifier-range-analysis22.pdf
        [2] https://link.springer.com/chapter/10.1007/978-3-031-37709-9_12
        [3] https://github.com/bpfverif/agniCo-developed-by: default avatarMatan Shachnai <m.shachnai@rutgers.edu>
      Signed-off-by: default avatarMatan Shachnai <m.shachnai@rutgers.edu>
      Co-developed-by: default avatarSrinivas Narayana <srinivas.narayana@rutgers.edu>
      Signed-off-by: default avatarSrinivas Narayana <srinivas.narayana@rutgers.edu>
      Co-developed-by: default avatarSantosh Nagarakatte <santosh.nagarakatte@rutgers.edu>
      Signed-off-by: default avatarSantosh Nagarakatte <santosh.nagarakatte@rutgers.edu>
      Signed-off-by: default avatarHarishankar Vishwanathan <harishankar.vishwanathan@gmail.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20240402212039.51815-1-harishankar.vishwanathan@gmail.com
      Link: https://lore.kernel.org/bpf/20240416115303.331688-1-harishankar.vishwanathan@gmail.com
      1f586614