bpf: allow ksock connect/send calls from wq
Subsystems (6)
PATCH v2 1/1 bpf: allow ksock connect/send calls from wq
In the initial implementation in '7ae4eb14c5f9 ("bpf: Add ksock kfuncs")', it was decided to let the ksock kfuncs go through the LSM hooks instead of by-passing them, and thus to disallow the use of the kfuncs from asynchronous workqueues as the credentials would be different than the task context that triggered the kfuncs.
After some discussions, in particular at Plumbers 2026 at the presentation introducing this new ksock API[^1], it was agreed that by-passing the LSM from socket created by BPF programs should be okay and thus that the process context switch from kthread executing the workqueues wouldn't be an issue.
For now, the ksock kfuncs still trigger the LSM hooks but we relax the constraint on not using the workqueues as it will be useful for users triggering connect() or send() from non-sleepable programs (like tracing, support will be extended in future patches).
Also adapt test_ksock_wq() added in 'c93cbdb13f99 ("selftests/bpf: Add ksock test for async callback guard")' to test that the connect() and send() kfuncs are now allowed and work when called from workqueues.
Note that bpf_ksock_create() still checks for workqueue context execution and makes sure the current->nsproxy is defined as we rely on the network namespace to be defined (could be NULL in the case do_exit() was called on the task before the wq was scheduled) and to be the one from the task that triggered the SYSCALL bpf prog.
[^1]: https://lpc.events/event/20/contributions/2430/
---
v1: https://lore.kernel.org/bpf/[email protected]/
net/core/bpf_ksock.c | 27 +------ .../selftests/bpf/prog_tests/ksock_wq.c | 29 +++++-- tools/testing/selftests/bpf/progs/ksock_wq.c | 76 +++++++++++++++---- 3 files changed, 91 insertions(+), 41 deletions(-)
net/core/bpf_ksock.c +4 -23
@@ -37,15 +37,6 @@ static void ksock_release_work_fn(struct work_struct *work)kfree (ks); } -static bool bpf_ksock_has_user_task_context (void ) -{ -/* -* Task work can run from do_exit() after exit_nsproxy_namespaces() -* cleared current->nsproxy, while current is still not a kthread. -*/ -return !(current->flags &PF_KTHREAD ) && current->nsproxy; -} -__bpf_kfunc_start_defs ();/**
@@ -59,7 +50,7 @@ __bpf_kfunc_start_defs();* This function may sleep (sock_create), so it can only be used * in sleepable BPF programs (SYSCALL). * It cannot be called from a BPF workqueue callback because that callback -* does not retain the invoking task's namespace or security context. +* does not retain the invoking task's namespace. * * @opts: Pointer to struct bpf_ksock_create_opts with socket parameters. * @opts__sz: Size of the opts struct.
@@ -73,13 +64,9 @@ bpf_ksock_create(const struct bpf_ksock_create_opts *opts, u32 opts__sz,struct bpf_ksock *ks;int err; -/* -* sock_create() derives the network namespace, credentials, and cgroup -* from current. Kernel threads, including BPF workqueue callbacks, do -* not carry the context of the task that invoked the BPF program. -*/ -if (!bpf_ksock_has_user_task_context ()) { - err = -EOPNOTSUPP ; +/* sock_create() derives the network namespace from current. */ +if ((current->flags &PF_KTHREAD ) || !current->nsproxy) { + err = -EPERM ; goto err_out; }
@@ -165,9 +152,6 @@ __bpf_kfunc int bpf_ksock_connect(struct bpf_ksock *ks,struct sockaddr_storage sa;int addrlen; -if (!bpf_ksock_has_user_task_context ()) -return -EOPNOTSUPP ; -if (!addr || addr__sz !=sizeof (*addr))return -EINVAL ;
@@ -247,9 +231,6 @@ __bpf_kfunc int bpf_ksock_send(struct bpf_ksock *ks, const void *data, };int ret; -if (!bpf_ksock_has_user_task_context ()) -return -EOPNOTSUPP ; - ret =kernel_sendmsg (ks->sock, &msg, &iov,1 , data__sz);return ret;
tools/testing/selftests/bpf/prog_tests/ksock_wq.c +24 -5
@@ -4,6 +4,7 @@#include <unistd.h> #include "test_progs.h" +#include "network_helpers.h" #include "ksock_wq.skel.h" #define CALLBACK_WAIT_RETRIES 1000
@@ -13,13 +14,22 @@ void test_ksock_wq(void) {LIBBPF_OPTS (bpf_test_run_opts, opts);struct ksock_wq *skel; +char recv_data[sizeof (skel->data->send_data)];u32 callback_done; -int err, i; +int err, i, port, rfd; skel =ksock_wq__open_and_load ();if (!ASSERT_OK_PTR (skel,"ksock_wq open and load" ))return ; + rfd =start_server (AF_INET ,SOCK_DGRAM ,"127.0.0.1" ,0 ,1000 ); +if (!ASSERT_OK_FD (rfd,"receiver socket" )) + goto out; + port =get_socket_local_port (rfd); +if (!ASSERT_GE (port,0 ,"receiver port" )) + goto out; + skel->bss->remote_port = port; + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.ksock_wq_start), &opts);if (!ASSERT_OK (err,"run ksock_wq_start" ))
@@ -28,18 +38,27 @@ void test_ksock_wq(void) goto out;for (i =0 ; i <CALLBACK_WAIT_RETRIES ; i++) { -if (__atomic_load_n (&skel->bss->callback_done, __ATOMIC_ACQUIRE)) +if (__atomic_load_n (&skel->bss->callback_done, __ATOMIC_ACQUIRE) ==2 )break ;usleep (CALLBACK_WAIT_US ); } callback_done =__atomic_load_n (&skel->bss->callback_done, __ATOMIC_ACQUIRE); -if (!ASSERT_EQ (callback_done,1 ,"workqueue callback completed" )) +if (!ASSERT_EQ (callback_done,2 ,"workqueue callbacks completed" )) goto out; -ASSERT_EQ (skel->bss->create_err, -EOPNOTSUPP , -"workqueue create rejected" ); +ASSERT_EQ (skel->bss->create_ret, -EPERM ,"workqueue create rejected" ); + +if (!ASSERT_EQ (skel->bss->connect_send_ret,sizeof (recv_data), +"workqueue send bytes" )) + goto out; + err =recv (rfd, recv_data,sizeof (recv_data),0 ); +if (ASSERT_EQ (err,sizeof (recv_data),"recv len" )) +ASSERT_MEMEQ (recv_data, skel->data->send_data,sizeof (recv_data), +"payload match" ); out: +if (rfd >=0 ) +close (rfd);ksock_wq__destroy (skel); }
tools/testing/selftests/bpf/progs/ksock_wq.c +63 -13
111 lines not on this page · raw message
--
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests/bpf: u64 map key causes workqueue double-init on big-endian --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: u64 map key causes workqueue double-init on big-endian --
commit 5eb6c40a460d2cca2d0dee2b208a0164f311d369
Author: Mahe Tardy <[email protected]>
bpf: allow ksock connect/send calls from wq
This commit relaxes the constraint preventing the use of ksock kfuncs from asynchronous workqueues. It also updates the ksock_wq selftest to verify that connect() and send() function properly when called from workqueues.
Quoted 4 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +0
-0
[ ... ]
Quoted 21 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +8
-7@@ -43,20 +73,40 @@ static int ksock_wq_callback(void *map, int *key, void *value) SEC("syscall")int ksock_wq_start (void *ctx) { +struct bpf_ksock_create_opts opts = { + .family =AF_INET , + .type =SOCK_DGRAM , + .protocol =IPPROTO_UDP , + };struct ksock_wq_value *value; -u32 key =0 ; -int err; +struct bpf_ksock *ks; +u64 key =0 ; +int err =0 ; - value =bpf_map_lookup_elem (&work_map, &key); -if (!value) -return -ENOENT ; - err =bpf_wq_init (&value->work, &work_map,0 ); -if (err)
tools/testing/selftests/bpf/progs/ksock_wq.c static int ksock_wq_callback(void *map, int *key, void *value) · line 93
Quoted 10 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +5
-2+ ks =bpf_ksock_create (&opts,sizeof (opts), &err); +if (!ks)return err; - err =bpf_wq_set_callback (&value->work, ksock_wq_callback,0 ); + err =ksock_ctx_insert (ks);if (err)return err; -return bpf_wq_start (&value->work,0 ); +for (key =0 ; key <2 ; key++) { + value =bpf_map_lookup_elem (&work_map, &key);
[Severity: Medium]
Can this cause a workqueue double-init on big-endian architectures?
The work_map map is defined with a u32 key, but the key variable here was changed to u64. On big-endian systems, passing &key to bpf_map_lookup_elem() will read the most significant 32 bits, which will evaluate to 0 for both key = 0 and key = 1.
Because of this, the loop would fetch the map element for index 0 twice. During the second iteration, bpf_wq_init() would be called on value->work while it may already be actively queued from the first iteration. Does re-initializing an active work_struct without serialization run the risk of corrupting the global workqueue list and crashing?
Quoted 17 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +16
-0+if (!value) +return -ENOENT ; + err =bpf_wq_init (&value->work, &work_map,0 ); +if (err) +return err; +if (key) + err =bpf_wq_set_callback (&value->work, ksock_wq_create,0 ); +else + err =bpf_wq_set_callback (&value->work, ksock_wq_connect_send,0 ); +if (err) +return err; + err =bpf_wq_start (&value->work,0 ); +if (err) +return err; + } +return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 58 lines · On Tue, Oct 13, 2026 at 06:35:59PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests/bpf: u64 map key causes workqueue double-init on big-endian --commit
5eb6c40a460d2cca2d0dee2b208a0164f311d369
Author: Mahe Tardy <[email protected]>bpf: allow ksock connect/send calls from wq
This commit relaxes the constraint preventing the use of ksock kfuncs from asynchronous workqueues. It also updates the
ksock_wqselftest to verify thatconnect()andsend()function properly when called from workqueues.Quoted 4 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +0
-0[ ... ]
Quoted 21 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +8
-7@@ -43,20 +73,40 @@ static int ksock_wq_callback(void *map, int *key, void *value) SEC("syscall")int ksock_wq_start (void *ctx) { +struct bpf_ksock_create_opts opts = { + .family =AF_INET , + .type =SOCK_DGRAM , + .protocol =IPPROTO_UDP , + };struct ksock_wq_value *value; -u32 key =0 ; -int err; +struct bpf_ksock *ks; +u64 key =0 ; +int err =0 ; - value =bpf_map_lookup_elem (&work_map, &key); -if (!value) -return -ENOENT ; - err =bpf_wq_init (&value->work, &work_map,0 ); -if (err)tools/testing/selftests/bpf/progs/ksock_wq.c static int ksock_wq_callback(void *map, int *key, void *value) · line 93
Quoted 10 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +5
-2+ ks =bpf_ksock_create (&opts,sizeof (opts), &err); +if (!ks)return err; - err =bpf_wq_set_callback (&value->work, ksock_wq_callback,0 ); + err =ksock_ctx_insert (ks);if (err)return err; -return bpf_wq_start (&value->work,0 ); +for (key =0 ; key <2 ; key++) { + value =bpf_map_lookup_elem (&work_map, &key);[Severity: Medium]
Can this cause a workqueue double-init on big-endian architectures?The
work_mapmap is defined with a u32 key, but the key variable here was changed to u64. On big-endian systems, passing &key tobpf_map_lookup_elem()will read the most significant 32 bits, which will evaluate to 0 for both key = 0 and key = 1.
Quoted 5 lines
Because of this, the loop would fetch the map element for index 0 twice. During the second iteration,
bpf_wq_init()would be called onvalue->workwhile it may already be actively queued from the first iteration. Does re-initializing an activework_structwithout serialization run the risk of corrupting the global workqueue list and crashing?
Yeah key should have been a u32 but when doing so the verifier loose track of its value because it's only taking 4 bytes on the stack and then it detects an infinite loop for my two iterations, I'll do:
u32 key __attribute__((aligned(8))) = 0;
So that the verifier can properly track the value in the for loop.
Quoted 21 lines
Quoted 18 lines
tools/testing/selftests/bpf/progs/ksock_wq.c +16
-0+if (!value) +return -ENOENT ; + err =bpf_wq_init (&value->work, &work_map,0 ); +if (err) +return err; +if (key) + err =bpf_wq_set_callback (&value->work, ksock_wq_create,0 ); +else + err =bpf_wq_set_callback (&value->work, ksock_wq_connect_send,0 ); +if (err) +return err; + err =bpf_wq_start (&value->work,0 ); +if (err) +return err; + } +return 0 ; }Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1