Introduce bpf_ksock
Subsystems (8)
v2 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.
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/
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 | 46 +++ include/linux/sched.h | 4 + include/linux/socket.h | 2 + kernel/bpf/verifier.c | 3 + net/core/Makefile | 3 + net/core/bpf_ksock.c | 361 ++++++++++++++++++ net/socket.c | 32 +- .../testing/selftests/bpf/prog_tests/ksock.c | 233 +++++++++++ .../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, 971 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
PATCH v2 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
@@ -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
@@ -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
PATCH v2 2/5 bpf: Add ksock kfuncs
In reply to Mahe TardyAdd 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 | 46 +++++ include/linux/sched.h | 4 + kernel/bpf/verifier.c | 3 + net/core/Makefile | 3 + net/core/bpf_ksock.c | 361 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 417 insertions(+) create mode 100644 include/linux/bpf_ksock.h create mode 100644 net/core/bpf_ksock.c
include/linux/bpf_ksock.h new file +46 -0
@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (c) 2026 Isovalent */ + +#ifndef _BPF_KSOCK_H +#define _BPF_KSOCK_H + +#include <linux/types.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; +}; + +/** +* struct bpf_ksock_addr_opts - BPF kernel socket address parameters +* @family: Address family: AF_INET or AF_INET6. +* @reserved: Must be zero. Reserved for future use. +* @port: Port in host byte order. +* @scope_id: IPv6 scope ID for scoped AF_INET6 addresses, or zero. +* Must be zero when family=AF_INET. +* @ipv4_addr: IPv4 address in network byte order. Used when family=AF_INET. +* @ipv6_addr: IPv6 address (16 bytes, network byte order). Used when family=AF_INET6. +*/ +struct bpf_ksock_addr_opts { +__u8 family; +__u8 reserved; +__u16 port; +__u32 scope_id; + +union { +__be32 ipv4_addr; +__u32 ipv6_addr[4 ];/* in6_addr; network order */ + }; +}; + +#endif /* _BPF_KSOCK_H */
include/linux/sched.h +4 -0
@@ -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
@@ -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
@@ -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 +361 -0
361 lines not on this page · raw message
--
2.34.1
Re: [PATCH bpf-next v2 2/5] bpf: Add ksock kfuncs
Quoted 65 lines · 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_DGRAMsocket types withIPPROTO_UDPprotocol but could be easily extended toSOCK_STREAMandIPPROTO_TCPin 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_worksincesock_releasesleeps)The setup kfuncs
bpf_ksock_create,bpf_ksock_connect, can be called from SYSCALL programs only. Whilebpf_ksock_acquire,bpf_ksock_releaseandbpf_ksock_sendcan 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_ksockstruct. Cleanup is deferred viaqueue_rcu_work()becausesock_release()may sleep.The kfuncs are only compiled when
CONFIG_INETis enabled, as they specifically supportAF_INETandAF_INET6sockets.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.Signed-off-by Mahe Tardy <[email protected]>--- include/linux/bpf_ksock.h | 46 +++++ include/linux/sched.h | 4 + kernel/bpf/verifier.c | 3 + net/core/Makefile | 3 + net/core/bpf_ksock.c | 361 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 417 insertions(+) create mode 100644 include/linux/bpf_ksock.h create mode 100644 net/core/bpf_ksock.cinclude/linux/bpf_ksock.h new file +8
-0@@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (c) 2026 Isovalent */ + +#ifndef _BPF_KSOCK_H +#define _BPF_KSOCK_H + +#include <linux/types.h> +
[..]
Quoted 26 lines
include/linux/bpf_ksock.h new file +26
-0+/** +* 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; +}; + +/** +* struct bpf_ksock_addr_opts - BPF kernel socket address parameters +* @family: Address family: AF_INET or AF_INET6. +* @reserved: Must be zero. Reserved for future use. +* @port: Port in host byte order. +* @scope_id: IPv6 scope ID for scoped AF_INET6 addresses, or zero. +* Must be zero when family=AF_INET. +* @ipv4_addr: IPv4 address in network byte order. Used when family=AF_INET. +* @ipv6_addr: IPv6 address (16 bytes, network byte order). Used when family=AF_INET6. +*/ +struct bpf_ksock_addr_opts {
include/linux/bpf_ksock.h line 44
Quoted 10 lines
include/linux/bpf_ksock.h new file +10
-0+__u8 family; +__u8 reserved; +__u16 port; +__u32 scope_id; + +union { +__be32 ipv4_addr; +__u32 ipv6_addr[4 ];/* in6_addr; network order */ + }; +};
Do you add these because of BTF type checking? Can we have something a bit more orthodox like:
union bpf_sockaddr {
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
};?
I hope we can just pass these to sock_create and make it do all the input checking..
Re: [PATCH bpf-next v2 2/5] bpf: Add ksock kfuncs
Quoted 7 lines · Stanislav Fomichev
Quoted 6 lines · 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_DGRAMsocket types withIPPROTO_UDPprotocol but could be easily extended toSOCK_STREAMandIPPROTO_TCPin the future.
[...]
Quoted 31 lines
Quoted 12 lines
include/linux/bpf_ksock.h new file +12
-0+ +/** +* struct bpf_ksock_addr_opts - BPF kernel socket address parameters +* @family: Address family: AF_INET or AF_INET6. +* @reserved: Must be zero. Reserved for future use. +* @port: Port in host byte order. +* @scope_id: IPv6 scope ID for scoped AF_INET6 addresses, or zero. +* Must be zero when family=AF_INET. +* @ipv4_addr: IPv4 address in network byte order. Used when family=AF_INET. +* @ipv6_addr: IPv6 address (16 bytes, network byte order). Used when family=AF_INET6. +*/ +struct bpf_ksock_addr_opts {include/linux/bpf_ksock.h line 44
Quoted 10 lines
include/linux/bpf_ksock.h new file +10
-0+__u8 family; +__u8 reserved; +__u16 port; +__u32 scope_id; + +union { +__be32 ipv4_addr; +__u32 ipv6_addr[4 ];/* in6_addr; network order */ + }; +};Do you add these because of BTF type checking? Can we have something a bit more orthodox like:
union bpf_sockaddr { struct sockaddr_in sin; struct sockaddr_in6 sin6; };
Quoted 4 lines
?
I hope we can just pass these to
sock_createand make it do all the input checking..
Yeah probably, I didn't think about this too much. I inherited it from the netpoll tentative where Daniel suggested to take inspiration from the fib lookup helpers[^1], which made sense for the netpoll call. If we can simplify this we can get rid of bpf_ksock_parse_addr(). Let me try.
PATCH v2 3/5 selftests/bpf: Add ksock kfunc test
In reply to Mahe TardyAdd 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 | 62 +++++++ 3 files changed, 289 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
@@ -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 +62 -0
62 lines not on this page · raw message
--
2.34.1
PATCH v2 4/5 selftests/bpf: Add ksock LSM recursion test
In reply to Mahe TardyThe 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 | 84 +++++++++++++++++-- .../testing/selftests/bpf/progs/ksock_basic.c | 33 +------- .../selftests/bpf/progs/ksock_common.h | 37 ++++++++ .../selftests/bpf/progs/ksock_recursion.c | 69 +++++++++++++++ 4 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/ksock_recursion.c
tools/testing/selftests/bpf/prog_tests/ksock.c +79 -5
@@ -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,9 @@ 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 +162,72 @@ 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
@@ -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 = {}; -struct bpf_ksock_addr_opts addr_opts = {}; -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_opts.family =AF_INET ; - addr_opts.port = remote_port; - addr_opts.ipv4_addr = ipv4_remote; - - err =bpf_ksock_connect (ks, &addr_opts,sizeof (addr_opts)); -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 +37 -0
@@ -59,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 = {}; +struct bpf_ksock_addr_opts addr_opts = {}; +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_opts.family =AF_INET ; + addr_opts.port = remote_port; + addr_opts.ipv4_addr = ipv4_remote; + + err =bpf_ksock_connect (ks, &addr_opts,sizeof (addr_opts)); +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
PATCH v2 5/5 selftests/bpf: Add ksock test for async callback guard
In reply to Mahe TardyBecause 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
@@ -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