Skip to contentSkip to conversation

Introduce bpf_ksock

PATCH Superseded Mahe Tardy 4 participants 13 messages Aug 4 – Aug 6

Subsystems (8)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Mahe Tardy

author
#0 lore raw

v3 cover 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.

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 v2: https://lore.kernel.org/bpf/[email protected]/

Mahe Tardy (5):
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 test for async callback guard

 include/linux/bpf_ksock.h                     |  36 ++
 include/linux/sched.h                         |   4 +
 include/linux/socket.h                        |   2 +
 kernel/bpf/verifier.c                         |   3 +
 net/core/Makefile                             |   3 +
 net/core/bpf_ksock.c                          | 329 ++++++++++++++++++
 net/socket.c                                  |  32 +-
 .../testing/selftests/bpf/prog_tests/ksock.c  | 231 ++++++++++++
 .../selftests/bpf/prog_tests/ksock_wq.c       |  34 ++
 .../testing/selftests/bpf/progs/ksock_basic.c |  37 ++
 .../selftests/bpf/progs/ksock_common.h        |  99 ++++++
 .../selftests/bpf/progs/ksock_recursion.c     |  69 ++++
 tools/testing/selftests/bpf/progs/ksock_wq.c  |  62 ++++
 13 files changed, 927 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_basic.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_common.h
 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

author
#1 lore raw

PATCH v3 1/5 net: Add __sys_connect_socket() helper

Add a helper that connects an existing socket while invoking the LSM hook. Reuse it in __sys_connect_file() to avoid duplicating the connect logic. Other socket operations have equivalent helpers that trigger the appropriate LSM hooks that can be reused, this one was the only one missing.

---
 include/linux/socket.h |  2 ++
 net/socket.c           | 32 ++++++++++++++++++--------------
 2 files changed, 20 insertions(+), 14 deletions(-)
include/linux/socket.h +2 -0
index 2a8d7b14f1d1..48f1eb8193de 100644
@@ -461,6 +461,8 @@ extern struct file *__sys_socket_file(int family, int type, int protocol);
 extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
 extern int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
 			     int addrlen);
+extern int __sys_connect_socket(struct socket *sock, struct sockaddr_storage *addr,
+				int addrlen, int flags);
 extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
 			      int addrlen, int file_flags);
 extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
net/socket.c +18 -14
index 63c69a0fa74e..c7427dd4dd52 100644
@@ -2115,27 +2115,31 @@ SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  *	include the -EINPROGRESS status for such sockets.
  */
 
+int __sys_connect_socket(struct socket *sock, struct sockaddr_storage *address,
+			 int addrlen, int flags)
+{
+	int err;
+
+	err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
+	if (err)
+		return err;
+
+	return READ_ONCE(sock->ops)->connect(sock,
+				    (struct sockaddr_unsized *)address,
+				    addrlen, flags);
+}
+
 int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
 		       int addrlen, int file_flags)
 {
 	struct socket *sock;
-	int err;
 
 	sock = sock_from_file(file);
-	if (!sock) {
-		err = -ENOTSOCK;
-		goto out;
-	}
-
-	err =
-	    security_socket_connect(sock, (struct sockaddr *)address, addrlen);
-	if (err)
-		goto out;
+	if (!sock)
+		return -ENOTSOCK;
 
-	err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address,
-					    addrlen, sock->file->f_flags | file_flags);
-out:
-	return err;
+	return __sys_connect_socket(sock, address, addrlen,
+				    sock->file->f_flags | file_flags);
 }
 
 int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)

--
2.34.1

#2 lore raw

Re: [PATCH bpf-next v3 1/5] net: Add __sys_connect_socket() helper

This isn't a bug, but now that __sys_connect_socket() sits directly under this comment block, would it read better to keep the comment attached to the function that actually copies the address in…

net/socket.c SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, · line 2120

Quoted 10 lines
net/socket.c +3 -0
@@ -2115,27 +2115,31 @@ SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
 *	include the -EINPROGRESS status for such sockets.
 */
 
+int __sys_connect_socket(struct socket *sock, struct sockaddr_storage *address,
+			 int addrlen, int flags)
+{

This isn't a bug, but now that __sys_connect_socket() sits directly under this comment block, would it read better to keep the comment attached to the function that actually copies the address in from user space? The first paragraph mentions "The address is in user space so we verify it is OK and move it to kernel space," but __sys_connect_socket() takes an already-copied struct sockaddr_storage pointer. That user-copy step now lives in __sys_connect() via move_addr_to_kernel().

Quoted 27 lines
net/socket.c +13 -7
+	int err;
+
+	err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
+	if (err)
+		return err;
+
+	return READ_ONCE(sock->ops)->connect(sock,
+				    (struct sockaddr_unsized *)address,
+				    addrlen, flags);
+}
+
 int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
 		       int addrlen, int file_flags)
 {
 	struct socket *sock;
-	int err;
 
 	sock = sock_from_file(file);
-	if (!sock) {
-		err = -ENOTSOCK;
-		goto out;
-	}
+	if (!sock)
+		return -ENOTSOCK;
 
-	err =
-	    security_socket_connect(sock, (struct sockaddr *)address, addrlen);

net/socket.c SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr, · line 2143

Quoted 10 lines
net/socket.c +2 -7
-	if (err)
-		goto out;
-
-	err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr_unsized *)address,
-					    addrlen, sock->file->f_flags | file_flags);
-out:
-	return err;
+	return __sys_connect_socket(sock, address, addrlen,
+				    sock->file->f_flags | file_flags);
 }

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/30931766736

