tls: set the copy cursor before cloning plaintext
Subsystems (3)
PATCH 1/1 tls: set the copy cursor before cloning plaintext
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.
---
Jiayuan Chen posted "[PATCH net v1] tls: fix ring wrap in sk_msg_memcopy_from_iter() after splice"
(https://lore.kernel.org/all/[email protected]/),
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(+)
net/tls/tls_sw.c +11 -0
@@ -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