From: Quanye Yang kTLS sits on the same struct sock as TCP. do_recvmmsg() and getsockopt(SO_ERROR) still call sock_error() without the socket lock and clear sk_err with xchg(). tls_rx_rec_wait() already peeks when data has been copied and consumes otherwise, but the outer if (sk_err) is an unmarked load. On the no-data path that check-then-sock_error() window can return 0 after another thread consumes the error. Call sock_error() once and only return when it is non-zero; keep READ_ONCE() on the peek path. tls_sw_sendmsg_locked(), tls_push_data(), bpf_exec_tx_verdict() and tls_encrypt_done() read sk_err more than once. Fold those unmarked loads into one READ_ONCE() and use that value as the returned errno. The field is still not consumed there. Link: https://lore.kernel.org/netdev/3d9d442f-f168-43da-87b0-010ad5a78365@redhat.com/ Signed-off-by: Quanye Yang --- net/tls/tls_device.c | 5 +++-- net/tls/tls_sw.c | 51 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c index f11d0528fc43..03ce83a9d4e9 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -444,8 +444,9 @@ static int tls_push_data(struct sock *sk, if ((flags & (MSG_MORE | MSG_EOR)) == (MSG_MORE | MSG_EOR)) return -EINVAL; - if (unlikely(sk->sk_err)) - return -sk->sk_err; + rc = -READ_ONCE(sk->sk_err); + if (unlikely(rc)) + return rc; flags |= MSG_SENDPAGE_DECRYPTED; tls_push_record_flags = flags | MSG_MORE; diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 312e51270f29..4221b2ad3c1d 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -473,6 +473,7 @@ static void tls_encrypt_done(void *data, int err) struct scatterlist *sge; struct sk_msg *msg_en; struct sock *sk; + int skerr; if (err == -EINPROGRESS) /* see the comment in tls_decrypt_done() */ return; @@ -488,13 +489,15 @@ static void tls_encrypt_done(void *data, int err) sge->offset -= prot->prepend_size; sge->length += prot->prepend_size; + skerr = READ_ONCE(sk->sk_err); + /* Check if error is previously set on socket */ - if (err || sk->sk_err) { + if (err || skerr) { rec = NULL; /* If err is already set on socket, return the same code */ - if (sk->sk_err) { - ctx->async_wait.err = -sk->sk_err; + if (skerr) { + ctx->async_wait.err = -skerr; } else { ctx->async_wait.err = err; tls_err_abort(sk, err); @@ -701,13 +704,17 @@ static int tls_push_record(struct sock *sk, int flags, static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk, u8 record_type, ssize_t *copied, int flags) { + int skerr; int err; err = tls_push_record(sk, flags, record_type); - if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) { - *copied -= sk_msg_free(sk, msg); - tls_free_open_rec(sk); - err = -sk->sk_err; + if (err && err != -EINPROGRESS) { + skerr = READ_ONCE(sk->sk_err); + if (skerr == EBADMSG) { + *copied -= sk_msg_free(sk, msg); + tls_free_open_rec(sk); + err = -skerr; + } } return err; } @@ -800,10 +807,9 @@ static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, } while (msg_data_left(msg)) { - if (sk->sk_err) { - ret = -sk->sk_err; + ret = -READ_ONCE(sk->sk_err); + if (ret) goto send_end; - } if (ctx->open_rec) rec = ctx->open_rec; @@ -1098,6 +1104,7 @@ tls_rx_rec_wait(struct sock *sk, bool nonblock, bool released, bool has_copied) struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); DEFINE_WAIT_FUNC(wait, woken_wake_function); int ret = 0; + int skerr; long timeo; /* a rekey is pending, let userspace deal with it */ @@ -1107,10 +1114,14 @@ tls_rx_rec_wait(struct sock *sk, bool nonblock, bool released, bool has_copied) timeo = sock_rcvtimeo(sk, nonblock); while (!tls_strp_msg_ready(ctx)) { - if (sk->sk_err) { - if (has_copied) - return -READ_ONCE(sk->sk_err); - return sock_error(sk); + if (has_copied) { + skerr = READ_ONCE(sk->sk_err); + if (skerr) + return -skerr; + } else { + skerr = sock_error(sk); + if (skerr) + return skerr; } if (ret < 0) @@ -1132,10 +1143,14 @@ tls_rx_rec_wait(struct sock *sk, bool nonblock, bool released, bool has_copied) * sk_err here so a connection abort surfaces as the * actual error rather than a clean EOF. */ - if (sk->sk_err) { - if (has_copied) - return -READ_ONCE(sk->sk_err); - return sock_error(sk); + if (has_copied) { + skerr = READ_ONCE(sk->sk_err); + if (skerr) + return -skerr; + } else { + skerr = sock_error(sk); + if (skerr) + return skerr; } if (sk->sk_shutdown & RCV_SHUTDOWN) return 0; -- 2.55.0 From: Quanye Yang do_recvmmsg() and getsockopt(SO_ERROR) clear sk_err with xchg() without the socket lock. TCP, MPTCP and kTLS already peek the same field with READ_ONCE() or consume it via sock_error(). sock_dequeue_err_skb() still uses plain stores. tcp_recvmsg() can call it via MSG_ERRQUEUE before lock_sock(), so those writes race with the annotated readers and with sock_error(). The same unmarked stores exist in strp_abort_strp() and sk_psock_report_error(), which run on the TCP/TLS socket. Annotate those writers with WRITE_ONCE(). No extra ordering is needed; this does not change who wins when ICMP error-queue entries overwrite sk_err. Link: https://lore.kernel.org/netdev/3d9d442f-f168-43da-87b0-010ad5a78365@redhat.com/ Signed-off-by: Quanye Yang Reviewed-by: Eric Dumazet --- include/linux/skmsg.h | 2 +- net/core/skbuff.c | 5 +++-- net/strparser/strparser.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index d5e35f24738d..52ce45f25f5a 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -429,7 +429,7 @@ static inline void sk_psock_report_error(struct sk_psock *psock, int err) { struct sock *sk = psock->sk; - sk->sk_err = err; + WRITE_ONCE(sk->sk_err, err); sk_error_report(sk); } diff --git a/net/core/skbuff.c b/net/core/skbuff.c index d56f4f0102f7..a6c9cf57bc3b 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -5539,12 +5539,13 @@ struct sk_buff *sock_dequeue_err_skb(struct sock *sk) if (skb && (skb_next = skb_peek(q))) { icmp_next = is_icmp_err_skb(skb_next); if (icmp_next) - sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; + WRITE_ONCE(sk->sk_err, + SKB_EXT_ERR(skb_next)->ee.ee_errno); } spin_unlock_irqrestore(&q->lock, flags); if (is_icmp_err_skb(skb) && !icmp_next) - sk->sk_err = 0; + WRITE_ONCE(sk->sk_err, 0); if (skb_next) sk_error_report(sk); diff --git a/net/strparser/strparser.c b/net/strparser/strparser.c index 9c1559d7923f..5f1da32b152f 100644 --- a/net/strparser/strparser.c +++ b/net/strparser/strparser.c @@ -56,7 +56,7 @@ static void strp_abort_strp(struct strparser *strp, int err) strp->need_bytes = 0; /* Report an error on the lower socket */ - sk->sk_err = -err; + WRITE_ONCE(sk->sk_err, -err); sk_error_report(sk); } -- 2.55.0