reviewer
#3 lore raw

Re: [PATCH bpf-next v3 1/5] net: Add __sys_connect_socket() helper

In reply to Mahe Tardy
Quoted 18 lines · Mahe Tardy

Add a helper that connects an existing socket while invoking the LSM hook. Reuse it in __sys_connect_file() to avoid duplicating the connect logic. Other socket operations have equivalent helpers that trigger the appropriate LSM hooks that can be reused, this one was the only one missing.

---
 include/linux/socket.h |  2 ++
 net/socket.c           | 32 ++++++++++++++++++--------------
 2 files changed, 20 insertions(+), 14 deletions(-)
include/linux/socket.h +0 -0
index 2a8d7b14f1d1..48f1eb8193de 100644

include/linux/socket.h extern struct file *__sys_socket_file(int family, int type, int protocol); · line 465

Quoted 5 lines
include/linux/socket.h +2 -0
@@ -461,6 +461,8 @@ extern struct file *__sys_socket_file(int family, int type, int protocol);
 extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
 extern int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
                             int addrlen);
+extern int __sys_connect_socket(struct socket *sock, struct sockaddr_storage *addr,
+                               int addrlen, int flags);

IIUC, __sys_ prefix is for functions behind syscalls. Do we really want this prefix here?

Thanks,
Song

Quoted 7 lines
include/linux/socket.h +0 -0
 extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
                              int addrlen, int file_flags);
 extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
net/socket.c +0 -0
index 63c69a0fa74e..c7427dd4dd52 100644
author
#4 lore raw

Re: [PATCH bpf-next v3 1/5] net: Add __sys_connect_socket() helper

Quoted 14 lines · Song Liu
Quoted 13 lines · Mahe Tardy

Add a helper that connects an existing socket while invoking the LSM hook. Reuse it in __sys_connect_file() to avoid duplicating the connect logic. Other socket operations have equivalent helpers that trigger the appropriate LSM hooks that can be reused, this one was the only one missing.

---
 include/linux/socket.h |  2 ++
 net/socket.c           | 32 ++++++++++++++++++--------------
 2 files changed, 20 insertions(+), 14 deletions(-)
Quoted 13 lines

include/linux/socket.h extern struct file *__sys_socket_file(int family, int type, int protocol); · line 465

Quoted 10 lines
include/linux/socket.h +2 -0
index 2a8d7b14f1d1..48f1eb8193de 100644
@@ -461,6 +461,8 @@ extern struct file *__sys_socket_file(int family, int type, int protocol);
 extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
 extern int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
                             int addrlen);
+extern int __sys_connect_socket(struct socket *sock, struct sockaddr_storage *addr,
+                               int addrlen, int flags);

IIUC, __sys_ prefix is for functions behind syscalls. Do we really want this prefix here?

Yeah let's remove it.

Quoted 11 lines

Thanks,
Song

Quoted 7 lines
include/linux/socket.h +0 -0
 extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
                              int addrlen, int file_flags);
 extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
net/socket.c +0 -0
index 63c69a0fa74e..c7427dd4dd52 100644
author
#5 lore raw

PATCH v3 2/5 bpf: Add ksock kfuncs

In reply to Mahe Tardy

Add BPF kfuncs that allow BPF LSM programs to create and use sockets for sending data. This provides a mechanism for BPF programs to emit telemetry. For this first patch set, it's restricted to SOCK_DGRAM socket types with IPPROTO_UDP protocol but could be easily extended to SOCK_STREAM and IPPROTO_TCP in the future.

The API consists of five kfuncs:

  bpf_ksock_create()   - Create a socket (sleepable)
  bpf_ksock_connect()  - Connect socket to remote address (sleepable)
  bpf_ksock_send()     - Send data through the socket (sleepable)
  bpf_ksock_acquire()  - Acquire a reference to a socket context
  bpf_ksock_release()  - Release a reference (cleanup via

queue_rcu_work since sock_release sleeps)

The setup kfuncs bpf_ksock_create, bpf_ksock_connect, can be called from SYSCALL programs only. While bpf_ksock_acquire, bpf_ksock_release and bpf_ksock_send can be called from SYSCALL and LSM programs.

The implementation follows the established kfunc lifecycle pattern (create/acquire/release with refcounting, kptr map storage, dtor registration). The kernel socket is wrapped in a refcounted bpf_ksock struct. Cleanup is deferred via queue_rcu_work() because sock_release() may sleep.

The kfuncs are only compiled when CONFIG_INET is enabled, as they specifically support AF_INET and AF_INET6 sockets.

