tls: Move cipher info to a separate struct
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
RFC PATCH 1/5 tls: Generalize zerocopy_from_iter
Refactor zerocopy_from_iter to take arguments for pages and size, such that it can be used for both tx and rx. RX will also support zerocopy direct to output iter, as long as the full message can be copied at once (a large enough userspace buffer was provided).
--- net/tls/tls_sw.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-)
net/tls/tls_sw.c +19 -12
@@ -281,23 +281,24 @@ static int tls_sw_push_pending_record(struct sock *sk, int flags) }static int zerocopy_from_iter (struct sock *sk,struct iov_iter *from, -int length) +int length,int *pages_used, +unsigned int *size_used, +struct scatterlist *to,int to_max_pages, +bool charge) { -struct tls_context *tls_ctx =tls_get_ctx (sk); -struct tls_sw_context *ctx =tls_sw_ctx (tls_ctx);struct page *pages[MAX_SKB_FRAGS ];size_t offset;ssize_t copied, use;int i =0 ; -unsigned int size = ctx->sg_plaintext_size; -int num_elem = ctx->sg_plaintext_num_elem; +unsigned int size = *size_used; +int num_elem = *pages_used;int rc =0 ;int maxpages;while (length >0 ) { i =0 ; - maxpages =ARRAY_SIZE (ctx->sg_plaintext_data) - num_elem; + maxpages = to_max_pages - num_elem;if (maxpages ==0 ) { rc = -EFAULT ; goto out;
@@ -317,10 +318,11 @@ static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from,while (copied) { use =min_t (int, copied,PAGE_SIZE - offset); -sg_set_page (&ctx->sg_plaintext_data[num_elem], +sg_set_page (&to[num_elem], pages[i], use, offset); -sg_unmark_end (&ctx->sg_plaintext_data[num_elem]); -sk_mem_charge (sk, use); +sg_unmark_end (&to[num_elem]); +if (charge) +sk_mem_charge (sk, use); offset =0 ; copied -= use;
@@ -331,8 +333,9 @@ static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from, } out: - ctx->sg_plaintext_size = size; - ctx->sg_plaintext_num_elem = num_elem; + *size_used = size; + *pages_used = num_elem; +return rc; }
@@ -429,7 +432,11 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)if (full_record || eor) { ret =zerocopy_from_iter (sk, &msg->msg_iter, - try_to_copy); + try_to_copy, &ctx->sg_plaintext_num_elem, + &ctx->sg_plaintext_size, + ctx->sg_plaintext_data, +ARRAY_SIZE (ctx->sg_plaintext_data), + true);if (ret) goto fallback_to_reg_send;
Signature · 1 line
2.9.5
RFC PATCH 2/5 tls: Move cipher info to a separate struct
In reply to a message not in the archiveSeparate tx crypto parameters to a separate cipher_context struct. The same parameters will be used for rx using the same struct.
tls_advance_record_sn is modified to only take the cipher info.
--- include/net/tls.h | 26 +++++++++++++----------- net/tls/tls_main.c | 8 ++++---- net/tls/tls_sw.c | 58 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 49 insertions(+), 43 deletions(-)
include/net/tls.h +15 -11
@@ -81,6 +81,16 @@ enum {TLS_PENDING_CLOSED_RECORD }; +struct cipher_context { +u16 prepend_size; +u16 tag_size; +u16 overhead_size; +u16 iv_size; +char *iv; +u16 rec_seq_size; +char *rec_seq; +}; +struct tls_context {union {struct tls_crypto_info crypto_send;
@@ -91,13 +101,7 @@ struct tls_context { u8 tx_conf:2; -u16 prepend_size; -u16 tag_size; -u16 overhead_size; -u16 iv_size; -char *iv; -u16 rec_seq_size; -char *rec_seq; +struct cipher_context tx;struct scatterlist *partially_sent_record;u16 partially_sent_offset;
@@ -190,7 +194,7 @@ static inline bool tls_bigint_increment(unsigned char *seq, int len) }static inline void tls_advance_record_sn (struct sock *sk, -struct tls_context *ctx) +struct cipher_context *ctx) {if (tls_bigint_increment (ctx->rec_seq, ctx->rec_seq_size))tls_err_abort (sk);
@@ -203,9 +207,9 @@ static inline void tls_fill_prepend(struct tls_context *ctx,size_t plaintext_len, unsigned char record_type) { - size_t pkt_len, iv_size = ctx->iv_size; + size_t pkt_len, iv_size = ctx->tx.iv_size; - pkt_len = plaintext_len + iv_size + ctx->tag_size; + pkt_len = plaintext_len + iv_size + ctx->tx.tag_size;/* we cover nonce explicit here as well, so buf should be of * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
@@ -217,7 +221,7 @@ static inline void tls_fill_prepend(struct tls_context *ctx, buf[3 ] = pkt_len >>8 ; buf[4 ] = pkt_len &0xFF ;memcpy (buf +TLS_NONCE_OFFSET , - ctx->iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , iv_size); + ctx->tx.iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , iv_size); } static inline void tls_make_aad(char *buf,
net/tls/tls_main.c +4 -4
@@ -259,8 +259,8 @@ static void tls_sk_proto_close(struct sock *sk, long timeout) } } -kfree (ctx->rec_seq); -kfree (ctx->iv); +kfree (ctx->tx.rec_seq); +kfree (ctx->tx.iv);if (ctx->tx_conf ==TLS_SW_TX )tls_sw_free_tx_resources (sk);
@@ -319,9 +319,9 @@ static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval, }lock_sock (sk);memcpy (crypto_info_aes_gcm_128->iv, - ctx->iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , + ctx->tx.iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE ,TLS_CIPHER_AES_GCM_128_IV_SIZE ); -memcpy (crypto_info_aes_gcm_128->rec_seq, ctx->rec_seq, +memcpy (crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE );release_sock (sk); if (copy_to_user(optval,
net/tls/tls_sw.c +30 -28
135 lines not on this page · raw message
Signature · 1 line
2.9.5
RFC PATCH 3/5 tls: Pass error code explicitly to tls_err_abort
In reply to a message not in the archivePass EBADMSG explicitly to tls_err_abort. Receive path will pass additional codes - E2BIG if framing is larger than max TLS record size.
--- include/net/tls.h | 6 +++--- net/tls/tls_sw.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-)
include/net/tls.h +3 -3
@@ -174,9 +174,9 @@ static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)return tls_ctx->pending_open_record_frags; } -static inline void tls_err_abort (struct sock *sk) +static inline void tls_err_abort (struct sock *sk,int err) { - sk->sk_err =EBADMSG ; + sk->sk_err = err; sk->sk_error_report (sk); }
@@ -197,7 +197,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); +tls_err_abort (sk,EBADMSG );tls_bigint_increment (ctx->iv +TLS_CIPHER_AES_GCM_128_SALT_SIZE , ctx->iv_size); }
net/tls/tls_sw.c +1 -1
@@ -269,7 +269,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); +tls_err_abort (sk,EBADMSG );tls_advance_record_sn (sk, &tls_ctx->tx);return rc;
Signature · 1 line
2.9.5
RFC PATCH 4/5 tls: RX path for ktls
In reply to a message not in the archiveAdd rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit. If no cmsg buffer is passed, then only application data records will be passed to userspace, and EIO is returned for other types of alerts.
EBADMSG is passed for decryption errors, and E2BIG is passed for framing errors. Both are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise sk_cow_data is called (similar to ipsec), and buffers are decrypted in place and copied. splice_read always decrypts in place, since no buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is received. Otherwise we wait for strparser to finish reading a full frame. Actual decryption is only done during recvmsg or splice_read calls.
--- include/net/tls.h | 27 ++- include/uapi/linux/tls.h | 2 + net/tls/Kconfig | 1 + net/tls/tls_main.c | 62 ++++- net/tls/tls_sw.c | 574 ++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 596 insertions(+), 70 deletions(-)
include/net/tls.h +25 -2
@@ -40,6 +40,7 @@#include <linux/socket.h> #include <linux/tcp.h> #include <net/tcp.h> +#include <net/strparser.h> #include <uapi/linux/tls.h>
@@ -58,8 +59,18 @@struct tls_sw_context {struct crypto_aead *aead_send; +struct crypto_aead *aead_recv;struct crypto_wait async_wait; +/* Receive context */ +struct strparser strp; +void (*saved_data_ready)(struct sock *sk); +unsigned int (*sk_poll)(struct file *file,struct socket *sock, +struct poll_table_struct *wait); +struct sk_buff *recv_pkt; +u8 control; +bool decrypted; +/* Sending context */ char aad_space[TLS_AAD_SPACE_SIZE ];
@@ -96,12 +107,17 @@ struct tls_context {struct tls_crypto_info crypto_send;struct tls12_crypto_info_aes_gcm_128 crypto_send_aes_gcm_128; }; +union { +struct tls_crypto_info crypto_recv; +struct tls12_crypto_info_aes_gcm_128 crypto_recv_aes_gcm_128; + };void *priv_ctx; u8 tx_conf:2;struct cipher_context tx; +struct cipher_context rx;struct scatterlist *partially_sent_record;u16 partially_sent_offset;
@@ -128,12 +144,19 @@ int tls_sk_attach(struct sock *sk, int optname, char __user *optval, unsigned int optlen); -int tls_set_sw_offload (struct sock *sk,struct tls_context *ctx); +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);int tls_sw_sendpage (struct sock *sk,struct page *page,int offset,size_t size,int flags);void tls_sw_close (struct sock *sk,long timeout); -void tls_sw_free_tx_resources (struct sock *sk); +void tls_sw_free_resources (struct sock *sk); +int tls_sw_recvmsg (struct sock *sk,struct msghdr *msg,size_t len, +int nonblock,int flags,int *addr_len); +unsigned int tls_sw_poll (struct file *file,struct socket *sock, +struct poll_table_struct *wait); +ssize_t tls_sw_splice_read (struct socket *sock,loff_t *ppos, +struct pipe_inode_info *pipe, +size_t len,unsigned int flags);void tls_sk_destruct (struct sock *sk,struct tls_context *ctx);void tls_icsk_clean_acked (struct sock *sk);
include/uapi/linux/tls.h +2 -0
@@ -38,6 +38,7 @@/* TLS socket options */ #define TLS_TX 1/* Set transmit parameters */ +#define TLS_RX 2/* Set receive parameters */ /* Supported versions */ #define TLS_VERSION_MINOR (ver) ((ver) & 0xFF)
@@ -59,6 +60,7 @@#define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE 8#define TLS_SET_RECORD_TYPE 1 +#define TLS_GET_RECORD_TYPE 2struct tls_crypto_info {__u16 version;
net/tls/Kconfig +1 -0
@@ -7,6 +7,7 @@ config TLS select CRYPTO select CRYPTO_AES select CRYPTO_GCM + select STREAM_PARSER default n ---help--- Enable kernel support for TLS protocol. This allows symmetric
net/tls/tls_main.c +52 -10
123 lines not on this page · raw message
net/tls/tls_sw.c +516 -58
643 lines not on this page · raw message
Signature · 1 line
2.9.5
Re: [PATCH RFC 4/5] tls: RX path for ktls
Hi Dave,
Quoted 8 lines · Dave Watson
Add rx path for tls software implementation.
recvmsg,
splice_read, and poll implemented.An additional sockopt
TLS_RXis added, with the same interface asTLS_TX. EitherTLX_RXorTLX_TXmay be provided separately, or together (with two different setsockopt calls with appropriate keys).
Quoted 7 lines
Control messages are passed via CMSG in a similar way to transmit. If no cmsg buffer is passed, then only application data records will be passed to userspace, and EIO is returned for other types of alerts.
EBADMSG is passed for decryption errors, and E2BIG is passed for framing errors. Both are unrecoverable.
I think E2BIG is for too long argument list. EMSGSIZE might be more appropriate. Also, we must check that the record is not too short (cipher specific). For TLS1.2 with AES-GCM the minimum length is 8 (IV) + 16 (TAG). The correct error for this case is EBADMSG, like a decryption failure.
Also, how about bad TLS version (e.g. not TLS1.2)?
A separate error type is required for bad version, because it triggers a unique alert in libraries such as OpenSSL. I thought of using EINVAL for bad version. What do you think?
I wonder if we should provide a more flexible method of obtaining errors for the future. Maybe use a special CMSG for errors?
This CMSG will be triggered only after the socket enters the error state.
Quoted 8 lines
....
net/tls/tls_sw.c +7
-0+ +int tls_sw_recvmsg (struct sock *sk, +struct msghdr *msg, +size_t len, +int nonblock, +int flags, +int *addr_len)
net/tls/tls_sw.c int tls_sw_sendpage(struct sock *sk, struct page *page, · line 813
Quoted 10 lines
net/tls/tls_sw.c +10
-0+{ +struct tls_context *tls_ctx =tls_get_ctx (sk); +struct tls_sw_context *ctx =tls_sw_ctx (tls_ctx); +unsigned char control; +struct strp_msg *rxm; +struct sk_buff *skb; +ssize_t copied =0 ; +bool cmsg = false; +int err =0 ; +long timeo;
Maybe try to read from the error queue here?
Quoted 95 lines
net/tls/tls_sw.c +95
-0+ + flags |= nonblock; + +lock_sock (sk); + + timeo =sock_rcvtimeo (sk, flags &MSG_DONTWAIT ); + do { + bool zc = false; + int chunk = 0; + + skb = tls_wait_data(sk, flags, timeo, &err); + if (!skb) + goto recv_end; + + rxm = strp_msg(skb); + if (!cmsg) { + int cerr; + + cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, + sizeof(ctx->control), &ctx->control); + cmsg = true; + control = ctx->control; + if (ctx->control != TLS_RECORD_TYPE_DATA) { + if (cerr || msg->msg_flags & MSG_CTRUNC) { + err = -EIO; + goto recv_end; + } + } + } else if (control != ctx->control) { + goto recv_end; + } + + if (!ctx->decrypted) { + int page_count; + int to_copy; + + page_count = iov_iter_npages(&msg->msg_iter, + MAX_SKB_FRAGS); + to_copy = rxm->full_len - tls_ctx->rx.overhead_size; + if (to_copy <= len && page_count < MAX_SKB_FRAGS && + likely(!(flags & MSG_PEEK))) { + struct scatterlist sgin[MAX_SKB_FRAGS + 1]; + char unused[21]; + int pages = 0; + + zc = true; + sg_init_table(sgin, MAX_SKB_FRAGS + 1); + sg_set_buf(&sgin[0], unused, 13); + + err = zerocopy_from_iter(sk, &msg->msg_iter, + to_copy, &pages, + &chunk, &sgin[1], + MAX_SKB_FRAGS, false); + if (err < 0) + goto fallback_to_reg_recv; + + err = decrypt_skb(sk, skb, sgin); + for (; pages > 0; pages--) + put_page(sg_page(&sgin[pages])); + if (err < 0) { + tls_err_abort(sk, EBADMSG); + goto recv_end; + } + } else { +fallback_to_reg_recv: + err = decrypt_skb(sk, skb, NULL); + if (err < 0) { + tls_err_abort(sk, EBADMSG); + goto recv_end; + } + } + ctx->decrypted = true; + } + + if (!zc) { + chunk = min_t(unsigned int, rxm->full_len, len); + err = skb_copy_datagram_msg(skb, rxm->offset, msg, + chunk); + if (err < 0) + goto recv_end; + } + + copied += chunk; + len -= chunk; + if (likely(!(flags & MSG_PEEK))) { + u8 control = ctx->control; + + if (tls_sw_advance_skb(sk, skb, chunk)) { + /* Return full control message to + * userspace before trying to parse + * another message type + */ + msg->msg_flags |= MSG_EOR; + if (control != TLS_RECORD_TYPE_DATA) + goto recv_end;
net/tls/tls_sw.c int tls_sw_sendpage(struct sock *sk, struct page *page, · line 917
Quoted 10 lines
net/tls/tls_sw.c +9
-0+ } + } + }while (len); + +recv_end: +release_sock (sk); +return copied ? : err; +} +
Best,
Boris.
Re: [PATCH RFC 4/5] tls: RX path for ktls
Quoted 16 lines · Boris Pismenny
Hi Dave,
Quoted 13 lines · Dave Watson
Add rx path for tls software implementation.
recvmsg,
splice_read, and poll implemented.An additional sockopt
TLS_RXis added, with the same interface asTLS_TX. EitherTLX_RXorTLX_TXmay be provided separately, or together (with two different setsockopt calls with appropriate keys).Control messages are passed via CMSG in a similar way to transmit. If no cmsg buffer is passed, then only application data records will be passed to userspace, and EIO is returned for other types of alerts.
Quoted 5 lines
Quoted 2 lines
EBADMSG is passed for decryption errors, and E2BIG is passed for framing errors. Both are unrecoverable.
I think E2BIG is for too long argument list. EMSGSIZE might be more appropriate.
Sounds good.
Quoted 8 lines
Also, we must check that the record is not too short (cipher specific). For TLS1.2 with AES-GCM the minimum length is 8 (IV) + 16 (TAG). The correct error for this case is EBADMSG, like a decryption failure.
Also, how about bad TLS version (e.g. not TLS1.2)?
A separate error type is required for bad version, because it triggers a unique alert in libraries such as OpenSSL.
I thought of using EINVAL for bad version. What do you think?
Ah, I did not realize there was a separate alert for that, sounds good.
Quoted 5 lines
I wonder if we should provide a more flexible method of obtaining errors for the future.
Maybe use a special CMSG for errors?
This CMSG will be triggered only after the socket enters the error state.
I'm not opposed to this in principle, but without a concrete use am hesitant to add it. I don't know of any other error codes that could be returned besides the ones discussed above.
Quoted 11 lines
Quoted 11 lines
....
net/tls/tls_sw.c +10
-0+ +int tls_sw_recvmsg (struct sock *sk, +struct msghdr *msg, +size_t len, +int nonblock, +int flags, +int *addr_len) +{ +struct tls_context *tls_ctx =tls_get_ctx (sk); +struct tls_sw_context *ctx =tls_sw_ctx (tls_ctx);
Quoted 8 lines
net/tls/tls_sw.c int tls_sw_sendpage(struct sock *sk, struct page *page, · line 813
Quoted 7 lines
net/tls/tls_sw.c +7
-0+unsigned char control; +struct strp_msg *rxm; +struct sk_buff *skb; +ssize_t copied =0 ; +bool cmsg = false; +int err =0 ; +long timeo;Maybe try to read from the error queue here?
Sure.
RFC PATCH 5/5 tls: Add receive path documentation
In reply to a message not in the archiveAdd documentation on rx path setup and cmsg interface.
--- Documentation/networking/tls.txt | 59 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-)
Documentation/networking/tls.txt +57 -2
@@ -48,6 +48,9 @@ the transmit and the receive into the kernel. setsockopt(sock, SOL_TLS, TLS_TX, &crypto_info, sizeof(crypto_info)); +Transmit and receive are set separately, but the setup is the same, using either +TLS_TX or TLS_RX. + Sending TLS application data ----------------------------
@@ -79,6 +82,21 @@ for memory), or the encryption will always succeed. If send() returns -ENOMEM and some data was left on the socket buffer from a previous call using MSG_MORE, the MSG_MORE data is left on the socket buffer. +Receiving TLS application data +------------------------------ + +After setting the TLS_RX socket option, all recv family socket calls +are decrypted using TLS parameters provided. A full TLS record must +be received before decryption can happen. + + char buffer[16384]; + recv(sock, buffer, 16384); + +Received data is decrypted directly in to the user buffer if it is +large enough, and no additional allocations occur. If the userspace +buffer is too small, data is decrypted in the kernel and copied to +userspace. + Send TLS control messages -------------------------
@@ -118,6 +136,43 @@ using a record of type @record_type. Control message data should be provided unencrypted, and will be encrypted by the kernel. +Receiving TLS control messages +------------------------------ + +TLS control messages are passed in the userspace buffer, with message +type passed via cmsg. If no cmsg buffer is provided, an error is +returned if a control message is received. Data messages may be +received without a cmsg buffer set. + + char buffer[16384]; + char cmsg[CMSG_SPACE(sizeof(unsigned char))]; + struct msghdr msg = {0}; + msg.msg_control = cmsg; + msg.msg_controllen = sizeof(cmsg); + + struct iovec msg_iov; + msg_iov.iov_base = buffer; + msg_iov.iov_len = 16384; + + msg.msg_iov = &msg_iov; + msg.msg_iovlen = 1; + + int ret = recvmsg(sock, &msg, 0 /* flags */); + + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); + if (cmsg->cmsg_level == SOL_TLS && + cmsg->cmsg_type == TLS_GET_RECORD_TYPE) { + int record_type = *((unsigned char *)CMSG_DATA(cmsg)); + // Do something with record_type, and control message data in + // buffer. + // + // Note that record_type may be == to application data (23). + } else { + // Buffer contains application data. + } + +recv will never return data from mixed types of TLS records. + Integrating in to userspace TLS library ---------------------------------------
@@ -126,10 +181,10 @@ layer of a userspace TLS library. A patchset to OpenSSL to use ktls as the record layer is here: -https://github.com/Mellanox/tls-openssl +https://github.com/Mellanox/openssl/commits/tls_rx An example of calling send directly after a handshake using gnutls. Since it doesn't implement a full record layer, control messages are not supported: -https://github.com/Mellanox/tls-af_ktls_tool +https://github.com/ktls/af_ktls-tool/commits/RX
Signature · 1 line
2.9.5