Skip to content

Commit df720d2

Browse files
dhowellskuba-moo
authored andcommitted
tls/sw: Use splice_eof() to flush
Allow splice to end a TLS record after prematurely ending a splice/sendfile due to getting an EOF condition (->splice_read() returned 0) after splice had called TLS with a sendmsg() with MSG_MORE set when the user didn't set MSG_MORE. Suggested-by: Linus Torvalds <[email protected]> Link: https://lore.kernel.org/r/CAHk-=wh=V579PDYvkpnTobCLGczbgxpMgGmmhqiTyE34Cpi5Gg@mail.gmail.com/ Signed-off-by: David Howells <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> cc: Chuck Lever <[email protected]> cc: Boris Pismenny <[email protected]> cc: John Fastabend <[email protected]> cc: Jens Axboe <[email protected]> cc: Matthew Wilcox <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 2bfc668 commit df720d2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

net/tls/tls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void tls_update_rx_zc_capable(struct tls_context *tls_ctx);
9797
void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
9898
void tls_sw_strparser_done(struct tls_context *tls_ctx);
9999
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
100+
void tls_sw_splice_eof(struct socket *sock);
100101
int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
101102
int offset, size_t size, int flags);
102103
int tls_sw_sendpage(struct sock *sk, struct page *page,

net/tls/tls_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,7 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG]
957957
ops[TLS_BASE][TLS_BASE] = *base;
958958

959959
ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
960+
ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
960961
ops[TLS_SW ][TLS_BASE].sendpage_locked = tls_sw_sendpage_locked;
961962

962963
ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
@@ -1027,6 +1028,7 @@ static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
10271028

10281029
prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
10291030
prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
1031+
prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
10301032
prot[TLS_SW][TLS_BASE].sendpage = tls_sw_sendpage;
10311033

10321034
prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];

net/tls/tls_sw.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,80 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
11571157
return copied > 0 ? copied : ret;
11581158
}
11591159

1160+
/*
1161+
* Handle unexpected EOF during splice without SPLICE_F_MORE set.
1162+
*/
1163+
void tls_sw_splice_eof(struct socket *sock)
1164+
{
1165+
struct sock *sk = sock->sk;
1166+
struct tls_context *tls_ctx = tls_get_ctx(sk);
1167+
struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1168+
struct tls_rec *rec;
1169+
struct sk_msg *msg_pl;
1170+
ssize_t copied = 0;
1171+
bool retrying = false;
1172+
int ret = 0;
1173+
int pending;
1174+
1175+
if (!ctx->open_rec)
1176+
return;
1177+
1178+
mutex_lock(&tls_ctx->tx_lock);
1179+
lock_sock(sk);
1180+
1181+
retry:
1182+
rec = ctx->open_rec;
1183+
if (!rec)
1184+
goto unlock;
1185+
1186+
msg_pl = &rec->msg_plaintext;
1187+
1188+
/* Check the BPF advisor and perform transmission. */
1189+
ret = bpf_exec_tx_verdict(msg_pl, sk, false, TLS_RECORD_TYPE_DATA,
1190+
&copied, 0);
1191+
switch (ret) {
1192+
case 0:
1193+
case -EAGAIN:
1194+
if (retrying)
1195+
goto unlock;
1196+
retrying = true;
1197+
goto retry;
1198+
case -EINPROGRESS:
1199+
break;
1200+
default:
1201+
goto unlock;
1202+
}
1203+
1204+
/* Wait for pending encryptions to get completed */
1205+
spin_lock_bh(&ctx->encrypt_compl_lock);
1206+
ctx->async_notify = true;
1207+
1208+
pending = atomic_read(&ctx->encrypt_pending);
1209+
spin_unlock_bh(&ctx->encrypt_compl_lock);
1210+
if (pending)
1211+
crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1212+
else
1213+
reinit_completion(&ctx->async_wait.completion);
1214+
1215+
/* There can be no concurrent accesses, since we have no pending
1216+
* encrypt operations
1217+
*/
1218+
WRITE_ONCE(ctx->async_notify, false);
1219+
1220+
if (ctx->async_wait.err)
1221+
goto unlock;
1222+
1223+
/* Transmit if any encryptions have completed */
1224+
if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1225+
cancel_delayed_work(&ctx->tx_work.work);
1226+
tls_tx_records(sk, 0);
1227+
}
1228+
1229+
unlock:
1230+
release_sock(sk);
1231+
mutex_unlock(&tls_ctx->tx_lock);
1232+
}
1233+
11601234
static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
11611235
int offset, size_t size, int flags)
11621236
{

0 commit comments

Comments
 (0)