The socket operations go through the expected LSM hooks instead of by-passing them like many kernel sockets since those are created by BPF programs and thus system users. Thus bpf_ksock_send() kfunc, which is exposed to LSM progs, has a re-entering protection to avoid recursion. Also, because of the LSM checks, we prevent the use of the kfuncs from asynchronous workqueue as the current value would then be invalid.

In bpf_ksock_create(), we copy the arg values to avoid TOCTOU races since the kfunc can sleep and the arg values could be stored in a map that could be re-written by BPF progs or even userspace programs if the map is mmaped.

---
 include/linux/bpf_ksock.h |  36 +++++
 include/linux/sched.h     |   4 +
 kernel/bpf/verifier.c     |   3 +
 net/core/Makefile         |   3 +
 net/core/bpf_ksock.c      | 329 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 375 insertions(+)
 create mode 100644 include/linux/bpf_ksock.h
 create mode 100644 net/core/bpf_ksock.c
include/linux/bpf_ksock.h new file +36 -0
new file mode 100644
index 000000000000..cb387fb75e43
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/* Copyright (c) 2026 Isovalent */
+
+#ifndef _BPF_KSOCK_H
+#define _BPF_KSOCK_H
+
+#include <linux/types.h>
+#include <linux/in.h>
+#include <linux/in6.h>
+
+/**
+ * struct bpf_ksock_create_opts - BPF kernel socket creation parameters
+ * @family:	Address family: AF_INET or AF_INET6.
+ * @type:	Socket type: only SOCK_DGRAM supported for now.
+ * @protocol:	Protocol number (e.g. IPPROTO_UDP), or 0 for the default protocol
+ *		of the given type.
+ * @reserved:	Must be zero. Reserved for future use.
+ */
+struct bpf_ksock_create_opts {
+	__u8 family;
+	__u8 type;
+	__u8 protocol;
+	__u8 reserved;
+};
+
+/**
+ * union bpf_ksock_addr - IPv4 or IPv6 socket address
+ * @sin: IPv4 socket address.
+ * @sin6: IPv6 socket address.
+ */
+union bpf_ksock_addr {
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+};
+
+#endif /* _BPF_KSOCK_H */
include/linux/sched.h +4 -0
index 373bcc0598d1..334846df9238 100644
@@ -1050,6 +1050,10 @@ struct task_struct {
 	/* Recursion prevention for eventfd_signal() */
 	unsigned			in_eventfd:1;
 #endif
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_INET)
+	/* Recursion prevention for bpf_ksock_send() */
+	unsigned			in_bpf_ksock_send:1;
+#endif
 #ifdef CONFIG_ARCH_HAS_CPU_PASID
 	unsigned			pasid_activated:1;
 #endif
kernel/bpf/verifier.c +3 -0
index 52be0a118cce..4f3de0a4b0c1 100644
@@ -4445,6 +4445,9 @@ BTF_ID(struct, task_struct)
 #ifdef CONFIG_CRYPTO
 BTF_ID(struct, bpf_crypto_ctx)
 #endif
+#ifdef CONFIG_INET
+BTF_ID(struct, bpf_ksock)
+#endif
 BTF_SET_END(rcu_protected_types)
 
 static bool rcu_protected_object(const struct btf *btf, u32 btf_id)
net/core/Makefile +3 -0
index b3fdcb4e355f..a9295b785901 100644
@@ -44,6 +44,9 @@ obj-$(CONFIG_FAILOVER) += failover.o
 obj-$(CONFIG_NET_SOCK_MSG) += skmsg.o
 obj-$(CONFIG_BPF_SYSCALL) += sock_map.o
 obj-$(CONFIG_BPF_SYSCALL) += bpf_sk_storage.o
+ifneq ($(CONFIG_INET),)
+obj-$(CONFIG_BPF_SYSCALL) += bpf_ksock.o
+endif
 obj-$(CONFIG_OF)	+= of_net.o
 obj-$(CONFIG_NET_TEST) += net_test.o
 obj-$(CONFIG_NET_DEVMEM) += devmem.o
net/core/bpf_ksock.c new file +329 -0

329 lines not on this page · raw message

--
2.34.1

reviewer
#6 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs

On Tue, Aug 4, 2026 at 9:47 AM Mahe Tardy <[email protected]> wrote:
[...]

Quoted 11 lines
net/core/bpf_ksock.c new file +11 -0
+__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
+                              u32 data__sz)
+{
+       struct msghdr msg = {
+               .msg_flags = MSG_DONTWAIT,
+       };
+       struct kvec iov = {
+               .iov_base = (void *)data,
+               .iov_len = data__sz,
+       };
+       int ret;

net/core/bpf_ksock.c line 261

Quoted 10 lines
net/core/bpf_ksock.c new file +10 -0
+
+       if (!bpf_ksock_has_user_task_context())
+               return -EOPNOTSUPP;
+
+       /* Early check for UDP. Exact limits enforced by kernel_sendmsg(). */
+       if (data__sz > IP_MAX_MTU)
+               return -EMSGSIZE;
+
+       if (current->in_bpf_ksock_send)
+               return -EBUSY;

Returning EBUSY is not ideal. Can we avoid recursion by disallowing calling bpf_ksock_send from certain hooks? You can find examples check_kfunc_call() or check_special_kfunc(). This probably means we need to grow special_kfunc_list, which is not ideal.

@Kumar, do you have strong objection that we grow special_kfunc_list for this use case?

Thanks,
Song

Quoted 9 lines
net/core/bpf_ksock.c new file +9 -0
+
+       current->in_bpf_ksock_send = true;
+       ret = kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
+       current->in_bpf_ksock_send = false;
+
+       return ret;
+}
+
+__bpf_kfunc_end_defs();
author
#7 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs

