From: Daniel Jordan daniel.m.jordan@oracle.com
mainline inclusion from mainline-v5.15 commit da353fac65fede6b8b4cfe207f0d9408e3121105 category: bugfix bugzilla: https://gitee.com/src-openeuler/kernel/issues/I9RCYN CVE: CVE-2021-47496
--------------------------------
sk->sk_err appears to expect a positive value, a convention that ktls doesn't always follow and that leads to memory corruption in other code. For instance,
[kworker] tls_encrypt_done(..., err=<negative error from crypto request>) tls_err_abort(.., err) sk->sk_err = err;
[task] splice_from_pipe_feed ... tls_sw_do_sendpage if (sk->sk_err) { ret = -sk->sk_err; // ret is positive
splice_from_pipe_feed (continued) ret = actor(...) // ret is still positive and interpreted as bytes // written, resulting in underflow of buf->len and // sd->len, leading to huge buf->offset and bogus // addresses computed in later calls to actor()
Fix all tls_err_abort() callers to pass a negative error code consistently and centralize the error-prone sign flip there, throwing in a warning to catch future misuse and uninlining the function so it really does only warn once.
Cc: stable@vger.kernel.org Fixes: c46234ebb4d1e ("tls: RX path for ktls") Reported-by: syzbot+b187b77c8474f9648fae@syzkaller.appspotmail.com Signed-off-by: Daniel Jordan daniel.m.jordan@oracle.com Signed-off-by: David S. Miller davem@davemloft.net Conflicts: include/net/tls.h net/tls/tls_sw.c [The version does not include commit a42055e8d2c3.] Signed-off-by: Ziyang Xuan william.xuanziyang@huawei.com --- include/net/tls.h | 9 ++------- net/tls/tls_sw.c | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 10 deletions(-)
diff --git a/include/net/tls.h b/include/net/tls.h index 4457bc67f2e6..de16c863fd66 100644 --- a/include/net/tls.h +++ b/include/net/tls.h @@ -272,6 +272,7 @@ int tls_sk_query(struct sock *sk, int optname, char __user *optval, int __user *optlen); int tls_sk_attach(struct sock *sk, int optname, char __user *optval, unsigned int optlen); +void tls_err_abort(struct sock *sk, int err);
int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx); int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); @@ -365,12 +366,6 @@ static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk) #endif }
-static inline void tls_err_abort(struct sock *sk, int err) -{ - sk->sk_err = err; - sk->sk_error_report(sk); -} - static inline bool tls_bigint_increment(unsigned char *seq, int len) { int i; @@ -388,7 +383,7 @@ static inline void tls_advance_record_sn(struct sock *sk, struct cipher_context *ctx) { if (tls_bigint_increment(ctx->rec_seq, ctx->rec_seq_size)) - tls_err_abort(sk, EBADMSG); + tls_err_abort(sk, -EBADMSG); tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, ctx->iv_size); } diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 7d761244a360..b4c68786077a 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -34,6 +34,7 @@ * SOFTWARE. */
+#include <linux/bug.h> #include <linux/sched/signal.h> #include <linux/module.h> #include <crypto/aead.h> @@ -43,6 +44,14 @@
#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
+noinline void tls_err_abort(struct sock *sk, int err) +{ + WARN_ON_ONCE(err >= 0); + /* sk->sk_err should contain a positive error code. */ + sk->sk_err = -err; + sk_error_report(sk); +} + static int tls_do_decryption(struct sock *sk, struct scatterlist *sgin, struct scatterlist *sgout, @@ -244,7 +253,7 @@ static int tls_push_record(struct sock *sk, int flags, /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */ rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags); if (rc < 0 && rc != -EAGAIN) - tls_err_abort(sk, EBADMSG); + tls_err_abort(sk, -EBADMSG);
tls_advance_record_sn(sk, &tls_ctx->tx); out_req: @@ -911,7 +920,7 @@ int tls_sw_recvmsg(struct sock *sk, err = decrypt_skb_update(sk, skb, &msg->msg_iter, &chunk, &zc); if (err < 0) { - tls_err_abort(sk, EBADMSG); + tls_err_abort(sk, -EBADMSG); goto recv_end; } ctx->decrypted = true; @@ -991,7 +1000,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc);
if (err < 0) { - tls_err_abort(sk, EBADMSG); + tls_err_abort(sk, -EBADMSG); goto splice_read_end; } ctx->decrypted = true;