Skip to content

Commit 4cbc325

Browse files
kuba-moodavem330
authored andcommitted
tls: rx: allow only one reader at a time
recvmsg() in TLS gets data from the skb list (rx_list) or fresh skbs we read from TCP via strparser. The former holds skbs which were already decrypted for peek or decrypted and partially consumed. tls_wait_data() only notices appearance of fresh skbs coming out of TCP (or psock). It is possible, if there is a concurrent call to peek() and recv() that the peek() will move the data from input to rx_list without recv() noticing. recv() will then read data out of order or never wake up. This is not a practical use case/concern, but it makes the self tests less reliable. This patch solves the problem by allowing only one reader in. Because having multiple processes calling read()/peek() is not normal avoid adding a lock and try to fast-path the single reader case. Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 3898f52 commit 4cbc325

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

include/net/tls.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,14 @@ struct tls_sw_context_rx {
116116
void (*saved_data_ready)(struct sock *sk);
117117

118118
struct sk_buff *recv_pkt;
119+
u8 reader_present;
119120
u8 async_capable:1;
120121
u8 zc_capable:1;
122+
u8 reader_contended:1;
121123
atomic_t decrypt_pending;
122124
/* protect crypto_wait with decrypt_pending*/
123125
spinlock_t decrypt_compl_lock;
126+
struct wait_queue_head wq;
124127
};
125128

126129
struct tls_record_info {

net/tls/tls_sw.c

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,51 @@ tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
17531753
sk_flush_backlog(sk);
17541754
}
17551755

1756+
static long tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx,
1757+
bool nonblock)
1758+
{
1759+
long timeo;
1760+
1761+
lock_sock(sk);
1762+
1763+
timeo = sock_rcvtimeo(sk, nonblock);
1764+
1765+
while (unlikely(ctx->reader_present)) {
1766+
DEFINE_WAIT_FUNC(wait, woken_wake_function);
1767+
1768+
ctx->reader_contended = 1;
1769+
1770+
add_wait_queue(&ctx->wq, &wait);
1771+
sk_wait_event(sk, &timeo,
1772+
!READ_ONCE(ctx->reader_present), &wait);
1773+
remove_wait_queue(&ctx->wq, &wait);
1774+
1775+
if (!timeo)
1776+
return -EAGAIN;
1777+
if (signal_pending(current))
1778+
return sock_intr_errno(timeo);
1779+
}
1780+
1781+
WRITE_ONCE(ctx->reader_present, 1);
1782+
1783+
return timeo;
1784+
}
1785+
1786+
static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx)
1787+
{
1788+
if (unlikely(ctx->reader_contended)) {
1789+
if (wq_has_sleeper(&ctx->wq))
1790+
wake_up(&ctx->wq);
1791+
else
1792+
ctx->reader_contended = 0;
1793+
1794+
WARN_ON_ONCE(!ctx->reader_present);
1795+
}
1796+
1797+
WRITE_ONCE(ctx->reader_present, 0);
1798+
release_sock(sk);
1799+
}
1800+
17561801
int tls_sw_recvmsg(struct sock *sk,
17571802
struct msghdr *msg,
17581803
size_t len,
@@ -1782,7 +1827,9 @@ int tls_sw_recvmsg(struct sock *sk,
17821827
return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
17831828

17841829
psock = sk_psock_get(sk);
1785-
lock_sock(sk);
1830+
timeo = tls_rx_reader_lock(sk, ctx, flags & MSG_DONTWAIT);
1831+
if (timeo < 0)
1832+
return timeo;
17861833
bpf_strp_enabled = sk_psock_strp_enabled(psock);
17871834

17881835
/* If crypto failed the connection is broken */
@@ -1801,7 +1848,6 @@ int tls_sw_recvmsg(struct sock *sk,
18011848

18021849
target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
18031850
len = len - copied;
1804-
timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
18051851

18061852
zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
18071853
ctx->zc_capable;
@@ -1956,7 +2002,7 @@ int tls_sw_recvmsg(struct sock *sk,
19562002
copied += decrypted;
19572003

19582004
end:
1959-
release_sock(sk);
2005+
tls_rx_reader_unlock(sk, ctx);
19602006
if (psock)
19612007
sk_psock_put(sk, psock);
19622008
return copied ? : err;
@@ -1978,9 +2024,9 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
19782024
long timeo;
19792025
int chunk;
19802026

1981-
lock_sock(sk);
1982-
1983-
timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK);
2027+
timeo = tls_rx_reader_lock(sk, ctx, flags & SPLICE_F_NONBLOCK);
2028+
if (timeo < 0)
2029+
return timeo;
19842030

19852031
from_queue = !skb_queue_empty(&ctx->rx_list);
19862032
if (from_queue) {
@@ -2029,7 +2075,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
20292075
}
20302076

20312077
splice_read_end:
2032-
release_sock(sk);
2078+
tls_rx_reader_unlock(sk, ctx);
20332079
return copied ? : err;
20342080
}
20352081

@@ -2371,6 +2417,7 @@ int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
23712417
} else {
23722418
crypto_init_wait(&sw_ctx_rx->async_wait);
23732419
spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
2420+
init_waitqueue_head(&sw_ctx_rx->wq);
23742421
crypto_info = &ctx->crypto_recv.info;
23752422
cctx = &ctx->rx;
23762423
skb_queue_head_init(&sw_ctx_rx->rx_list);

0 commit comments

Comments
 (0)