Quoted 20 lines · Song Liu

On Tue, Aug 4, 2026 at 9:47 AM Mahe Tardy <[email protected]> wrote:
[...]

Quoted 18 lines
net/core/bpf_ksock.c new file +18 -0
+__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
+                              u32 data__sz)
+{
+       struct msghdr msg = {
+               .msg_flags = MSG_DONTWAIT,
+       };
+       struct kvec iov = {
+               .iov_base = (void *)data,
+               .iov_len = data__sz,
+       };
+       int ret;
+
+       if (!bpf_ksock_has_user_task_context())
+               return -EOPNOTSUPP;
+
+       /* Early check for UDP. Exact limits enforced by kernel_sendmsg(). */
+       if (data__sz > IP_MAX_MTU)
+               return -EMSGSIZE;
Quoted 8 lines

net/core/bpf_ksock.c line 261

Quoted 3 lines
net/core/bpf_ksock.c new file +3 -0
+
+       if (current->in_bpf_ksock_send)
+               return -EBUSY;

Returning EBUSY is not ideal. Can we avoid recursion by disallowing calling bpf_ksock_send from certain hooks? You can find examples check_kfunc_call() or check_special_kfunc(). This probably means we need to grow special_kfunc_list, which is not ideal.

This is actually a really nice feedback. I didn't know we could add a filter per attach BTF ID. This resolves the issue that the send kfunc calls security_socket_sendmsg so we can remove all the recursion protection and struggles, it's rejected at verifier time.

The filter now looks like this, I'd send this in the next version:

BTF_ID_LIST_SINGLE(bpf_lsm_socket_sendmsg_id, func, bpf_lsm_socket_sendmsg)

static int bpf_ksock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)

{
	if (!btf_id_set8_contains(&ksock_kfunc_btf_ids, kfunc_id))
		return 0;

	if (prog->type == BPF_PROG_TYPE_SYSCALL)
		return 0;

	if (prog->type == BPF_PROG_TYPE_LSM &&
	    prog->aux->attach_btf_id != bpf_lsm_socket_sendmsg_id[0])
		return 0;

	return -EACCES;
}

I don't think I need to touch anything else. I'll adjust the tests according to this. Thanks Song.

Quoted 17 lines

@Kumar, do you have strong objection that we grow special_kfunc_list for this use case?

Thanks,
Song

Quoted 9 lines
net/core/bpf_ksock.c new file +9 -0
+
+       current->in_bpf_ksock_send = true;
+       ret = kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
+       current->in_bpf_ksock_send = false;
+
+       return ret;
+}
+
+__bpf_kfunc_end_defs();
#8 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs

Quoted 54 lines · Mahe Tardy
Quoted 20 lines · Song Liu

On Tue, Aug 4, 2026 at 9:47 AM Mahe Tardy <[email protected]> wrote:
[...]

Quoted 18 lines
net/core/bpf_ksock.c new file +18 -0
+__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
+                              u32 data__sz)
+{
+       struct msghdr msg = {
+               .msg_flags = MSG_DONTWAIT,
+       };
+       struct kvec iov = {
+               .iov_base = (void *)data,
+               .iov_len = data__sz,
+       };
+       int ret;
+
+       if (!bpf_ksock_has_user_task_context())
+               return -EOPNOTSUPP;
+
+       /* Early check for UDP. Exact limits enforced by kernel_sendmsg(). */
+       if (data__sz > IP_MAX_MTU)
+               return -EMSGSIZE;
Quoted 8 lines

net/core/bpf_ksock.c line 261

Quoted 3 lines
net/core/bpf_ksock.c new file +3 -0
+
+       if (current->in_bpf_ksock_send)
+               return -EBUSY;

Returning EBUSY is not ideal. Can we avoid recursion by disallowing calling bpf_ksock_send from certain hooks? You can find examples check_kfunc_call() or check_special_kfunc(). This probably means we need to grow special_kfunc_list, which is not ideal.

This is actually a really nice feedback. I didn't know we could add a filter per attach BTF ID. This resolves the issue that the send kfunc calls security_socket_sendmsg so we can remove all the recursion protection and struggles, it's rejected at verifier time.

The filter now looks like this, I'd send this in the next version:

BTF_ID_LIST_SINGLE(bpf_lsm_socket_sendmsg_id, func, bpf_lsm_socket_sendmsg)

static int bpf_ksock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)

{
	if (!btf_id_set8_contains(&ksock_kfunc_btf_ids, kfunc_id))
		return 0;

	if (prog->type == BPF_PROG_TYPE_SYSCALL)
		return 0;

	if (prog->type == BPF_PROG_TYPE_LSM &&
	    prog->aux->attach_btf_id != bpf_lsm_socket_sendmsg_id[0])
		return 0;

	return -EACCES;
}
Quoted 3 lines

