Skip to content
  1. Dec 07, 2022
  2. Dec 06, 2022
  3. Dec 05, 2022
    • James Hilliard's avatar
      selftests/bpf: Fix conflicts with built-in functions in bpf_iter_ksym · ab0350c7
      James Hilliard authored
      
      
      Both tolower and toupper are built in c functions, we should not
      redefine them as this can result in a build error.
      
      Fixes the following errors:
      progs/bpf_iter_ksym.c:10:20: error: conflicting types for built-in function 'tolower'; expected 'int(int)' [-Werror=builtin-declaration-mismatch]
         10 | static inline char tolower(char c)
            |                    ^~~~~~~
      progs/bpf_iter_ksym.c:5:1: note: 'tolower' is declared in header '<ctype.h>'
          4 | #include <bpf/bpf_helpers.h>
        +++ |+#include <ctype.h>
          5 |
      progs/bpf_iter_ksym.c:17:20: error: conflicting types for built-in function 'toupper'; expected 'int(int)' [-Werror=builtin-declaration-mismatch]
         17 | static inline char toupper(char c)
            |                    ^~~~~~~
      progs/bpf_iter_ksym.c:17:20: note: 'toupper' is declared in header '<ctype.h>'
      
      See background on this sort of issue:
      https://stackoverflow.com/a/20582607
      https://gcc.gnu.org/bugzilla/show_bug.cgi?id=12213
      
      (C99, 7.1.3p1) "All identifiers with external linkage in any of the
      following subclauses (including the future library directions) are
      always reserved for use as identifiers with external linkage."
      
      This is documented behavior in GCC:
      https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-std-2
      
      Signed-off-by: default avatarJames Hilliard <james.hilliard1@gmail.com>
      Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/r/20221203010847.2191265-1-james.hilliard1@gmail.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      ab0350c7
    • Eric Dumazet's avatar
      bpf, sockmap: fix race in sock_map_free() · 0a182f8d
      Eric Dumazet authored
      sock_map_free() calls release_sock(sk) without owning a reference
      on the socket. This can cause use-after-free as syzbot found [1]
      
      Jakub Sitnicki already took care of a similar issue
      in sock_hash_free() in commit 75e68e5b ("bpf, sockhash:
      Synchronize delete from bucket list on map free")
      
      [1]
      refcount_t: decrement hit 0; leaking memory.
      WARNING: CPU: 0 PID: 3785 at lib/refcount.c:31 refcount_warn_saturate+0x17c/0x1a0 lib/refcount.c:31
      Modules linked in:
      CPU: 0 PID: 3785 Comm: kworker/u4:6 Not tainted 6.1.0-rc7-syzkaller-00103-gef4d3ea40565 #0
      Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 10/26/2022
      Workqueue: events_unbound bpf_map_free_deferred
      RIP: 0010:refcount_warn_saturate+0x17c/0x1a0 lib/refcount.c:31
      Code: 68 8b 31 c0 e8 75 71 15 fd 0f 0b e9 64 ff ff ff e8 d9 6e 4e fd c6 05 62 9c 3d 0a 01 48 c7 c7 80 bb 68 8b 31 c0 e8 54 71 15 fd <0f> 0b e9 43 ff ff ff 89 d9 80 e1 07 80 c1 03 38 c1 0f 8c a2 fe ff
      RSP: 0018:ffffc9000456fb60 EFLAGS: 00010246
      RAX: eae59bab72dcd700 RBX: 0000000000000004 RCX: ffff8880207057c0
      RDX: 0000000000000000 RSI: 0000000000000201 RDI: 0000000000000000
      RBP: 0000000000000004 R08: ffffffff816fdabd R09: fffff520008adee5
      R10: fffff520008adee5 R11: 1ffff920008adee4 R12: 0000000000000004
      R13: dffffc0000000000 R14: ffff88807b1c6c00 R15: 1ffff1100f638dcf
      FS: 0000000000000000(0000) GS:ffff8880b9800000(0000) knlGS:0000000000000000
      CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      CR2: 0000001b30c30000 CR3: 000000000d08e000 CR4: 00000000003506f0
      DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
      DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
      Call Trace:
      <TASK>
      __refcount_dec include/linux/refcount.h:344 [inline]
      refcount_dec include/linux/refcount.h:359 [inline]
      __sock_put include/net/sock.h:779 [inline]
      tcp_release_cb+0x2d0/0x360 net/ipv4/tcp_output.c:1092
      release_sock+0xaf/0x1c0 net/core/sock.c:3468
      sock_map_free+0x219/0x2c0 net/core/sock_map.c:356
      process_one_work+0x81c/0xd10 kernel/workqueue.c:2289
      worker_thread+0xb14/0x1330 kernel/workqueue.c:2436
      kthread+0x266/0x300 kernel/kthread.c:376
      ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:306
      </TASK>
      
      Fixes: 7e81a353
      
       ("bpf: Sockmap, ensure sock lock held during tear down")
      Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
      Reported-by: default avatarsyzbot <syzkaller@googlegroups.com>
      Cc: Jakub Sitnicki <jakub@cloudflare.com>
      Cc: John Fastabend <john.fastabend@gmail.com>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Daniel Borkmann <daniel@iogearbox.net>
      Cc: Song Liu <songliubraving@fb.com>
      Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
      Link: https://lore.kernel.org/r/20221202111640.2745533-1-edumazet@google.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      0a182f8d
    • Toke Høiland-Jørgensen's avatar
      bpf: Add dummy type reference to nf_conn___init to fix type deduplication · 578ce69f
      Toke Høiland-Jørgensen authored
      The bpf_ct_set_nat_info() kfunc is defined in the nf_nat.ko module, and
      takes as a parameter the nf_conn___init struct, which is allocated through
      the bpf_xdp_ct_alloc() helper defined in the nf_conntrack.ko module.
      However, because kernel modules can't deduplicate BTF types between each
      other, and the nf_conn___init struct is not referenced anywhere in vmlinux
      BTF, this leads to two distinct BTF IDs for the same type (one in each
      module). This confuses the verifier, as described here:
      
      https://lore.kernel.org/all/87leoh372s.fsf@toke.dk/
      
      As a workaround, add an explicit BTF_TYPE_EMIT for the type in
      net/filter.c, so the type definition gets included in vmlinux BTF. This
      way, both modules can refer to the same type ID (as they both build on top
      of vmlinux BTF), and the verifier is no longer confused.
      
      v2:
      
      - Use BTF_TYPE_EMIT (which is a statement so it has to be inside a function
        definition; use xdp_func_proto() for this, since this is mostly
        xdp-related).
      
      Fixes: 820dc052
      
       ("net: netfilter: move bpf_ct_set_nat_info kfunc in nf_nat_bpf.c")
      Signed-off-by: default avatarToke Høiland-Jørgensen <toke@redhat.com>
      Link: https://lore.kernel.org/r/20221201123939.696558-1-toke@redhat.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      578ce69f
    • Yonghong Song's avatar
      bpf: Add sleepable prog tests for cgrp local storage · 41d76c72
      Yonghong Song authored
      
      
      Add three tests for cgrp local storage support for sleepable progs.
      Two tests can load and run properly, one for cgroup_iter, another
      for passing current->cgroups->dfl_cgrp to bpf_cgrp_storage_get()
      helper. One test has bpf_rcu_read_lock() and failed to load.
      
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221201050449.2785613-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      41d76c72
    • Yonghong Song's avatar
      bpf: Enable sleeptable support for cgrp local storage · 2c40d97d
      Yonghong Song authored
      
      
      Similar to sk/inode/task local storage, enable sleepable support for
      cgrp local storage.
      
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221201050444.2785007-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      2c40d97d
    • Sreevani Sreejith's avatar
      bpf, docs: BPF Iterator Document · 8972e18a
      Sreevani Sreejith authored
      
      
      Document that describes how BPF iterators work, how to use iterators,
      and how to pass parameters in BPF iterators.
      
      Acked-by: default avatarDavid Vernet <void@manifault.com>
      Signed-off-by: default avatarSreevani Sreejith <psreep@gmail.com>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221202221710.320810-2-ssreevani@meta.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      8972e18a
    • Yonghong Song's avatar
      bpf: Do not mark certain LSM hook arguments as trusted · c0c852dd
      Yonghong Song authored
      Martin mentioned that the verifier cannot assume arguments from
      LSM hook sk_alloc_security being trusted since after the hook
      is called, the sk ref_count is set to 1. This will overwrite
      the ref_count changed by the bpf program and may cause ref_count
      underflow later on.
      
      I then further checked some other hooks. For example,
      for bpf_lsm_file_alloc() hook in fs/file_table.c,
      
              f->f_cred = get_cred(cred);
              error = security_file_alloc(f);
              if (unlikely(error)) {
                      file_free_rcu(&f->f_rcuhead);
                      return ERR_PTR(error);
              }
      
              atomic_long_set(&f->f_count, 1);
      
      The input parameter 'f' to security_file_alloc() cannot be trusted
      as well.
      
      Specifically, I investiaged bpf_map/bpf_prog/file/sk/task alloc/free
      lsm hooks. Except bpf_map_alloc and task_alloc, arguments for all other
      hooks should not be considered as trusted. This may not be a complete
      list, but it covers common usage for sk and task.
      
      Fixes: 3f00c523
      
       ("bpf: Allow trusted pointers to be passed to KF_TRUSTED_ARGS kfuncs")
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221203204954.2043348-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      c0c852dd
    • Alexei Starovoitov's avatar
      Merge branch 'bpf: Handle MEM_RCU type properly' · 1910676c
      Alexei Starovoitov authored
      
      
      Yonghong Song says:
      
      ====================
      
      Patch set [1] added rcu support for bpf programs. In [1], a rcu
      pointer is considered to be trusted and not null. This is actually
      not true in some cases. The rcu pointer could be null, and for non-null
      rcu pointer, it may have reference count of 0. This small patch set
      fixed this problem. Patch 1 is the kernel fix. Patch 2 adjusted
      selftests properly. Patch 3 added documentation for newly-introduced
      KF_RCU flag.
      
        [1] https://lore.kernel.org/all/20221124053201.2372298-1-yhs@fb.com/
      
      Changelogs:
        v1 -> v2:
          - rcu ptr could be NULL.
          - non_null_rcu_ptr->rcu_field can be marked as MEM_RCU as well.
          - Adjust the code to avoid existing error message change.
      ====================
      
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      1910676c
    • Yonghong Song's avatar
      docs/bpf: Add KF_RCU documentation · f5362564
      Yonghong Song authored
      
      
      Add proper KF_RCU documentation in kfuncs.rst.
      
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221203184613.478967-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      f5362564
    • Yonghong Song's avatar
      selftests/bpf: Fix rcu_read_lock test with new MEM_RCU semantics · 8723ec22
      Yonghong Song authored
      
      
      Add MEM_RCU pointer null checking for related tests. Also
      modified task_acquire test so it takes a rcu ptr 'ptr' where
      'ptr = rcu_ptr->rcu_field'.
      
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221203184607.478314-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      8723ec22
    • Yonghong Song's avatar
      bpf: Handle MEM_RCU type properly · fca1aa75
      Yonghong Song authored
      Commit 9bb00b28 ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
      introduced MEM_RCU and bpf_rcu_read_lock/unlock() support. In that
      commit, a rcu pointer is tagged with both MEM_RCU and PTR_TRUSTED
      so that it can be passed into kfuncs or helpers as an argument.
      
      Martin raised a good question in [1] such that the rcu pointer,
      although being able to accessing the object, might have reference
      count of 0. This might cause a problem if the rcu pointer is passed
      to a kfunc which expects trusted arguments where ref count should
      be greater than 0.
      
      This patch makes the following changes related to MEM_RCU pointer:
        - MEM_RCU pointer might be NULL (PTR_MAYBE_NULL).
        - Introduce KF_RCU so MEM_RCU ptr can be acquired with
          a KF_RCU tagged kfunc which assumes ref count of rcu ptr
          could be zero.
        - For mem access 'b = ptr->a', say 'ptr' is a MEM_RCU ptr, and
          'a' is tagged with __rcu as well. Let us mark 'b' as
          MEM_RCU | PTR_MAYBE_NULL.
      
       [1] https://lore.kernel.org/bpf/ac70f574-4023-664e-b711-e0d3b18117fd@linux.dev/
      
      Fixes: 9bb00b28
      
       ("bpf: Add kfunc bpf_rcu_read_lock/unlock()")
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221203184602.477272-1-yhs@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      fca1aa75
  4. Dec 03, 2022
  5. Dec 02, 2022
    • Dave Marchevsky's avatar
      selftests/bpf: Validate multiple ref release_on_unlock logic · 78b037bd
      Dave Marchevsky authored
      
      
      Modify list_push_pop_multiple to alloc and insert nodes 2-at-a-time.
      Without the previous patch's fix, this block of code:
      
        bpf_spin_lock(lock);
        bpf_list_push_front(head, &f[i]->node);
        bpf_list_push_front(head, &f[i + 1]->node);
        bpf_spin_unlock(lock);
      
      would fail check_reference_leak check as release_on_unlock logic would miss
      a ref that should've been released.
      
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/r/20221201183406.1203621-2-davemarchevsky@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      78b037bd
    • Dave Marchevsky's avatar
      bpf: Fix release_on_unlock release logic for multiple refs · 1f82dffc
      Dave Marchevsky authored
      
      
      Consider a verifier state with three acquired references, all with
      release_on_unlock = true:
      
                  idx  0 1 2
        state->refs = [2 4 6]
      
      (with 2, 4, and 6 being the ref ids).
      
      When bpf_spin_unlock is called, process_spin_lock will loop through all
      acquired_refs and, for each ref, if it's release_on_unlock, calls
      release_reference on it. That function in turn calls
      release_reference_state, which removes the reference from state->refs by
      swapping the reference state with the last reference state in
      refs array and decrements acquired_refs count.
      
      process_spin_lock's loop logic, which is essentially:
      
        for (i = 0; i < state->acquired_refs; i++) {
          if (!state->refs[i].release_on_unlock)
            continue;
          release_reference(state->refs[i].id);
        }
      
      will fail to release release_on_unlock references which are swapped from
      the end. Running this logic on our example demonstrates:
      
        state->refs = [2 4 6] (start of idx=0 iter)
          release state->refs[0] by swapping w/ state->refs[2]
      
        state->refs = [6 4]   (start of idx=1)
          release state->refs[1], no need to swap as it's the last idx
      
        state->refs = [6]     (start of idx=2, loop terminates)
      
      ref_id 6 should have been removed but was skipped.
      
      Fix this by looping from back-to-front, which results in refs that are
      candidates for removal being swapped with refs which have already been
      examined and kept.
      
      If we modify our initial example such that ref 6 is replaced with ref 7,
      which is _not_ release_on_unlock, and loop from the back, we'd see:
      
        state->refs = [2 4 7] (start of idx=2)
      
        state->refs = [2 4 7] (start of idx=1)
      
        state->refs = [2 7]   (start of idx=0, refs 7 and 4 swapped)
      
        state->refs = [7]     (after idx=0, 7 and 2 swapped, loop terminates)
      
      Signed-off-by: default avatarDave Marchevsky <davemarchevsky@fb.com>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
      Fixes: 534e86bc
      
       ("bpf: Add 'release on unlock' logic for bpf_list_push_{front,back}")
      Link: https://lore.kernel.org/r/20221201183406.1203621-1-davemarchevsky@fb.com
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      1f82dffc
  6. Dec 01, 2022
    • Yonghong Song's avatar
      bpf: Fix a compilation failure with clang lto build · 3144bfa5
      Yonghong Song authored
      When building the kernel with clang lto (CONFIG_LTO_CLANG_FULL=y), the
      following compilation error will appear:
      
        $ make LLVM=1 LLVM_IAS=1 -j
        ...
        ld.lld: error: ld-temp.o <inline asm>:26889:1: symbol 'cgroup_storage_map_btf_ids' is already defined
        cgroup_storage_map_btf_ids:;
        ^
        make[1]: *** [/.../bpf-next/scripts/Makefile.vmlinux_o:61: vmlinux.o] Error 1
      
      In local_storage.c, we have
        BTF_ID_LIST_SINGLE(cgroup_storage_map_btf_ids, struct, bpf_local_storage_map)
      Commit c4bcfb38 ("bpf: Implement cgroup storage available to
      non-cgroup-attached bpf progs") added the above identical BTF_ID_LIST_SINGLE
      definition in bpf_cgrp_storage.c. With duplicated definitions, llvm linker
      complains with lto build.
      
      Also, extracting btf_id of 'struct bpf_local_storage_map' is defined four times
      for sk, inode, task and cgrp local storages. Let us define a single global one
      with a different name than cgroup_storage_map_btf_ids, which also fixed
      the lto compilation error.
      
      Fixes: c4bcfb38
      
       ("bpf: Implement cgroup storage available to non-cgroup-attached bpf progs")
      Signed-off-by: default avatarYonghong Song <yhs@fb.com>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Link: https://lore.kernel.org/bpf/20221130052147.1591625-1-yhs@fb.com
      3144bfa5
    • Pengcheng Yang's avatar
      selftests/bpf: Add ingress tests for txmsg with apply_bytes · 89903dcb
      Pengcheng Yang authored
      
      
      Currently, the ingress redirect is not covered in "txmsg test apply".
      
      Signed-off-by: default avatarPengcheng Yang <yangpc@wangsu.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/bpf/1669718441-2654-5-git-send-email-yangpc@wangsu.com
      89903dcb
    • Pengcheng Yang's avatar
      bpf, sockmap: Fix data loss caused by using apply_bytes on ingress redirect · 9072931f
      Pengcheng Yang authored
      Use apply_bytes on ingress redirect, when apply_bytes is less than
      the length of msg data, some data may be skipped and lost in
      bpf_tcp_ingress().
      
      If there is still data in the scatterlist that has not been consumed,
      we cannot move the msg iter.
      
      Fixes: 604326b4
      
       ("bpf, sockmap: convert to generic sk_msg interface")
      Signed-off-by: default avatarPengcheng Yang <yangpc@wangsu.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/bpf/1669718441-2654-4-git-send-email-yangpc@wangsu.com
      9072931f
    • Pengcheng Yang's avatar
      bpf, sockmap: Fix missing BPF_F_INGRESS flag when using apply_bytes · a351d608
      Pengcheng Yang authored
      When redirecting, we use sk_msg_to_ingress() to get the BPF_F_INGRESS
      flag from the msg->flags. If apply_bytes is used and it is larger than
      the current data being processed, sk_psock_msg_verdict() will not be
      called when sendmsg() is called again. At this time, the msg->flags is 0,
      and we lost the BPF_F_INGRESS flag.
      
      So we need to save the BPF_F_INGRESS flag in sk_psock and use it when
      redirection.
      
      Fixes: 8934ce2f
      
       ("bpf: sockmap redirect ingress support")
      Signed-off-by: default avatarPengcheng Yang <yangpc@wangsu.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/bpf/1669718441-2654-3-git-send-email-yangpc@wangsu.com
      a351d608
    • Pengcheng Yang's avatar
      bpf, sockmap: Fix repeated calls to sock_put() when msg has more_data · 7a9841ca
      Pengcheng Yang authored
      In tcp_bpf_send_verdict() redirection, the eval variable is assigned to
      __SK_REDIRECT after the apply_bytes data is sent, if msg has more_data,
      sock_put() will be called multiple times.
      
      We should reset the eval variable to __SK_NONE every time more_data
      starts.
      
      This causes:
      
      IPv4: Attempt to release TCP socket in state 1 00000000b4c925d7
      ------------[ cut here ]------------
      refcount_t: addition on 0; use-after-free.
      WARNING: CPU: 5 PID: 4482 at lib/refcount.c:25 refcount_warn_saturate+0x7d/0x110
      Modules linked in:
      CPU: 5 PID: 4482 Comm: sockhash_bypass Kdump: loaded Not tainted 6.0.0 #1
      Hardware name: Red Hat KVM, BIOS 1.11.0-2.el7 04/01/2014
      Call Trace:
       <TASK>
       __tcp_transmit_skb+0xa1b/0xb90
       ? __alloc_skb+0x8c/0x1a0
       ? __kmalloc_node_track_caller+0x184/0x320
       tcp_write_xmit+0x22a/0x1110
       __tcp_push_pending_frames+0x32/0xf0
       do_tcp_sendpages+0x62d/0x640
       tcp_bpf_push+0xae/0x2c0
       tcp_bpf_sendmsg_redir+0x260/0x410
       ? preempt_count_add+0x70/0xa0
       tcp_bpf_send_verdict+0x386/0x4b0
       tcp_bpf_sendmsg+0x21b/0x3b0
       sock_sendmsg+0x58/0x70
       __sys_sendto+0xfa/0x170
       ? xfd_validate_state+0x1d/0x80
       ? switch_fpu_return+0x59/0xe0
       __x64_sys_sendto+0x24/0x30
       do_syscall_64+0x37/0x90
       entry_SYSCALL_64_after_hwframe+0x63/0xcd
      
      Fixes: cd9733f5
      
       ("tcp_bpf: Fix one concurrency problem in the tcp_bpf_send_verdict function")
      Signed-off-by: default avatarPengcheng Yang <yangpc@wangsu.com>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarJakub Sitnicki <jakub@cloudflare.com>
      Link: https://lore.kernel.org/bpf/1669718441-2654-2-git-send-email-yangpc@wangsu.com
      7a9841ca
    • Alexei Starovoitov's avatar
      bpf: Tighten ptr_to_btf_id checks. · c67cae55
      Alexei Starovoitov authored
      The networking programs typically don't require CAP_PERFMON, but through kfuncs
      like bpf_cast_to_kern_ctx() they can access memory through PTR_TO_BTF_ID. In
      such case enforce CAP_PERFMON.
      Also make sure that only GPL programs can access kernel data structures.
      All kfuncs require GPL already.
      
      Also remove allow_ptr_to_map_access. It's the same as allow_ptr_leaks and
      different name for the same check only causes confusion.
      
      Fixes: fd264ca0 ("bpf: Add a kfunc to type cast from bpf uapi ctx to kernel ctx")
      Fixes: 50c6b8a9
      
       ("selftests/bpf: Add a test for btf_type_tag "percpu"")
      Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Acked-by: default avatarYonghong Song <yhs@fb.com>
      Link: https://lore.kernel.org/bpf/20221125220617.26846-1-alexei.starovoitov@gmail.com
      c67cae55
    • Daniel Borkmann's avatar
      selftests/bpf: Add bench test to arm64 and s390x denylist · 996c060e
      Daniel Borkmann authored
      BPF CI fails for arm64 and s390x each with the following result:
      
        [...]
        All error logs:
      
        serial_test_kprobe_multi_bench_attach:PASS:get_syms 0 nsec
        serial_test_kprobe_multi_bench_attach:PASS:kprobe_multi_empty__open_and_load 0 nsec
        libbpf: prog 'test_kprobe_empty': failed to attach: Operation not supported
        serial_test_kprobe_multi_bench_attach:FAIL:bpf_program__attach_kprobe_multi_opts unexpected error: -95
        #92      kprobe_multi_bench_attach:FAIL
        [...]
      
      Add the test to the deny list.
      
      Fixes: 5b6c7e5c
      
       ("selftests/bpf: Add attach bench test")
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      996c060e
    • Andrii Nakryiko's avatar
      selftests/bpf: Make sure enum-less bpf_enable_stats() API works in C++ mode · f8186bf6
      Andrii Nakryiko authored
      
      
      Just a simple test to make sure we don't introduce unwanted compiler
      warnings and API still supports passing enums as input argument.
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20221130200013.2997831-2-andrii@kernel.org
      f8186bf6
    • Andrii Nakryiko's avatar
      libbpf: Avoid enum forward-declarations in public API in C++ mode · b4269341
      Andrii Nakryiko authored
      
      
      C++ enum forward declarations are fundamentally not compatible with pure
      C enum definitions, and so libbpf's use of `enum bpf_stats_type;`
      forward declaration in libbpf/bpf.h public API header is causing C++
      compilation issues.
      
      More details can be found in [0], but it comes down to C++ supporting
      enum forward declaration only with explicitly specified backing type:
      
        enum bpf_stats_type: int;
      
      In C (and I believe it's a GCC extension also), such forward declaration
      is simply:
      
        enum bpf_stats_type;
      
      Further, in Linux UAPI this enum is defined in pure C way:
      
      enum bpf_stats_type { BPF_STATS_RUN_TIME = 0; }
      
      And even though in both cases backing type is int, which can be
      confirmed by looking at DWARF information, for C++ compiler actual enum
      definition and forward declaration are incompatible.
      
      To eliminate this problem, for C++ mode define input argument as int,
      which makes enum unnecessary in libbpf public header. This solves the
      issue and as demonstrated by next patch doesn't cause any unwanted
      compiler warnings, at least with default warnings setting.
      
        [0] https://stackoverflow.com/questions/42766839/c11-enum-forward-causes-underlying-type-mismatch
        [1] Closes: https://github.com/libbpf/libbpf/issues/249
      
      Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Link: https://lore.kernel.org/bpf/20221130200013.2997831-1-andrii@kernel.org
      b4269341
    • Martin KaFai Lau's avatar
      selftests/bpf: Avoid pinning prog when attaching to tc ingress in btf_skc_cls_ingress · 443f2164
      Martin KaFai Lau authored
      
      
      This patch removes the need to pin prog when attaching to tc ingress
      in the btf_skc_cls_ingress test.  Instead, directly use the
      bpf_tc_hook_create() and bpf_tc_attach().  The qdisc clsact
      will go away together with the netns, so no need to
      bpf_tc_hook_destroy().
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/bpf/20221129070900.3142427-8-martin.lau@linux.dev
      443f2164
    • Martin KaFai Lau's avatar
      selftests/bpf: Remove serial from tests using {open,close}_netns · 9b6a7773
      Martin KaFai Lau authored
      
      
      After removing the mount/umount dance from {open,close}_netns()
      in the pervious patch, "serial_" can be removed from
      the tests using {open,close}_netns().
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/bpf/20221129070900.3142427-7-martin.lau@linux.dev
      9b6a7773
    • Martin KaFai Lau's avatar
      selftests/bpf: Remove the "/sys" mount and umount dance in {open,close}_netns · 3084097c
      Martin KaFai Lau authored
      
      
      The previous patches have removed the need to do the mount and umount
      dance when switching netns. In particular:
      * Avoid remounting /sys/fs/bpf to have a clean start
      * Avoid remounting /sys to get a ifindex of a particular netns
      
      This patch can finally remove the mount and umount dance in
      {open,close}_netns which is unnecessarily complicated and
      error-prone.
      
      Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
      Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
      Acked-by: default avatarStanislav Fomichev <sdf@google.com>
      Link: https://lore.kernel.org/bpf/20221129070900.3142427-6-martin.lau@linux.dev
      3084097c