Skip to contentSkip to conversation

tls: fix ring wrap in sk_msg_memcopy_from_iter() after splice

PATCH In review Jiayuan Chen 3 participants 3 messages Nov 20 – Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tls: fix ring wrap in sk_msg_memcopy_from_iter() after splice

tls_sw_sendmsg_splice() forces sg.curr = sg.end after adding the spliced page.

The following non-splice sk_msg_memcopy_from_iter() is a do-while
loop: it advances i with sk_msg_iter_var_next() and only then tests i != sg.end, so starting from sg.curr == sg.end it wraps the ring.

Fixes c5a595000e26 ("net: tls, update curr on splice as well")
---
 net/tls/tls_sw.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/tls/tls_sw.c +3 -1
index 365c9485bb7c..d3a9b1c41de8 100644
@@ -764,8 +764,10 @@ static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg,
 		}
 
 		sk_msg_page_add(msg_pl, page, part, off);
-		msg_pl->sg.copybreak = 0;
+		/* keep sg.curr on the last frag so a later copy won't wrap */
 		msg_pl->sg.curr = msg_pl->sg.end;
+		sk_msg_iter_var_prev(msg_pl->sg.curr);
+		msg_pl->sg.copybreak = sk_msg_elem(msg_pl, msg_pl->sg.curr)->length;
 		sk_mem_charge(sk, part);
 		*copied += part;
 		try_to_copy -= part;
Signature · 1 line
2.43.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Jiayuan Chen
Quoted 6 lines · Jiayuan Chen

tls_sw_sendmsg_splice() forces sg.curr = sg.end after adding the spliced page.

The following non-splice sk_msg_memcopy_from_iter() is a do-while
loop: it advances i with sk_msg_iter_var_next() and only then tests i != sg.end, so starting from sg.curr == sg.end it wraps the ring.

I don't think this addresses the root cause. The copy path in tls_sw_sendmsg_locked() starts writing at whatever cursor the last producer left behind, and this is the second time the splice line has been fixed because of that: c5a595000e26 made it move sg.curr because the copy was writing into the spliced page, and that change is what introduced this bug.

This patch does close the splice route on mainline but doesn't touch multiple producers that leave the scatterlist cursor in a bad state for the next copy:

- tls_sw_do_sendpage() on 6.1, 5.15 and 5.10, where this patch does not apply. The bug is reachable there too, as a race rather than deterministically (won within a second in my tests).
- the record split in tls_push_record() on 7.1 and older.

The copy path always appends after the data already in the record, so it can set the cursor itself, in tls_clone_plaintext_msg(), 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, covers every producer, would have made c5a595000e26 unnecessary, and applies unchanged to every stable branch.

I'll send it as a separate patch that tags d829e9c4112b which caused the symptom that c5a595000e26 fixed but introduced this bug.

Best,
Agustin