I don't think I need to touch anything else. I'll adjust the tests according to this. Thanks Song.

Yep, using a kfunc filter should be enough.

Quoted 17 lines
Quoted 17 lines

@Kumar, do you have strong objection that we grow special_kfunc_list for this use case?

Thanks,
Song

Quoted 9 lines
net/core/bpf_ksock.c new file +9 -0
+
+       current->in_bpf_ksock_send = true;
+       ret = kernel_sendmsg(ks->sock, &msg, &iov, 1, data__sz);
+       current->in_bpf_ksock_send = false;
+
+       return ret;
+}
+
+__bpf_kfunc_end_defs();
reviewer
#9 lore raw

Re: [PATCH bpf-next v3 2/5] bpf: Add ksock kfuncs

In reply to Mahe Tardy
Quoted 55 lines · Mahe Tardy
Quoted 20 lines · Song Liu

On Tue, Aug 4, 2026 at 9:47 AM Mahe Tardy <[email protected]> wrote:
[...]

Quoted 18 lines
net/core/bpf_ksock.c new file +18 -0
+__bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data,
+                              u32 data__sz)
+{
+       struct msghdr msg = {
+               .msg_flags = MSG_DONTWAIT,
+       };
+       struct kvec iov = {
+               .iov_base = (void *)data,
+               .iov_len = data__sz,
+       };
+       int ret;
+
+       if (!bpf_ksock_has_user_task_context())
+               return -EOPNOTSUPP;
+
+       /* Early check for UDP. Exact limits enforced by kernel_sendmsg(). */
+       if (data__sz > IP_MAX_MTU)
+               return -EMSGSIZE;
Quoted 8 lines

net/core/bpf_ksock.c line 261

Quoted 3 lines
net/core/bpf_ksock.c new file +3 -0
+
+       if (current->in_bpf_ksock_send)
+               return -EBUSY;

Returning EBUSY is not ideal. Can we avoid recursion by disallowing calling bpf_ksock_send from certain hooks? You can find examples check_kfunc_call() or check_special_kfunc(). This probably means we need to grow special_kfunc_list, which is not ideal.

This is actually a really nice feedback. I didn't know we could add a filter per attach BTF ID. This resolves the issue that the send kfunc calls security_socket_sendmsg so we can remove all the recursion protection and struggles, it's rejected at verifier time.

The filter now looks like this, I'd send this in the next version:

BTF_ID_LIST_SINGLE(bpf_lsm_socket_sendmsg_id, func, bpf_lsm_socket_sendmsg)

static int bpf_ksock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)

{
        if (!btf_id_set8_contains(&ksock_kfunc_btf_ids, kfunc_id))
                return 0;

        if (prog->type == BPF_PROG_TYPE_SYSCALL)
                return 0;

        if (prog->type == BPF_PROG_TYPE_LSM &&
            prog->aux->attach_btf_id != bpf_lsm_socket_sendmsg_id[0])
                return 0;

        return -EACCES;
}
Quoted 2 lines

I don't think I need to touch anything else. I'll adjust the tests according to this. Thanks Song.

Ah, right, filter is enough for this case.

Thanks for digging deep into this.
Song

author
#10 lore raw

PATCH v3 3/5 selftests/bpf: Add ksock kfunc test

In reply to Mahe Tardy

Add a selftest that exercises the ksock kfuncs end-to-end. One syscall bpf setup program creates a ksock context and connects the socket. Another syscall bpf program looks up the context and send test data. The userspace harness creates a network namespace and a new socket on loopback, run the setup and send syscall bpf progs then check that the userspace socket received the data from bpf.

---
 .../testing/selftests/bpf/prog_tests/ksock.c  | 159 ++++++++++++++++++
 .../testing/selftests/bpf/progs/ksock_basic.c |  68 ++++++++
 .../selftests/bpf/progs/ksock_common.h        |  61 +++++++
 3 files changed, 288 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_basic.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_common.h
