From: Juan Agustin Niccolini <juanagusnicco@gmail.com>
To: netdev@vger.kernel.org
Cc: John Fastabend <john.fastabend@gmail.com>,
Jakub Kicinski <kuba@kernel.org>,
Sabrina Dubroca <sd@queasysnail.net>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Simon Horman <horms@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Alexei Starovoitov <ast@kernel.org>,
Jiayuan Chen <jiayuan.chen@linux.dev>,
linux-kernel@vger.kernel.org,
Juan Agustin Niccolini <juanagusnicco@gmail.com>,
Eddy Tsalolikhin <eddsterxyz@gmail.com>,
Kyle Zeng <kylebot@openai.com>
Subject: [PATCH net] tls: set the copy cursor before cloning plaintext
Date: Mon, 23 Nov 2026 01:38:42 -0500 [thread overview]
Message-ID: <20261123063842.64536-1-juanagusnicco@gmail.com> (raw)
tls_sw_sendmsg_locked() copies into the open record by cloning space
with tls_clone_plaintext_msg() and filling it with
sk_msg_memcopy_from_iter(), which starts at sg.curr and sg.copybreak
as left by whatever last added to the record. The copy trusts other
paths to leave that cursor in the right state, and they have not.
Since commit d829e9c4112b ("tls: convert to generic sk_msg interface"),
the path that adds the caller's own pages (sendpage, later splice) did
not move sg.curr, so the next copy wrote into those pages. Commit
c5a595000e26 ("net: tls, update curr on splice as well") fixed that by
making the splice path leave sg.curr at sg.end with copybreak 0. That
is only correct if the clone adds a new element. sk_msg_clone() instead
grows the last element when the cloned range is contiguous with it, so
sg.end does not move, and the copy starts at an unused slot, wraps
around the ring and writes from sg.start, into the spliced pages again.
The spliced pages can be page cache pages of a file the caller opened
read-only, so an unprivileged user can change the cached contents of a
file it cannot write.
The copy path always appends after the data already in the record, so
set the cursor there before cloning: on the last element with copybreak
at its length, or at sg.start when the record is empty, as
sk_msg_reset_curr() does. That holds whether the clone grows the last
element or adds a new one, and it covers every path that adds to the
record, whatever state it left the cursor in.
This is where the cursor should have been set all along. With it set
here, the copy never depended on the splice path moving sg.curr, so
neither the original bug nor the one introduced by its fix would
have existed.
I found this bug during an LLM-based audit. Kyle Zeng and Eddy
Tsalolikhin independently reported it before and are credited below.
Fixes: d829e9c4112b ("tls: convert to generic sk_msg interface")
Fixes: c5a595000e26 ("net: tls, update curr on splice as well")
Reported-by: Eddy Tsalolikhin <eddsterxyz@gmail.com>
Reported-by: Kyle Zeng <kylebot@openai.com>
Closes: https://lore.kernel.org/all/20261030042138.73739-1-kylebot@openai.com/
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Jiayuan Chen posted "[PATCH net v1] tls: fix ring wrap in
sk_msg_memcopy_from_iter() after splice"
(https://lore.kernel.org/all/20261120225720.89044-1-jiayuan.chen@linux.dev/),
which sets the same cursor in the splice path. The two patches touch
different functions and apply in either order, and this one is still
needed even if his goes in: his closes the splice route on mainline,
but not tls_sw_do_sendpage() on 6.1, 5.15 and 5.10, where it does not
apply, nor the record split on 7.1 and older. This patch removes the
copy's dependence on all of them, and applies unchanged to every
stable branch, because tls_clone_plaintext_msg() is the same everywhere.
I have an exploit that lets an unprivileged process overwrite the page
cache of /bin/su to open a root shell. I confirmed it works on mainline
and 5 distros. It is available to the maintainers on request.
On 6.1, 5.15 and 5.10 the bug is a race, but an unprivileged user
won it within a second in my tests. I have another reproducer
available for those.
I used a fuzzer with a page cache oracle to confirm this patch fixes
the issue in QEMU guests on mainline 7e1392a42ea2, and all stable branches:
7.3.5, 7.1.13, 6.18.64, 6.12.118, 6.6.162, 6.1.192, 5.15.225, and 5.10.274.
I also confirmed that it fixes the original bug on a kernel
without c5a595000e26 applied.
net/tls/tls_sw.c | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c
index 226e0f176fdc..efff7a02aef2 100644
--- a/net/tls/tls_sw.c
+++ b/net/tls/tls_sw.c
@@ -349,6 +349,17 @@ static int tls_clone_plaintext_msg(struct sock *sk, int required)
*/
skip = prot->prepend_size + msg_pl->sg.size;
+ if (msg_pl->sg.size) {
+ u32 i = msg_pl->sg.end;
+
+ sk_msg_iter_var_prev(i);
+ msg_pl->sg.curr = i;
+ msg_pl->sg.copybreak = msg_pl->sg.data[i].length;
+ } else {
+ msg_pl->sg.curr = msg_pl->sg.start;
+ msg_pl->sg.copybreak = 0;
+ }
+
return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
}
base-commit: af6737b9066832a2017c281159950ba92385ce00
--
2.53.0
reply other threads:[~2026-11-23 6:38 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123063842.64536-1-juanagusnicco@gmail.com \
--to=juanagusnicco@gmail.com \
--cc=ast@kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddsterxyz@gmail.com \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=jiayuan.chen@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=kylebot@openai.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sd@queasysnail.net \
--cc=willemb@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox