Skip to content

Commit f322136

Browse files
committed
net: tls: avoid hanging tasks on the tx_lock
syzbot sent a hung task report and Eric explains that adversarial receiver may keep RWIN at 0 for a long time, so we are not guaranteed to make forward progress. Thread which took tx_lock and went to sleep may not release tx_lock for hours. Use interruptible sleep where possible and reschedule the work if it can't take the lock. Testing: existing selftest passes Reported-by: [email protected] Fixes: 79ffe60 ("net/tls: add a TX lock") Link: https://lore.kernel.org/all/[email protected]/ Cc: [email protected] # wait 4 weeks Reviewed-by: Eric Dumazet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 49c47cc commit f322136

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

net/tls/tls_sw.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,9 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
956956
MSG_CMSG_COMPAT))
957957
return -EOPNOTSUPP;
958958

959-
mutex_lock(&tls_ctx->tx_lock);
959+
ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
960+
if (ret)
961+
return ret;
960962
lock_sock(sk);
961963

962964
if (unlikely(msg->msg_controllen)) {
@@ -1290,7 +1292,9 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
12901292
MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
12911293
return -EOPNOTSUPP;
12921294

1293-
mutex_lock(&tls_ctx->tx_lock);
1295+
ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
1296+
if (ret)
1297+
return ret;
12941298
lock_sock(sk);
12951299
ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
12961300
release_sock(sk);
@@ -2435,11 +2439,19 @@ static void tx_work_handler(struct work_struct *work)
24352439

24362440
if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
24372441
return;
2438-
mutex_lock(&tls_ctx->tx_lock);
2439-
lock_sock(sk);
2440-
tls_tx_records(sk, -1);
2441-
release_sock(sk);
2442-
mutex_unlock(&tls_ctx->tx_lock);
2442+
2443+
if (mutex_trylock(&tls_ctx->tx_lock)) {
2444+
lock_sock(sk);
2445+
tls_tx_records(sk, -1);
2446+
release_sock(sk);
2447+
mutex_unlock(&tls_ctx->tx_lock);
2448+
} else if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
2449+
/* Someone is holding the tx_lock, they will likely run Tx
2450+
* and cancel the work on their way out of the lock section.
2451+
* Schedule a long delay just in case.
2452+
*/
2453+
schedule_delayed_work(&ctx->tx_work.work, msecs_to_jiffies(10));
2454+
}
24432455
}
24442456

24452457
static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)

0 commit comments

Comments
 (0)