tools/testing/selftests/bpf/prog_tests/ksock.c new file +159 -0
new file mode 100644
index 000000000000..fb88eac9eeba
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Isovalent */
+
+#include <arpa/inet.h>
+
+#include "test_progs.h"
+#include "network_helpers.h"
+#include "ksock_basic.skel.h"
+
+#define NS_TEST "ksock_basic_ns"
+#define LOOPBACK_IP "127.0.0.1"
+#define RECV_PORT 7777
+#define RECV_TIMEOUT_SEC 5
+
+struct ksock_test_env {
+	const char *netns;
+	bool netns_created;
+	struct nstoken *nstoken;
+	struct sockaddr_in addr;
+	int rfd;
+	char buf[32];
+};
+
+static bool ksock_test_env_setup(struct ksock_test_env *env, const char *netns)
+{
+	struct timeval tv;
+	int err;
+
+	memset(env, 0, sizeof(*env));
+	env->netns = netns;
+	env->rfd = -1;
+
+	SYS(fail, "ip netns add %s", netns);
+	env->netns_created = true;
+	SYS(fail, "ip -net %s link set lo up", netns);
+
+	env->nstoken = open_netns(netns);
+	if (!ASSERT_OK_PTR(env->nstoken, "open_netns"))
+		goto fail;
+
+	env->rfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	if (!ASSERT_OK_FD(env->rfd, "receiver socket"))
+		goto fail;
+
+	env->addr.sin_family = AF_INET;
+	env->addr.sin_addr.s_addr = inet_addr(LOOPBACK_IP);
+	env->addr.sin_port = htons(RECV_PORT);
+
+	err = bind(env->rfd, (struct sockaddr *)&env->addr, sizeof(env->addr));
+	if (!ASSERT_OK(err, "bind receiver"))
+		goto fail;
+
+	tv.tv_sec = RECV_TIMEOUT_SEC;
+	tv.tv_usec = 0;
+	err = setsockopt(env->rfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+	if (!ASSERT_OK(err, "set rcvtimeo"))
+		goto fail;
+
+	return true;
+
+fail:
+	if (env->rfd >= 0) {
+		close(env->rfd);
+		env->rfd = -1;
+	}
+	if (env->nstoken) {
+		close_netns(env->nstoken);
+		env->nstoken = NULL;
+	}
+	if (env->netns_created) {
+		SYS_NOFAIL("ip netns del %s >/dev/null 2>&1", netns);
+		env->netns_created = false;
+	}
+	return false;
+}
+
+static void ksock_test_env_cleanup(struct ksock_test_env *env)
+{
+	if (env->rfd >= 0)
+		close(env->rfd);
+	if (env->nstoken)
+		close_netns(env->nstoken);
+	if (env->netns_created) {
+		SYS_NOFAIL("ip netns del %s >/dev/null 2>&1", env->netns);
+		env->netns_created = false;
+	}
+}
+
+static void ksock_assert_recv(struct ksock_test_env *env, const char *data,
+			      size_t data_sz)
+{
+	ssize_t n;
+
+	memset(env->buf, 0, sizeof(env->buf));
+	n = recvfrom(env->rfd, env->buf, sizeof(env->buf), 0, NULL, NULL);
+	if (!ASSERT_EQ(n, data_sz, "recvfrom len"))
+		return;
+	ASSERT_MEMEQ(env->buf, data, data_sz, "payload match");
+}
+
+static bool ksock_setup_ctx(struct ksock_basic *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	int err, pfd;
+
+	skel->bss->ipv4_remote = inet_addr(LOOPBACK_IP);
+	skel->bss->remote_port = RECV_PORT;
+
+	pfd = bpf_program__fd(skel->progs.ksock_setup);
+	err = bpf_prog_test_run_opts(pfd, &opts);
+	if (!ASSERT_OK(err, "ksock_setup run"))
+		return false;
+	if (!ASSERT_OK(opts.retval, "ksock_setup retval"))
+		return false;
+
+	return true;
+}
+
+void test_ksock_basic(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct ksock_test_env env;
+	struct ksock_basic *skel;
+	int err, pfd;
+
+	skel = ksock_basic__open();
+	if (!ASSERT_OK_PTR(skel, "skel open"))
+		return;
+
+	err = ksock_basic__load(skel);
+	if (!ASSERT_OK(err, "skel load")) {
+		ksock_basic__destroy(skel);
+		return;
+	}
+
+	if (!ksock_test_env_setup(&env, NS_TEST))
+		goto fail;
+
+	/* Step 1: Run the setup SYSCALL prog to create ksock */
+	if (!ksock_setup_ctx(skel))
+		goto fail;
+
+	/* Step 2: Run the send SYSCALL prog */
+	pfd = bpf_program__fd(skel->progs.ksock_send);
+	err = bpf_prog_test_run_opts(pfd, &opts);
+	if (!ASSERT_OK(err, "ksock_send run"))
+		goto fail;
+	if (!ASSERT_EQ(opts.retval, sizeof(skel->data->send_data),
+		       "sendmsg bytes"))
+		goto fail;
+
+	/* Step 3: Receive and verify the data */
+	ksock_assert_recv(&env, skel->data->send_data,
+			  sizeof(skel->data->send_data));
+
+fail:
+	ksock_test_env_cleanup(&env);
+	ksock_basic__destroy(skel);
+}
tools/testing/selftests/bpf/progs/ksock_basic.c new file +68 -0

68 lines not on this page · raw message

tools/testing/selftests/bpf/progs/ksock_common.h new file +61 -0

61 lines not on this page · raw message

--
2.34.1

author
#11 lore raw

PATCH v3 4/5 selftests/bpf: Add ksock LSM recursion test

In reply to Mahe Tardy

The bpf_ksock_send() kfunc triggers the security_socket_sendmsg() LSM hook via kernel_sendmsg(), thus attaching an LSM program triggering that kfunc on the same hook would provoke recursion. This test exercises that path and make sure that recursion is guarded and prevented.

---
 .../testing/selftests/bpf/prog_tests/ksock.c  | 82 +++++++++++++++++--
 .../testing/selftests/bpf/progs/ksock_basic.c | 33 +-------
 .../selftests/bpf/progs/ksock_common.h        | 38 +++++++++
 .../selftests/bpf/progs/ksock_recursion.c     | 69 ++++++++++++++++
 4 files changed, 185 insertions(+), 37 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_recursion.c
tools/testing/selftests/bpf/prog_tests/ksock.c +77 -5
index fb88eac9eeba..2db89e9c0186 100644
@@ -6,8 +6,10 @@
 #include "test_progs.h"
 #include "network_helpers.h"
 #include "ksock_basic.skel.h"
+#include "ksock_recursion.skel.h"
 
 #define NS_TEST "ksock_basic_ns"
+#define NS_LSM_RECURSION_TEST "ksock_lsm_recursion_ns"
 #define LOOPBACK_IP "127.0.0.1"
 #define RECV_PORT 7777
 #define RECV_TIMEOUT_SEC 5
@@ -98,15 +100,16 @@ static void ksock_assert_recv(struct ksock_test_env *env, const char *data,
 	ASSERT_MEMEQ(env->buf, data, data_sz, "payload match");
 }
 
-static bool ksock_setup_ctx(struct ksock_basic *skel)
+static bool ksock_setup_ctx(struct bpf_program *prog, __be32 *ipv4_remote,
+			    __u16 *remote_port)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, opts);
 	int err, pfd;
 
