tls fixes for SPLICE with more hint
Subsystems (5)
John Fastabend
maintainercover tls fixes for SPLICE with more hint
Syzbot found a splat where it tried to splice data over a tls socket with the more hint and sending greater than the number of frags that fit in a msg scatterlist. This resulted in an error where we do not correctly send the data when the msg sg is full. The more flag being just a hint not a strict contract. This then results in the syzbot warning on the next send.
Edward generated an initial patch for this which checked for a full msg on entry to the sendmsg hook. This fixed the WARNING, but didn't fully resolve the issue because the full msg_pl scatterlist was never sent resulting in a stuck socket. In this series instead avoid the situation by forcing the send on the splice that fills the scatterlist.
Also in original thread I mentioned it didn't seem to be enough to simply fix the send on full sg problem. That was incorrect and was really a bug in my test program that was hanging the test program. I had setup a repair socket and wasn't handling it correctly so my tester got stuck.
Thanks. Please review. Fix in patch 1 and test in patch 2.
John Fastabend (2):
net: tls, fix WARNIING in __sk_msg_free
net: tls, add test to capture error on large splice
net/tls/tls_sw.c | 6 +++++- tools/testing/selftests/net/tls.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-)
Signature · 1 line
2.33.0
John Fastabend
maintainerPATCH 1/2 net: tls, fix WARNIING in __sk_msg_free
A splice with MSG_SPLICE_PAGES will cause tls code to use the tls_sw_sendmsg_splice path in the TLS sendmsg code to move the user provided pages from the msg into the msg_pl. This will loop over the msg until msg_pl is full, checked by sk_msg_full(msg_pl). The user can also set the MORE flag to hint stack to delay sending until receiving more pages and ideally a full buffer.
If the user adds more pages to the msg than can fit in the msg_pl scatterlist (MAX_MSG_FRAGS) we should ignore the MORE flag and send the buffer anyways.
What actually happens though is we abort the msg to msg_pl scatterlist setup and then because we forget to set 'full record' indicating we can no longer consume data without a send we fallthrough to the 'continue' path which will check if msg_data_left(msg) has more bytes to send and then attempts to fit them in the already full msg_pl. Then next iteration of sender doing send will encounter a full msg_pl and throw the warning in the syzbot report.
To fix simply check if we have a full_record in splice code path and if not send the msg regardless of MORE flag.
--- net/tls/tls_sw.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
net/tls/tls_sw.c +5 -1
@@ -1052,7 +1052,11 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,if (ret <0 ) goto send_end; tls_ctx->pending_open_record_frags = true; -if (full_record || eor ||sk_msg_full (msg_pl)) + +if (sk_msg_full (msg_pl)) + full_record = true; + +if (full_record || eor) goto copied;continue ; }
Signature · 1 line
2.33.0
John Fastabend
maintainerPATCH 2/2 net: tls, add test to capture error on large splice
In reply to John Fastabendsyzbot found an error with how splice() is handled with a msg greater than 32. This was fixed in previous patch, but lets add a test for it to ensure it continues to work.
--- tools/testing/selftests/net/tls.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
tools/testing/selftests/net/tls.c +13 -0
@@ -707,6 +707,19 @@ TEST_F(tls, splice_from_pipe)EXPECT_EQ (memcmp (mem_send, mem_recv, send_len),0 ); } +TEST_F(tls, splice_more) +{ +int send_len =TLS_PAYLOAD_MAX_LEN ; +char mem_send[TLS_PAYLOAD_MAX_LEN ]; +int i, send_pipe =1 ; +int p[2 ]; + +ASSERT_GE (pipe (p),0 ); +EXPECT_GE (write (p[1 ], mem_send, send_len),0 ); +for (i =0 ; i <32 ; i++) +EXPECT_EQ (splice (p[0 ],NULL , self->fd,NULL , send_pipe,0xe ),1 ); +} + TEST_F(tls, splice_from_pipe2) {int send_len =16000 ;
Signature · 1 line
2.33.0
Re: [PATCH net 1/2] net: tls, fix WARNIING in __sk_msg_free
In reply to John FastabendQuoted 19 lines · On Wed, 10 Jan 2024 14:01:23 -0800 John Fastabend wrote:
A splice with
MSG_SPLICE_PAGESwill cause tls code to use thetls_sw_sendmsg_splicepath in the TLS sendmsg code to move the user provided pages from the msg into themsg_pl. This will loop over the msg untilmsg_plis full, checked bysk_msg_full(msg_pl). The user can also set the MORE flag to hint stack to delay sending until receiving more pages and ideally a full buffer.If the user adds more pages to the msg than can fit in the
msg_plscatterlist (MAX_MSG_FRAGS) we should ignore the MORE flag and send the buffer anyways.What actually happens though is we abort the msg to
msg_plscatterlist setup and then because we forget to set 'full record' indicating we can no longer consume data without a send we fallthrough to the 'continue' path which will check ifmsg_data_left(msg)has more bytes to send and then attempts to fit them in the already fullmsg_pl. Then next iteration of sender doing send will encounter a fullmsg_pland throw the warning in the syzbot report.
Quoted 2 lines
To fix simply check if we have a
full_recordin splice code path and if not send the msg regardless of MORE flag.
Jakub Kicinski
maintainerRe: [PATCH net 2/2] net: tls, add test to capture error on large splice
In reply to John Fastabendtools/testing/selftests/net/tls.c TEST_F(tls, splice_from_pipe) · line 720
Quoted 1 line · On Wed, 10 Jan 2024 14:01:24 -0800 John Fastabend wrote:
tools/testing/selftests/net/tls.c +1
-0+EXPECT_EQ (splice (p[0 ],NULL , self->fd,NULL , send_pipe,0xe ),1 );
Any reason to use 0xe rather than the SPLICE_F_* defines for flags?
John Fastabend
maintainerRe: [PATCH net 2/2] net: tls, add test to capture error on large splice
Quoted 4 lines · Jakub Kicinski
tools/testing/selftests/net/tls.c TEST_F(tls, splice_from_pipe) · line 720
Quoted 1 line · On Wed, 10 Jan 2024 14:01:24 -0800 John Fastabend wrote:
tools/testing/selftests/net/tls.c +1
-0+EXPECT_EQ (splice (p[0 ],NULL , self->fd,NULL , send_pipe,0xe ),1 );Any reason to use
0xerather than the SPLICE_F_* defines for flags?
No reason I crafted the test first and then forgot to come back and tidy it much.
I'll send a v2 with readable names.