Skip to content

Commit f3dec7e

Browse files
danieljordan10gregkh
authored andcommitted
net/tls: Fix flipped sign in tls_err_abort() calls
commit da353fa upstream. 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: c46234e ("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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c828115 commit f3dec7e

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

‎include/net/tls.h‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ int tls_sk_query(struct sock *sk, int optname, char __user *optval,
359359
int __user *optlen);
360360
int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
361361
unsigned int optlen);
362+
void tls_err_abort(struct sock *sk, int err);
362363

363364
int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
364365
void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
@@ -467,12 +468,6 @@ static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
467468
#endif
468469
}
469470

470-
static inline void tls_err_abort(struct sock *sk, int err)
471-
{
472-
sk->sk_err = err;
473-
sk->sk_error_report(sk);
474-
}
475-
476471
static inline bool tls_bigint_increment(unsigned char *seq, int len)
477472
{
478473
int i;
@@ -513,7 +508,7 @@ static inline void tls_advance_record_sn(struct sock *sk,
513508
struct cipher_context *ctx)
514509
{
515510
if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
516-
tls_err_abort(sk, EBADMSG);
511+
tls_err_abort(sk, -EBADMSG);
517512

518513
if (prot->version != TLS_1_3_VERSION)
519514
tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,

‎net/tls/tls_sw.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* SOFTWARE.
3636
*/
3737

38+
#include <linux/bug.h>
3839
#include <linux/sched/signal.h>
3940
#include <linux/module.h>
4041
#include <linux/splice.h>
@@ -43,6 +44,14 @@
4344
#include <net/strparser.h>
4445
#include <net/tls.h>
4546

47+
noinline void tls_err_abort(struct sock *sk, int err)
48+
{
49+
WARN_ON_ONCE(err >= 0);
50+
/* sk->sk_err should contain a positive error code. */
51+
sk->sk_err = -err;
52+
sk->sk_error_report(sk);
53+
}
54+
4655
static int __skb_nsg(struct sk_buff *skb, int offset, int len,
4756
unsigned int recursion_level)
4857
{
@@ -419,7 +428,7 @@ int tls_tx_records(struct sock *sk, int flags)
419428

420429
tx_err:
421430
if (rc < 0 && rc != -EAGAIN)
422-
tls_err_abort(sk, EBADMSG);
431+
tls_err_abort(sk, -EBADMSG);
423432

424433
return rc;
425434
}
@@ -764,7 +773,7 @@ static int tls_push_record(struct sock *sk, int flags,
764773
msg_pl->sg.size + prot->tail_size, i);
765774
if (rc < 0) {
766775
if (rc != -EINPROGRESS) {
767-
tls_err_abort(sk, EBADMSG);
776+
tls_err_abort(sk, -EBADMSG);
768777
if (split) {
769778
tls_ctx->pending_open_record_frags = true;
770779
tls_merge_open_record(sk, rec, tmp, orig_end);
@@ -1828,7 +1837,7 @@ int tls_sw_recvmsg(struct sock *sk,
18281837
err = decrypt_skb_update(sk, skb, &msg->msg_iter,
18291838
&chunk, &zc, async_capable);
18301839
if (err < 0 && err != -EINPROGRESS) {
1831-
tls_err_abort(sk, EBADMSG);
1840+
tls_err_abort(sk, -EBADMSG);
18321841
goto recv_end;
18331842
}
18341843

@@ -2008,7 +2017,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
20082017
}
20092018

20102019
if (err < 0) {
2011-
tls_err_abort(sk, EBADMSG);
2020+
tls_err_abort(sk, -EBADMSG);
20122021
goto splice_read_end;
20132022
}
20142023
ctx->decrypted = 1;

0 commit comments

Comments
 (0)