-	skel->bss->ipv4_remote = inet_addr(LOOPBACK_IP);
-	skel->bss->remote_port = RECV_PORT;
+	*ipv4_remote = inet_addr(LOOPBACK_IP);
+	*remote_port = RECV_PORT;
 
-	pfd = bpf_program__fd(skel->progs.ksock_setup);
+	pfd = bpf_program__fd(prog);
 	err = bpf_prog_test_run_opts(pfd, &opts);
 	if (!ASSERT_OK(err, "ksock_setup run"))
 		return false;
@@ -137,7 +140,8 @@ void test_ksock_basic(void)
 		goto fail;
 
 	/* Step 1: Run the setup SYSCALL prog to create ksock */
-	if (!ksock_setup_ctx(skel))
+	if (!ksock_setup_ctx(skel->progs.ksock_setup, &skel->bss->ipv4_remote,
+			     &skel->bss->remote_port))
 		goto fail;
 
 	/* Step 2: Run the send SYSCALL prog */
@@ -157,3 +161,71 @@ void test_ksock_basic(void)
 	ksock_test_env_cleanup(&env);
 	ksock_basic__destroy(skel);
 }
+
+void test_ksock_lsm_recursion(void)
+{
+	struct ksock_test_env env;
+	struct ksock_recursion *skel;
+	char trigger = 'x';
+	int tfd = -1;
+	int err;
+	ssize_t n;
+
+	skel = ksock_recursion__open();
+	if (!ASSERT_OK_PTR(skel, "skel open"))
+		return;
+
+	err = ksock_recursion__load(skel);
+	if (!ASSERT_OK(err, "skel load")) {
+		ksock_recursion__destroy(skel);
+		return;
+	}
+
+	if (!ksock_test_env_setup(&env, NS_LSM_RECURSION_TEST))
+		goto fail;
+
+	/* Step 1: Run the setup SYSCALL prog to create the ksock */
+	if (!ksock_setup_ctx(skel->progs.ksock_setup, &skel->bss->ipv4_remote,
+			     &skel->bss->remote_port))
+		goto fail;
+
+	/* Step 2: Attach LSM prog and trigger socket_sendmsg from userspace */
+	skel->links.ksock_socket_sendmsg =
+		bpf_program__attach_lsm(skel->progs.ksock_socket_sendmsg);
+	if (!ASSERT_OK_PTR(skel->links.ksock_socket_sendmsg,
+			   "attach socket_sendmsg lsm"))
+		goto fail;
+
+	tfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+	if (!ASSERT_OK_FD(tfd, "trigger socket"))
+		goto fail;
+
+	skel->bss->target_pid = getpid();
+	skel->bss->trigger_send = 1;
+	n = sendto(tfd, &trigger, sizeof(trigger), 0,
+		   (struct sockaddr *)&env.addr, sizeof(env.addr));
+	skel->bss->target_pid = 0;
+	skel->bss->trigger_send = 0;
+	if (!ASSERT_EQ(n, sizeof(trigger), "trigger sendto"))
+		goto fail;
+
+	/* Step 3: The nested bpf_ksock_send() must hit the recursion guard */
+	if (!ASSERT_EQ(skel->bss->rec_count, 2,
+		       "socket_sendmsg recursion count"))
+		goto fail;
+	if (!ASSERT_EQ(skel->data->rec_kfunc_rets[0], -EBUSY,
+		       "recursive send status"))
+		goto fail;
+	if (!ASSERT_EQ(skel->data->rec_kfunc_rets[1],
+		       sizeof(skel->data->send_data), "outer send bytes"))
+		goto fail;
+
+	ksock_assert_recv(&env, skel->data->send_data,
+			  sizeof(skel->data->send_data));
+
+fail:
+	if (tfd >= 0)
+		close(tfd);
+	ksock_test_env_cleanup(&env);
+	ksock_recursion__destroy(skel);
+}
tools/testing/selftests/bpf/progs/ksock_basic.c +1 -32
index c18dcc2ea37b..189b0fc15770 100644
@@ -8,41 +8,10 @@
 #include "bpf_tracing_net.h"
 #include "ksock_common.h"
 
