net: tls: fix async split record handling
Subsystems (6)
v2 cover net: tls: fix async split record handling
When BPF sk_msg apply_bytes splits an open kTLS TX record and the selected AEAD provider completes asynchronously, tls_push_record() currently returns -EINPROGRESS before reattaching the split remainder. The peer can receive a truncated stream and the detached tls_rec remainder is leaked.
Patch 1 keeps the split remainder rooted before returning -EINPROGRESS, continues the BPF verdict drain loop after queueing an async record, and waits for already-queued async encryption if a later verdict iteration must return a hard error. That last part addresses the return-value masking issue reported by Sashiko on v1.
Patch 2 adds a selftest covering the sync and async providers for the split-record path. v2 also checks the BPF program fd before attaching the selftest program.
This report and patch were prepared with AI assistance. The generated analysis was checked against the current source, the reproducer was run against vulnerable and fixed kernels, and the fix was runtime-validated on QEMU/KVM with a KASAN+LOCKDEP-instrumented kernel against net base 5db89c995. The pass-then-drop BPF probe that exercises Finding 1's failure mode ran clean (no KASAN report, no lockdep splat).
v1:
https://lore.kernel.org/all/[email protected]/
Sashiko review:
https://sashiko.dev/#/patchset/[email protected]
John Fastabend reply on v1 (confirmed Sashiko's return-value masking finding is a legitimate concern; this v2 is the response):
https://lore.kernel.org/all/huduxtn6parzgiaf5cyiyrrvjjvx6jsdedowvrd4nkwmuyeind@j6migjgofh2i/
Changes since v1:
- Preserve the later hard error from bpf_exec_tx_verdict() after waiting for any earlier async encryption queued in the same verdict drain loop.
- Flush completed async records after that local wait.
- Check bpf_program__fd() before bpf_prog_attach() in the selftest.
- Leave the __SK_REDIRECT socket-lock-drop finding out of this series; it appears pre-existing and should be handled separately if maintainers want to pursue it.
Christopher Lusk (2):
net: tls: preserve split open record on async encrypt
selftests: net: add kTLS async split record regression
net/tls/tls_sw.c | 40 +- tools/testing/selftests/net/Makefile | 5 + .../selftests/net/ktls_async_split.bpf.c | 24 ++ .../testing/selftests/net/ktls_async_split.c | 393 ++++++++++++++++++ 4 files changed, 454 insertions(+), 8 deletions(-) create mode 100644 tools/testing/selftests/net/ktls_async_split.bpf.c create mode 100644 tools/testing/selftests/net/ktls_async_split.c
Signature · 1 line
2.54.0
PATCH v2 1/2 net: tls: preserve split open record on async encrypt
When the BPF sk_msg verdict sets apply_bytes smaller than the current open record, tls_push_record() splits ctx->open_rec into the record being encrypted and a remainder record. The synchronous path reattaches the remainder to ctx->open_rec before continuing.
If the selected AEAD provider completes asynchronously, tls_do_encryption() returns -EINPROGRESS after unhooking ctx->open_rec. tls_push_record() currently returns immediately in that case, before the split remainder is reattached. The remainder is no longer reachable through ctx->open_rec or ctx->tx_list, which can silently drop transmitted data and leak the unreachable tls_rec.
Keep the split remainder rooted even when encryption of the first record is pending asynchronously, and continue the BPF verdict drain loop after an async record has been queued. If that loop then hits a later verdict error, wait for the pending async encryption before returning the error so zerocopy user pages cannot be released while cryptd still reads them.
--- net/tls/tls_sw.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-)
net/tls/tls_sw.c +32 -8
@@ -840,16 +840,19 @@ static int tls_push_record(struct sock *sk, int flags, rc =tls_do_encryption (sk, tls_ctx, ctx, req, msg_pl->sg.size + prot->tail_size, i);if (rc <0 ) { -if (rc != -EINPROGRESS ) { -tls_err_abort (sk, -EBADMSG ); -if (split) { - tls_ctx->pending_open_record_frags = true; -tls_merge_open_record (sk, rec, tmp, orig_end); - } +if (rc == -EINPROGRESS ) + goto split_done; + +tls_err_abort (sk, -EBADMSG ); +if (split) { + tls_ctx->pending_open_record_frags = true; +tls_merge_open_record (sk, rec, tmp, orig_end); } ctx->async_capable =1 ;return rc; - }else if (split) { + } +split_done: +if (split) { msg_pl = &tmp->msg_plaintext; msg_en = &tmp->msg_encrypted;sk_msg_trim (sk, msg_en, msg_pl->sg.size + prot->overhead_size);
@@ -857,6 +860,11 @@ static int tls_push_record(struct sock *sk, int flags, ctx->open_rec = tmp; } +if (rc <0 ) { + ctx->async_capable =1 ; +return rc; + } +return tls_tx_records (sk, flags); }
@@ -871,6 +879,8 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,struct sock *sk_redir;struct tls_rec *rec;bool enospc, policy, redir_ingress; +bool async = false; +int async_err =0 ;int err =0 , send;u32 delta =0 ;
@@ -920,6 +930,10 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,switch (psock->eval) {case __SK_PASS: err =tls_push_record (sk, flags, record_type); +if (err == -EINPROGRESS ) { + async = true; + err =0 ; + }if (err && err != -EINPROGRESS && sk->sk_err ==EBADMSG ) { *copied -=sk_msg_free (sk, msg);tls_free_open_rec (sk);
@@ -988,8 +1002,18 @@ static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk, goto more_data; } out_err: +if (async && err && err != -EINPROGRESS ) { + async_err =tls_encrypt_async_wait (ctx); +if (test_and_clear_bit (BIT_TX_SCHEDULED , &ctx->tx_bitmask)) { +/* tx_lock is held; the worker will reschedule if needed. */ +cancel_delayed_work (&ctx->tx_work.work); +tls_tx_records (sk, flags); + } +if (async_err) + err = async_err; + }sk_psock_put (sk, psock); -return err; +return err ?: (async ? -EINPROGRESS :0 ); }static int tls_sw_push_pending_record (struct sock *sk,int flags)
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerRe: [PATCH net v2 1/2] net: tls: preserve split open record on async encrypt
Quoted 4 lines · On Wed, 20 May 2026 22:58:39 -0400 Christopher Lusk wrote:
When the BPF
sk_msgverdict setsapply_bytessmaller than the current open record,tls_push_record()splitsctx->open_recinto the record being encrypted and a remainder record. The synchronous path reattaches the remainder toctx->open_recbefore continuing.
The current understanding is that this code has no real users. So let's try clear async_capable if BPF is attached and avoid all these bugs in record handling, please? The savings from zero copy are negligible.
PATCH v2 2/2 selftests: net: add kTLS async split record regression
In reply to Christopher LuskAdd a kTLS TX regression test for the BPF sk_msg apply_bytes split-record path. The test sends four 4096-byte writes through a sockmap verdict program that applies 512-byte verdict windows, then verifies that the peer receives the full TLS-protected stream.
Run the case once with the synchronous provider and once after instantiating an async pcrypt gcm(aes) provider. Vulnerable kernels silently truncate the async run because the split open-record remainder is lost when encryption returns -EINPROGRESS.
Validate the loaded BPF program fd before attaching it so setup failures are reported directly instead of being passed to bpf_prog_attach().
--- tools/testing/selftests/net/Makefile | 5 + .../selftests/net/ktls_async_split.bpf.c | 24 ++ .../testing/selftests/net/ktls_async_split.c | 393 ++++++++++++++++++ 3 files changed, 422 insertions(+) create mode 100644 tools/testing/selftests/net/ktls_async_split.bpf.c create mode 100644 tools/testing/selftests/net/ktls_async_split.c
tools/testing/selftests/net/Makefile +5 -0
@@ -179,6 +179,7 @@ TEST_GEN_PROGS := \ epoll_busy_poll \ icmp_rfc4884 \ ipv6_fragmentation \ + ktls_async_split \ proc_net_pktgen \ reuseaddr_conflict \ reuseport_bpf \
@@ -236,3 +237,7 @@ $(OUTPUT)/bind_bhash: LDLIBS += -lpthread $(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/ include bpf.mk + +$(OUTPUT)/ktls_async_split: CFLAGS += -I../../../lib +$(OUTPUT)/ktls_async_split: LDLIBS += $(BPFOBJ) -lelf -lz +$(OUTPUT)/ktls_async_split: $(BPFOBJ)
tools/testing/selftests/net/ktls_async_split.bpf.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +#ifndef SK_PASS +#define SK_PASS 1 +#endif + +char LICENSE[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_SOCKMAP); + __uint(max_entries, 4); + __type(key, __u32); + __type(value, int); +} sock_map SEC(".maps"); + +SEC("sk_msg") +int apply_bytes_verdict (struct sk_msg_md *msg) +{ +bpf_msg_apply_bytes (msg,512 ); +return SK_PASS ; +}
tools/testing/selftests/net/ktls_async_split.c new file +393 -0
393 lines not on this page · raw message
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerRe: [PATCH net v2 2/2] selftests: net: add kTLS async split record regression
Quoted 2 lines · On Wed, 20 May 2026 22:58:40 -0400 Christopher Lusk wrote:
Assisted-by Codex:gpt-5.5Assisted-by Claude:claude-opus-4-7
There are already tests for sockmap + TLS in bpf, point your bots at that please.
[email protected]
AI reviewRe: [PATCH net v2 2/2] selftests: net: add kTLS async split record regression
In reply to Christopher LuskThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Memory and file descriptor leak of `struct bpf_object` on error paths inside `attach_bpf()`.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Memory and file descriptor leak of struct bpf_object on error paths inside attach_bpf().
- [Low] Including libbpf source headers directly instead of installed/exported headers in Makefile. --
commit 4256015d1c1b459bffc8e935d002cdb7f0465799
Author: Christopher Lusk <[email protected]>
selftests: net: add kTLS async split record regression
Add a kTLS TX regression test for the BPF sk_msg apply_bytes split-record path. The test sends four 4096-byte writes through a sockmap verdict program that applies 512-byte verdict windows, then verifies that the peer receives the full TLS-protected stream.
Quoted 4 lines
tools/testing/selftests/net/Makefile +0
-0
[ ... ]
tools/testing/selftests/net/Makefile $(OUTPUT)/bind_bhash: LDLIBS += -lpthread · line 241
Quoted 6 lines
tools/testing/selftests/net/Makefile +2
-0@@ -236,3 +237,7 @@ $(OUTPUT)/bind_bhash: LDLIBS += -lpthread $(OUTPUT)/io_uring_zerocopy_tx: CFLAGS += -I../../../include/ include bpf.mk + +$(OUTPUT)/ktls_async_split: CFLAGS += -I../../../lib
[Severity: Low]
Is this include path correct? By pointing directly to lib instead of the exported headers like ../../../include/, could we bypass the proper libbpf build and export step and rely on raw source headers instead?
Quoted 5 lines
tools/testing/selftests/net/ktls_async_split.c new file +0
-0
[ ... ]
tools/testing/selftests/net/ktls_async_split.c line 242
Quoted 12 lines
tools/testing/selftests/net/ktls_async_split.c new file +12
-0+static int attach_bpf (const char *obj_path,int sock_fd,struct bpf_object **obj_out) +{ +struct bpf_object *obj; +struct bpf_program *prog; +int map_fd, prog_fd; +__u32 key =0 ; + + obj =bpf_object__open_file (obj_path,NULL ); +if (libbpf_get_error (obj)) +return -EINVAL ; +if (bpf_object__load (obj)) +return -EINVAL ;
[Severity: Medium]
Does this leak the bpf_object structure and associated resources on error paths?
Since *obj_out is only assigned at the end of the function upon success, if bpf_object__load() or any subsequent operations fail, the caller receives a NULL pointer and cannot clean up the leaked object via bpf_object__close().
Quoted 20 lines
tools/testing/selftests/net/ktls_async_split.c new file +20
-0+ + prog =bpf_object__find_program_by_name (obj,"apply_bytes_verdict" ); +if (!prog) +return -ENOENT ; + prog_fd =bpf_program__fd (prog); +if (prog_fd <0 ) +return prog_fd; + + map_fd =bpf_object__find_map_fd_by_name (obj,"sock_map" ); +if (map_fd <0 ) +return -ENOENT ; + +if (bpf_prog_attach (prog_fd, map_fd,BPF_SK_MSG_VERDICT ,0 )) +return -errno; +if (bpf_map_update_elem (map_fd, &key, &sock_fd,BPF_ANY )) +return -errno; + + *obj_out = obj; +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2