From nobody Sat May 14 00:09:59 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 9AF531AE330C; Sat, 14 May 2022 00:10:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4L0Qr8188Hz4Sgv; Sat, 14 May 2022 00:09:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652487000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1GQDbo6RMD9SGbOf13F0QSVgOv7oj03pGlLrt8JXAvU=; b=ylasmAwztgyMi/el57h/2g6p6YCA7j2yqG8FVUN9pkQyt+1p83aaF6t2/H4EvqouAQa7+1 MhFHLdgrdl/N14zNpAdii1V6TQcPaAAqEWlhluHW17qoSHX6PjThBR/tx3VmVEf/yQFLVu HdK/BlQLm6Gt4y3bwOLTO3OaHHYZDYMbkYK9luZNL+WJdYCoMnls23HwAHZ+gKO5WLFgcz eRjRGItJLW27z65Bw9trdqLL/AQ6/abjvtbdXPP38N+HwYYLpSBwPlAbMEIfOR9A6WZ63S J4tVvi0qggp8oHtssqe/gS29lLke0zS8YFOL2edt3Ik3dgF/ewFnmZdO2uNOBw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 916714B32; Sat, 14 May 2022 00:09:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 24E09xF7073737; Sat, 14 May 2022 00:09:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 24E09xYr073736; Sat, 14 May 2022 00:09:59 GMT (envelope-from git) Date: Sat, 14 May 2022 00:09:59 GMT Message-Id: <202205140009.24E09xYr073736@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 9550224b16bb - stable/13 - OpenSSL: KTLS: Check for unprocessed receive records in ktls_configure_crypto. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 9550224b16bbe7c04004d9c40a6d54c65effbfc1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1652487000; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=1GQDbo6RMD9SGbOf13F0QSVgOv7oj03pGlLrt8JXAvU=; b=HqnUe1Y7Qa22oC2l+XR6h4BtZV2C/MXwuB8XOMZSGf0VT6fYc6fNkHVsIhQpVz3o/UuO06 cSH/dyfYVn1nZodp2c/X/XB0lWhrx/4maTuKmnZBXsJ8z7UxXNAIfzzVs7vyYq+yk3JdPA G3Cn8z+u/0qboXmUhruGegFzLlhdHBGKt7qRq5oPLCceq18jNGUD/Q8Jmo7yo99rEOg3wQ UIrFWp5vRI0DVSZ06ytAqFqqqjgnxJDh1U/wpAp6VCDim8TlKTYs4apAtlIWdFF0wTiXF2 y6mXn2O1tDiAvC3WxOmdLOqVh6Ce+abyPswRiSU3SVUTgs5HID5ZmEteqcy39Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1652487000; a=rsa-sha256; cv=none; b=rxLGfhKpQGZxzepWF5V1gDZEE7aP7YB53uccHbKa/3Sn+lpD71c5+J6hi3nxbFj4Z+4cL8 zMcAWb32dcRDbAcjSWJDXcOT8tGXqSWeXnh3WSZ6l6PEE1DUVcIw0T/hZS6NGX6NkE+LA/ 0qjqupGg65KEoFx+/+euEtWh8e+yNwsg86SdfdFjd88iTsxFdCvHK2cK3qpiBcxTGiE5hH /Cjxyc0tWQlvPEm1yh4DzMdQSjxmJa+Q9xh1QdqOeHvRgyylDhdHZB61S2iYS92gRZcMfu ZXMZeaYmJL/oLHxMP3XGPiB34UVFNtrbr6tfmt1mAIrQAJMAIMPJywVnMOlnCg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=9550224b16bbe7c04004d9c40a6d54c65effbfc1 commit 9550224b16bbe7c04004d9c40a6d54c65effbfc1 Author: John Baldwin AuthorDate: 2022-05-04 20:08:03 +0000 Commit: John Baldwin CommitDate: 2022-05-13 23:50:51 +0000 OpenSSL: KTLS: Check for unprocessed receive records in ktls_configure_crypto. KTLS implementations currently assume that the start of the in-kernel socket buffer is aligned with the start of a TLS record for the receive side. The socket option to enable KTLS specifies the TLS sequence number of this initial record. When read ahead is enabled, data can be pending in the SSL read buffer after negotiating session keys. This pending data must be examined to ensurs that the kernel's socket buffer does not contain a partial TLS record as well as to determine the correct sequence number of the first TLS record to be processed by the kernel. In preparation for enabling receive kernel offload for TLS 1.3, move the existing logic to handle read ahead from t1_enc.c into ktls.c and invoke it from ktls_configure_crypto(). Approved by: jkim Obtained from: OpenSSL commit 85773128d0e80cd8dcc772a6931d385b8cf4acd1 MFC after: 1 week Sponsored by: Netflix Differential Revision: https://reviews.freebsd.org/D34973 (cherry picked from commit 019cff03b32ce1fbf91dbf84f0805662cd04d459) --- crypto/openssl/ssl/ktls.c | 100 ++++++++++++++++++++++++++++++++++------- crypto/openssl/ssl/ssl_local.h | 4 +- crypto/openssl/ssl/t1_enc.c | 65 ++------------------------- crypto/openssl/ssl/tls13_enc.c | 3 +- 4 files changed, 91 insertions(+), 81 deletions(-) diff --git a/crypto/openssl/ssl/ktls.c b/crypto/openssl/ssl/ktls.c index c7a440b79bd2..13635232863e 100644 --- a/crypto/openssl/ssl/ktls.c +++ b/crypto/openssl/ssl/ktls.c @@ -10,6 +10,67 @@ #include "ssl_local.h" #include "internal/ktls.h" +#ifndef OPENSSL_NO_KTLS_RX + /* + * Count the number of records that were not processed yet from record boundary. + * + * This function assumes that there are only fully formed records read in the + * record layer. If read_ahead is enabled, then this might be false and this + * function will fail. + */ +static int count_unprocessed_records(SSL *s) +{ + SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer); + PACKET pkt, subpkt; + int count = 0; + + if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left)) + return -1; + + while (PACKET_remaining(&pkt) > 0) { + /* Skip record type and version */ + if (!PACKET_forward(&pkt, 3)) + return -1; + + /* Read until next record */ + if (!PACKET_get_length_prefixed_2(&pkt, &subpkt)) + return -1; + + count += 1; + } + + return count; +} + +/* + * The kernel cannot offload receive if a partial TLS record has been read. + * Check the read buffer for unprocessed records. If the buffer contains a + * partial record, fail and return 0. Otherwise, update the sequence + * number at *rec_seq for the count of unprocessed records and return 1. + */ +static int check_rx_read_ahead(SSL *s, unsigned char *rec_seq) +{ + int bit, count_unprocessed; + + count_unprocessed = count_unprocessed_records(s); + if (count_unprocessed < 0) + return 0; + + /* increment the crypto_info record sequence */ + while (count_unprocessed) { + for (bit = 7; bit >= 0; bit--) { /* increment */ + ++rec_seq[bit]; + if (rec_seq[bit] != 0) + break; + } + count_unprocessed--; + + } + + return 1; +} +#endif + #if defined(__FreeBSD__) # include @@ -59,9 +120,9 @@ int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c, } /* Function to configure kernel TLS structure */ -int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, +int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, void *rl_sequence, ktls_crypto_info_t *crypto_info, - unsigned char **rec_seq, unsigned char *iv, + int is_tx, unsigned char *iv, unsigned char *key, unsigned char *mac_key, size_t mac_secret_size) { @@ -111,11 +172,11 @@ int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, crypto_info->tls_vminor = (s->version & 0x000000ff); # ifdef TCP_RXTLS_ENABLE memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq)); - if (rec_seq != NULL) - *rec_seq = crypto_info->rec_seq; + if (!is_tx && !check_rx_read_ahead(s, crypto_info->rec_seq)) + return 0; # else - if (rec_seq != NULL) - *rec_seq = NULL; + if (!is_tx) + return 0; # endif return 1; }; @@ -163,15 +224,20 @@ int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c, } /* Function to configure kernel TLS structure */ -int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, +int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, void *rl_sequence, ktls_crypto_info_t *crypto_info, - unsigned char **rec_seq, unsigned char *iv, + int is_tx, unsigned char *iv, unsigned char *key, unsigned char *mac_key, size_t mac_secret_size) { unsigned char geniv[12]; unsigned char *iiv = iv; +# ifdef OPENSSL_NO_KTLS_RX + if (!is_tx) + return 0; +# endif + if (s->version == TLS1_2_VERSION && EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) { EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GET_IV, @@ -194,8 +260,8 @@ int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_key_length(c)); memcpy(crypto_info->gcm128.rec_seq, rl_sequence, TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE); - if (rec_seq != NULL) - *rec_seq = crypto_info->gcm128.rec_seq; + if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm128.rec_seq)) + return 0; return 1; # endif # ifdef OPENSSL_KTLS_AES_GCM_256 @@ -209,8 +275,8 @@ int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_key_length(c)); memcpy(crypto_info->gcm256.rec_seq, rl_sequence, TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE); - if (rec_seq != NULL) - *rec_seq = crypto_info->gcm256.rec_seq; + if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm256.rec_seq)) + return 0; return 1; # endif # ifdef OPENSSL_KTLS_AES_CCM_128 @@ -224,8 +290,8 @@ int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_key_length(c)); memcpy(crypto_info->ccm128.rec_seq, rl_sequence, TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE); - if (rec_seq != NULL) - *rec_seq = crypto_info->ccm128.rec_seq; + if (!is_tx && !check_rx_read_ahead(s, crypto_info->ccm128.rec_seq)) + return 0; return 1; # endif # ifdef OPENSSL_KTLS_CHACHA20_POLY1305 @@ -238,8 +304,10 @@ int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, memcpy(crypto_info->chacha20poly1305.key, key, EVP_CIPHER_key_length(c)); memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence, TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE); - if (rec_seq != NULL) - *rec_seq = crypto_info->chacha20poly1305.rec_seq; + if (!is_tx + && !check_rx_read_ahead(s, + crypto_info->chacha20poly1305.rec_seq)) + return 0; return 1; # endif default: diff --git a/crypto/openssl/ssl/ssl_local.h b/crypto/openssl/ssl/ssl_local.h index 3c4bf726bc73..587064cc1800 100644 --- a/crypto/openssl/ssl/ssl_local.h +++ b/crypto/openssl/ssl/ssl_local.h @@ -2623,9 +2623,9 @@ __owur int ssl_log_secret(SSL *ssl, const char *label, /* ktls.c */ int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c, const EVP_CIPHER_CTX *dd); -int ktls_configure_crypto(const SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, +int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd, void *rl_sequence, ktls_crypto_info_t *crypto_info, - unsigned char **rec_seq, unsigned char *iv, + int is_tx, unsigned char *iv, unsigned char *key, unsigned char *mac_key, size_t mac_secret_size); # endif diff --git a/crypto/openssl/ssl/t1_enc.c b/crypto/openssl/ssl/t1_enc.c index 422dde31262a..46191908aba8 100644 --- a/crypto/openssl/ssl/t1_enc.c +++ b/crypto/openssl/ssl/t1_enc.c @@ -82,41 +82,6 @@ static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num) return ret; } -#ifndef OPENSSL_NO_KTLS - /* - * Count the number of records that were not processed yet from record boundary. - * - * This function assumes that there are only fully formed records read in the - * record layer. If read_ahead is enabled, then this might be false and this - * function will fail. - */ -# ifndef OPENSSL_NO_KTLS_RX -static int count_unprocessed_records(SSL *s) -{ - SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer); - PACKET pkt, subpkt; - int count = 0; - - if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left)) - return -1; - - while (PACKET_remaining(&pkt) > 0) { - /* Skip record type and version */ - if (!PACKET_forward(&pkt, 3)) - return -1; - - /* Read until next record */ - if (PACKET_get_length_prefixed_2(&pkt, &subpkt)) - return -1; - - count += 1; - } - - return count; -} -# endif -#endif - int tls1_change_cipher_state(SSL *s, int which) { unsigned char *p, *mac_secret; @@ -135,12 +100,7 @@ int tls1_change_cipher_state(SSL *s, int which) int reuse_dd = 0; #ifndef OPENSSL_NO_KTLS ktls_crypto_info_t crypto_info; - unsigned char *rec_seq; void *rl_sequence; -# ifndef OPENSSL_NO_KTLS_RX - int count_unprocessed; - int bit; -# endif BIO *bio; #endif @@ -403,30 +363,11 @@ int tls1_change_cipher_state(SSL *s, int which) else rl_sequence = RECORD_LAYER_get_read_sequence(&s->rlayer); - if (!ktls_configure_crypto(s, c, dd, rl_sequence, &crypto_info, &rec_seq, - iv, key, ms, *mac_secret_size)) + if (!ktls_configure_crypto(s, c, dd, rl_sequence, &crypto_info, + which & SSL3_CC_WRITE, iv, key, ms, + *mac_secret_size)) goto skip_ktls; - if (which & SSL3_CC_READ) { -# ifndef OPENSSL_NO_KTLS_RX - count_unprocessed = count_unprocessed_records(s); - if (count_unprocessed < 0) - goto skip_ktls; - - /* increment the crypto_info record sequence */ - while (count_unprocessed) { - for (bit = 7; bit >= 0; bit--) { /* increment */ - ++rec_seq[bit]; - if (rec_seq[bit] != 0) - break; - } - count_unprocessed--; - } -# else - goto skip_ktls; -# endif - } - /* ktls works with user provided buffers directly */ if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) { if (which & SSL3_CC_WRITE) diff --git a/crypto/openssl/ssl/tls13_enc.c b/crypto/openssl/ssl/tls13_enc.c index 39530237d897..cc1435ec55c2 100644 --- a/crypto/openssl/ssl/tls13_enc.c +++ b/crypto/openssl/ssl/tls13_enc.c @@ -756,7 +756,8 @@ int tls13_change_cipher_state(SSL *s, int which) /* configure kernel crypto structure */ if (!ktls_configure_crypto(s, cipher, ciph_ctx, RECORD_LAYER_get_write_sequence(&s->rlayer), - &crypto_info, NULL, iv, key, NULL, 0)) + &crypto_info, which & SSL3_CC_WRITE, iv, key, + NULL, 0)) goto skip_ktls; /* ktls works with user provided buffers directly */