[PATCH bpf-next 0/6] Introduce bpf_ksock
From: Mahe Tardy <hidden>
Date: 2026-07-06 09:35:44
Also in:
bpf
This patch series introduces bpf_ksock, a set of BPF kfuncs to allow BPF programs to create UDP sockets and send data. This provides a mechanism for BPF LSM progs to emit telemetry over UDP independently of userspace. The main use case is to be able to completely dispense with agents/daemons for BPF programs after startup. In the case of Isovalent's Tetragon, the idea would be to be able to emit security alerts or export data from BPF even when the agent is down. For meta, according to Liam presentation[^2], this could replace logging via ringbuffers which created cross-binary versioning issues. The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration), for example used by the network bpf_crypto kfuncs. For reference, this was discussed at LSF/MM/BPF 2025[^1] in Montreal, again at Plumbers 2025 in Tokyo. Liam Wisehart mentioned this work during his presentation of BpfJailer[^2]. Then it was also discussed during LSF/MM/BPF 2026 in Zagreb. A first version of it, called bpf_netpoll was submitted to the mailing list but eventually NACKED by Jakub Kicinski[^3]. The discussion eventually reached an agreement that we should use regular kernel sockets if we want to do network from BPF programs[^4]. This was fundamentally more complex to implement but here is a first proposition of how it could look like after several automated reviews using sashiko. For more details, here are some of the main adjustements I had to make during the preparation of these patches: Initially, the goal was to register the ksock_kfunc_set with BPF_PROG_TYPE_UNSPEC to allow send to be called from any programs. This introduces significant challenges (but might be doable). The limitation is still that the programs should be able to sleep but combining this with bpf workqueue allows to send from virtually anywhere. However, not by-passing LSM socket hooks make it impossible to be called from the workqueue context as the credential of the initial caller would not be preserved. Also, it would be easy for users to shoot themselves in the foot and attach a program that sends asynchronously over the network on a network hook. So the idea for now is to restrict the ksock_kfunc_set (which is acquire, release and send) to SYSCALL and LSM to make it simpler. Also to make the patch set easier to start with, the sockets are restricted to UDP. v0 updates (from local sashiko iterations): - do not bypass the LSM and thus add send re-enter protection; - limit the number of socket creation through the kfunc per ns; - copy the arg values to avoid TOCTOU race since kfunc can sleep; - prevent calling bpf_ksock_create from workqueue with improper creds. [^1]: https://lwn.net/Articles/1022034/ [^2]: https://lpc.events/event/19/contributions/2159/ [^3]: https://lore.kernel.org/bpf/20260511182019.69ebc7c6@kernel.org/ (local) [^4]: https://lore.kernel.org/bpf/CAPhsuW71P58XqsXrLbqsShgnozg66TA=T_c=fYrqSSzvL1tTWA@mail.gmail.com/ (local) Mahe Tardy (6): net: Add __sys_connect_socket() helper bpf: Add ksock kfuncs selftests/bpf: Add ksock kfunc test selftests/bpf: Add ksock LSM recursion test selftests/bpf: Add ksock net ns quota tests selftests/bpf: Add ksock test for async callback guard Documentation/admin-guide/sysctl/net.rst | 12 + include/linux/bpf_ksock.h | 50 ++ include/linux/socket.h | 2 + kernel/bpf/verifier.c | 3 + net/core/Makefile | 3 + net/core/bpf_ksock.c | 516 ++++++++++++++++++ net/core/sysctl_net_core.c | 11 + net/socket.c | 32 +- .../testing/selftests/bpf/prog_tests/ksock.c | 239 ++++++++ .../selftests/bpf/prog_tests/ksock_quota.c | 139 +++++ .../selftests/bpf/prog_tests/ksock_wq.c | 34 ++ .../testing/selftests/bpf/progs/ksock_basic.c | 37 ++ .../selftests/bpf/progs/ksock_common.h | 110 ++++ .../testing/selftests/bpf/progs/ksock_quota.c | 167 ++++++ .../selftests/bpf/progs/ksock_recursion.c | 69 +++ tools/testing/selftests/bpf/progs/ksock_wq.c | 62 +++ 16 files changed, 1472 insertions(+), 14 deletions(-) create mode 100644 include/linux/bpf_ksock.h create mode 100644 net/core/bpf_ksock.c create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock.c create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock_quota.c create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock_wq.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_basic.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_common.h create mode 100644 tools/testing/selftests/bpf/progs/ksock_quota.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_recursion.c create mode 100644 tools/testing/selftests/bpf/progs/ksock_wq.c -- 2.34.1