Skip to content

Introduce bpf_ksock

Mahe Tardy bpf netdev 7 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v7 Latest5 patches 2 repliesIn review 2 of 5 applied
  2. v65 patches 7 repliesSuperseded
  3. v55 patches 13 repliesSuperseded
Earlier versions · 4
VersionPostedPatchesRepliesStatus
v45 patches7 repliesSuperseded
v35 patches6 repliesSuperseded
v25 patches2 repliesSuperseded
v16 patches14 repliesSuperseded

Patches in v7 2 of 5 applied

#Subject and filesSizeRepliesApplied
1/5net: Add connect_socket() helperinclude/linux/socket.h net/socket.c+20 -140 replies
2/5bpf: Add ksock kfuncsinclude/linux/bpf_ksock.h kernel/bpf/verifier.c net/core/Makefile +1 more+370 -00 replies
3/5selftests/bpf: Add ksock kfunc testtools/testing/selftests/bpf/prog_tests/ksock.c tools/testing/selftests/bpf/progs/ksock_common.h tools/testing/selftests/bpf/progs/ksock_lsm.c+274 -01 replyApplied
4/5selftests/bpf: Test forbidden bpf_ksock_send() LSM attachtools/testing/selftests/bpf/prog_tests/ksock.c tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c+41 -00 replies
5/5selftests/bpf: Add ksock test for async callback guardtools/testing/selftests/bpf/prog_tests/ksock_wq.c tools/testing/selftests/bpf/progs/ksock_wq.c+107 -01 replyApplied

Reviews trailers per patch

All 5 patches have a Reviewed-by or Acked-by.

PatchSong LiuStanislav FomichevJiayuan ChenKuniyuki Iwashima
1/5A-b A-b R-b R-b
2/5A-b A-b R-b
3/5A-b R-b
4/5A-b A-b
5/5A-b A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 4 reviewers.

Read the v7 thread

Cover letter [PATCH bpf-next v7 0/5] Introduce bpf_ksock

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.

v7 updates:
- remove early size check in bpf_ksock_send() (bot+bpf-ci)
- cleanup some CONFIG_INET and CONFIG_IPV6 guards (bot+bpf-ci)
- clarify some commits changelog (bot+bpf-ci)
- reuse network_helpers.h functions in tests (bot+bpf-ci)
- add bounded iterations in test_ksock_wq() (bot+bpf-ci)
- add Song's acked-by.

v6 updates:
- add Stanislav, Jiayuan and Kuniyuki acked-by and reviewed-by tags.
- reformat some weird indents and nits (Kuniyuki)
- remove unused import in ksock_lsm_verifier.c (bot+bpf-ci)

v5 updates:
- add Song Liu ack on first patch (Song)
- guard BTF ID of bpf_lsm_socket_sendmsg on CONFIG_BPF_LSM (Jiayuan)
- fix checkpatch warnings on style (Jiayuan)
- encapsulate RCU dance in new ksock_ctx_get() selftest helper (Jiayuan)

v4 updates:
- drop the __sys_ prefix on connect_socket() (Song)
- replace recursion protection with a verifier filter on calling bpf_ksock_send() on a program attached to the security_socket_sendmsg() LSM hook (Song)

v3 updates:
Simplifies bpf_ksock_connect arg by using a union (union bpf_ksock_addr) containing struct sockaddr_in and struct sockaddr_in6 to remove most of bpf_ksock_parse_addr() code (Stanislav).

Note that I initially tried to put the union inside of struct bpf_ksock_addr_opts but we ended up with too much struct nesting depth from bpf, for example when trying to write the ipv4 address:

	addr_opts.addr.sin.sin_addr.s_addr = ipv4_remote;

It resulted in:

max struct nesting depth exceeded
R2 pointer type STRUCT bpf_ksock_addr_opts must point to void, scalar, or struct with scalar

v2 updates:
This second version simplifies the patch set by removing bind, keeping connect + send for now (could be replaced per sendto if needed). It also removes the sysctl limit and the whole hashtable ksock_send guard with a single bit in task_struct.

- remove bind from the API (Stanislav, Amery, Kuniyuki)
- remove the bpf_ksock_max sysctl limit (Stanislav, Kuniyuki)
- replace ksock_send_guard with a bit in task_struct (Kuniyuki)
- remove unnecessary init struct sockaddr_storage addr = {}; (Kuniyuki)
- remove redundant ASSERT_OK_FD (sashiko)
- fix typos in ksock test patch commit log (bot+bpf-ci)
- fix return statement in kfunc registration (bot+bpf-ci)

v1 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/[email protected]/
[^4]: https://lore.kernel.org/bpf/CAPhsuW71P58XqsXrLbqsShgnozg66TA=T_c=fYrqSSzvL1tTWA@mail.gmail.com/

Link to v6: https://lore.kernel.org/bpf/[email protected]/

Mahe Tardy (5):
net: Add connect_socket() helper
bpf: Add ksock kfuncs
selftests/bpf: Add ksock kfunc test selftests/bpf: Test forbidden bpf_ksock_send() LSM attach selftests/bpf: Add ksock test for async callback guard

 include/linux/bpf_ksock.h                     |  36 ++
 include/linux/socket.h                        |   2 +
 kernel/bpf/verifier.c                         |   3 +
 net/core/Makefile                             |   3 +
 net/core/bpf_ksock.c                          | 328 ++++++++++++++++++
 net/socket.c                                  |  32 +-
 .../testing/selftests/bpf/prog_tests/ksock.c  | 130 +++++++
 .../selftests/bpf/prog_tests/ksock_wq.c       |  45 +++
 .../selftests/bpf/progs/ksock_common.h        |  78 +++++
 tools/testing/selftests/bpf/progs/ksock_lsm.c |  72 ++++
 .../selftests/bpf/progs/ksock_lsm_verifier.c  |  35 ++
 tools/testing/selftests/bpf/progs/ksock_wq.c  |  62 ++++
 12 files changed, 812 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_wq.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_common.h
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_lsm.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_lsm_verifier.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_wq.c

--
2.34.1