-__be32 ipv4_remote;
-__u16 remote_port;
-
-char send_data[32] = "hello from bpf ksock";
-
 SEC("syscall")
 int ksock_setup(void *ctx)
 {
-	struct bpf_ksock_create_opts create_opts = {};
-	union bpf_ksock_addr addr = {};
-	struct bpf_ksock *ks;
-	int err = 0;
-
-	create_opts.family = AF_INET;
-	create_opts.type = SOCK_DGRAM;
-	create_opts.protocol = IPPROTO_UDP;
-
-	ks = bpf_ksock_create(&create_opts, sizeof(create_opts), &err);
-	if (!ks)
-		return err;
-
-	addr.sin.sin_family = AF_INET;
-	addr.sin.sin_port = bpf_htons(remote_port);
-	addr.sin.sin_addr.s_addr = ipv4_remote;
-
-	err = bpf_ksock_connect(ks, &addr, sizeof(addr));
-	if (err) {
-		bpf_ksock_release(ks);
-		return err;
-	}
-
-	err = ksock_ctx_insert(ks);
-	if (err && err != -EEXIST)
-		return err;
-	return 0;
+	return do_ksock_setup();
 }
 
 SEC("syscall")
tools/testing/selftests/bpf/progs/ksock_common.h +38 -0
index 6e5b5b089124..c3203220a2d0 100644
@@ -5,6 +5,7 @@
 #define _KSOCK_COMMON_H
 
 #include "errno.h"
+#include <bpf/bpf_endian.h>
 #include <stdbool.h>
 
 #define SOCK_STREAM	1
@@ -58,4 +59,41 @@ static inline int ksock_ctx_insert(struct bpf_ksock *ctx)
 	return 0;
 }
 
+/* Globals for passing config from userspace */
+__be32 ipv4_remote;
+__u16 remote_port;
+
+char send_data[32] = "hello from bpf ksock";
+
+static inline int do_ksock_setup(void)
+{
+	struct bpf_ksock_create_opts create_opts = {};
+	union bpf_ksock_addr addr = {};
+	struct bpf_ksock *ks;
+	int err = 0;
+
+	create_opts.family = AF_INET;
+	create_opts.type = SOCK_DGRAM;
+	create_opts.protocol = IPPROTO_UDP;
+
+	ks = bpf_ksock_create(&create_opts, sizeof(create_opts), &err);
+	if (!ks)
+		return err;
+
+	addr.sin.sin_family = AF_INET;
+	addr.sin.sin_port = bpf_htons(remote_port);
+	addr.sin.sin_addr.s_addr = ipv4_remote;
+
+	err = bpf_ksock_connect(ks, &addr, sizeof(addr));
+	if (err) {
+		bpf_ksock_release(ks);
+		return err;
+	}
+
+	err = ksock_ctx_insert(ks);
+	if (err && err != -EEXIST)
+		return err;
+	return 0;
+}
+
 #endif /* _KSOCK_COMMON_H */
tools/testing/selftests/bpf/progs/ksock_recursion.c new file +69 -0

69 lines not on this page · raw message

--
2.34.1

author
#12 lore raw

PATCH v3 5/5 selftests/bpf: Add ksock test for async callback guard

In reply to Mahe Tardy

Because the kfuncs are going through LSM hooks, allowing their use via workqueue callbacks would expose the wrong credentials. This test ensures the kfunc are preventing any use from these contexts.

---
 .../selftests/bpf/prog_tests/ksock_wq.c       | 34 ++++++++++
 tools/testing/selftests/bpf/progs/ksock_wq.c  | 62 +++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/ksock_wq.c
 create mode 100644 tools/testing/selftests/bpf/progs/ksock_wq.c
tools/testing/selftests/bpf/prog_tests/ksock_wq.c new file +34 -0
new file mode 100644
index 000000000000..5b9c6cc30392
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2026 Isovalent */
+
+#include <unistd.h>
+
+#include "test_progs.h"
+#include "ksock_wq.skel.h"
+
+void test_ksock_wq(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct ksock_wq *skel;
+	int err;
+
+	skel = ksock_wq__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "ksock_wq open and load"))
+		return;
+
+	err = bpf_prog_test_run_opts(
+		bpf_program__fd(skel->progs.ksock_wq_start), &opts);
+	if (!ASSERT_OK(err, "run ksock_wq_start"))
+		goto out;
+	if (!ASSERT_OK(opts.retval, "ksock_wq_start retval"))
+		goto out;
+
+	while (!__atomic_load_n(&skel->bss->callback_done, __ATOMIC_ACQUIRE))
+		usleep(1000);
+
+	ASSERT_EQ(skel->bss->create_err, -EOPNOTSUPP,
+		  "workqueue create rejected");
+
+out:
+	ksock_wq__destroy(skel);
+}
tools/testing/selftests/bpf/progs/ksock_wq.c new file +62 -0

62 lines not on this page · raw message

--
2.34.1