From: Dickson Wu MSG_PEEK can leave decrypted records on rx_list after TLS has consumed their encrypted TCP skbs. The cleartext skbs have no socket owner, so the memory they hold is not charged to the socket, the TCP memory counters or the reader's memcg. A single peek with a large length keeps decrypting and pinning records while TCP reopens the window for more, which can drive a global OOM that kills processes in other cgroups. Charge a newly decrypted skb to the socket when it is retained on rx_list. If the normal receive reservation fails, force the reservation for that record and stop decrypting more records in this call, so the reader gets what was already decrypted rather than an error. Synchronous readers that consume full records do not retain the cleartext skb. With async crypto, a record can be queued and charged briefly before the same call drains it. tls_sw_splice_read() and tls_sw_read_sock() can also put a newly decrypted record back at the head of rx_list after a partial read, so charge those insertions too. An skb that was charged when it was first retained keeps that charge when it is dequeued and requeued, and is not charged again. Export sk_forced_mem_schedule() so TLS built as a module can use it, and add a splice selftest that checks a retained record is charged once and released once it is read. Fixes: 692d7b5d1f91 ("tls: Fix recvmsg() to be able to peek across multiple records") Signed-off-by: Dickson Wu --- Changes in v2: - Also charge the records that tls_sw_splice_read() and tls_sw_read_sock() put back at the head of rx_list. v1 charged only the recvmsg insertions. - Don't charge an skb again when it is requeued with its charge. - Add a splice_requeue_rmem test to tools/testing/selftests/net/tls.c. - v1: https://lore.kernel.org/r/20261127-ktls-peek-v1-1-244836f9f89e@certik.com v2 was tested under x86_64 UML with KASAN on net/main 28a371228d4e: - The new splice_requeue_rmem test reads 1 byte of a record through a pipe, twice. Receive memory reads 0, 0, 0 on v1 (the requeued record is never charged) and 0, 21440, 21440 on v2 (charged once, not again on the second requeue), and drops back to 0 once the record is read. It fails on v1 (TLS 1.2 AES-GCM) and passes on v2 for the AES-GCM, ChaCha20-Poly1305, ARIA-GCM and SM4-CCM variants. - The full tls selftest suite passes 978/978 on v2, with no KASAN, BUG or WARNING. - W=1 build of net/tls/ and net/ipv4/tcp.o is clean. The recvmsg path is unchanged from v1, so the v1 runs still apply: under UML with KASAN and cgroup v2 memcg, TLS 1.2 AES-GCM-128 over loopback, an unprivileged reader (uid 1000) in a cgroup with memory.max=16M and memory.swap.max=0: - One 192 MiB MSG_PEEK read pinned about 257 MiB on stock and 2684 KiB with the patch. - A 400 MiB peek on stock, with a 300 MiB process in the root cgroup, hit a global OOM (CONSTRAINT_NONE) that killed that unrelated process. With the patch the peek returned early and nothing was killed. - A TLS 1.3 normal reader in a tight memcg completed a 16 MiB transfer with and without the patch, and a 64 MiB peek-then-read transfer compared every byte. Not covered: tls_sw_read_sock() is only reached by in-kernel readers (nvme-tcp over TLS, for example), so its two requeue sites were checked by reading the code, not at runtime. The new selftest covers only the splice requeue. The MSG_PEEK and forced reservation paths are covered by the memcg runs above, not by a selftest. Async crypto and device offload were not exercised. The TLS ULP has to be built in or already loaded, since an unprivileged TCP_ULP setsockopt won't autoload it. This was found and the fix prepared with AI coding tools (OpenAI Codex agents, with review by Claude). I went over the analysis, the review and the patch before signing off. net/ipv4/tcp.c | 1 + net/tls/tls_sw.c | 30 ++++++++++++++++---- tools/testing/selftests/net/tls.c | 47 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 80de376a9..3c8293ade 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -925,6 +925,7 @@ void sk_forced_mem_schedule(struct sock *sk, int size) sk_memory_allocated_add(sk, amt); } +EXPORT_SYMBOL_GPL(sk_forced_mem_schedule); struct sk_buff *tcp_stream_alloc_skb(struct sock *sk, gfp_t gfp, bool force_schedule) diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 226e0f176..3b8ab2106 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -1826,6 +1826,25 @@ static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx) release_sock(sk); } +static bool tls_rx_list_add(struct sock *sk, struct sk_buff_head *list, + struct sk_buff *skb, bool head) +{ + bool forced = false; + + if (!skb->sk) { + forced = !sk_rmem_schedule(sk, skb, skb->truesize); + if (forced) + sk_forced_mem_schedule(sk, skb->truesize); + skb_set_owner_r(skb, sk); + } + if (head) + __skb_queue_head(list, skb); + else + __skb_queue_tail(list, skb); + + return forced; +} + int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, @@ -1923,7 +1942,7 @@ int tls_sw_recvmsg(struct sock *sk, DEBUG_NET_WARN_ON_ONCE(darg.zc); tls_rx_rec_done(ctx); put_on_rx_list_err: - __skb_queue_tail(&ctx->rx_list, darg.skb); + tls_rx_list_add(sk, &ctx->rx_list, darg.skb, false); goto recv_end; } @@ -1953,7 +1972,8 @@ int tls_sw_recvmsg(struct sock *sk, put_on_rx_list: decrypted += chunk; len -= chunk; - __skb_queue_tail(&ctx->rx_list, skb); + if (tls_rx_list_add(sk, &ctx->rx_list, skb, false)) + goto recv_end; if (unlikely(control != TLS_RECORD_TYPE_DATA)) break; continue; @@ -2105,7 +2125,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, return copied ? : err; splice_requeue: - __skb_queue_head(&ctx->rx_list, skb); + tls_rx_list_add(sk, &ctx->rx_list, skb, true); goto splice_read_end; } @@ -2196,7 +2216,7 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc, if (used < rxm->full_len) { rxm->offset += used; rxm->full_len -= used; - __skb_queue_head(&ctx->rx_list, skb); + tls_rx_list_add(sk, &ctx->rx_list, skb, true); } else { consume_skb(skb); } @@ -2207,7 +2227,7 @@ int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc, return copied ? : err; read_sock_requeue: - __skb_queue_head(&ctx->rx_list, skb); + tls_rx_list_add(sk, &ctx->rx_list, skb, true); goto read_sock_end; } diff --git a/tools/testing/selftests/net/tls.c b/tools/testing/selftests/net/tls.c index 59e403b5f..fc5e3325a 100644 --- a/tools/testing/selftests/net/tls.c +++ b/tools/testing/selftests/net/tls.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -109,6 +110,15 @@ static void memrnd(void *s, size_t n) *byte++ = rand(); } +static __u32 get_rmem(struct __test_metadata *_metadata, int fd) +{ + __u32 meminfo[SK_MEMINFO_VARS] = {}; + socklen_t len = sizeof(meminfo); + + EXPECT_EQ(getsockopt(fd, SOL_SOCKET, SO_MEMINFO, meminfo, &len), 0); + return meminfo[SK_MEMINFO_RMEM_ALLOC]; +} + static void ulp_sock_pair(struct __test_metadata *_metadata, int *fd, int *cfd, bool *notls) { @@ -889,6 +899,43 @@ TEST_F(tls, splice_to_pipe) EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0); } +TEST_F(tls, splice_requeue_rmem) +{ + __u32 rmem_before, rmem_first, rmem_second; + int send_len = TLS_PAYLOAD_MAX_LEN; + char mem_send[TLS_PAYLOAD_MAX_LEN]; + char mem_recv[TLS_PAYLOAD_MAX_LEN]; + int p[2]; + + if (self->notls) + SKIP(return, "no TLS support"); + + memrnd(mem_send, sizeof(mem_send)); + ASSERT_GE(pipe(p), 0); + rmem_before = get_rmem(_metadata, self->cfd); + + ASSERT_EQ(send(self->fd, mem_send, send_len, 0), send_len); + ASSERT_EQ(splice(self->cfd, NULL, p[1], NULL, 1, 0), 1); + ASSERT_EQ(read(p[0], mem_recv, 1), 1); + rmem_first = get_rmem(_metadata, self->cfd); + EXPECT_GE(rmem_first, rmem_before + send_len); + + ASSERT_EQ(splice(self->cfd, NULL, p[1], NULL, 1, 0), 1); + ASSERT_EQ(read(p[0], mem_recv + 1, 1), 1); + rmem_second = get_rmem(_metadata, self->cfd); + TH_LOG("rmem before=%u first=%u second=%u", + rmem_before, rmem_first, rmem_second); + EXPECT_EQ(rmem_second, rmem_first); + + ASSERT_EQ(recv(self->cfd, mem_recv + 2, send_len - 2, MSG_WAITALL), + send_len - 2); + EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0); + EXPECT_EQ(get_rmem(_metadata, self->cfd), rmem_before); + + close(p[0]); + close(p[1]); +} + /* The socket's O_NONBLOCK governs the record wait, as on a plain TCP * socket, even without SPLICE_F_NONBLOCK. */ -- 2.43.0