Re: [PATCH net-next 5/6] tls: RX path for ktls

2018-03-21 Thread Dave Watson
On 03/21/18 07:20 AM, Boris Pismenny wrote:
> 
> 
> On 3/20/2018 7:54 PM, Dave Watson wrote:
> > +   ctx->control = header[0];
> > +
> > +   data_len = ((header[4] & 0xFF) | (header[3] << 8));
> > +
> > +   cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
> > +
> > +   if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
> > +   ret = -EMSGSIZE;
> > +   goto read_failure;
> > +   }
> > +   if (data_len < cipher_overhead) {
> > +   ret = -EMSGSIZE;
> 
> I think this should be considered EBADMSG, because this error is cipher
> dependent. At least, that's what happens within OpenSSL. Also, EMSGSIZE is
> usually used only for too long messages.

Ah, indeed.  Thanks, will send v2.


Re: [PATCH net-next 5/6] tls: RX path for ktls

2018-03-20 Thread Boris Pismenny



On 3/20/2018 7:54 PM, Dave Watson wrote:

Add 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 EMSGSIZE is passed for
framing errors (either framing too big *or* too small with crypto
overhead). EINVAL is returned for TLS versions that do not match the
original setsockopt call.  All 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.

Signed-off-by: Dave Watson 
---

...

+
+static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
+{
+   struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
+   struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
+   char header[tls_ctx->rx.prepend_size];
+   struct strp_msg *rxm = strp_msg(skb);
+   size_t cipher_overhead;
+   size_t data_len = 0;
+   int ret;
+
+   /* Verify that we have a full TLS header, or wait for more data */
+   if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
+   return 0;
+
+   /* Linearize header to local buffer */
+   ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
+
+   if (ret < 0)
+   goto read_failure;
+
+   ctx->control = header[0];
+
+   data_len = ((header[4] & 0xFF) | (header[3] << 8));
+
+   cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
+
+   if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
+   ret = -EMSGSIZE;
+   goto read_failure;
+   }
+   if (data_len < cipher_overhead) {
+   ret = -EMSGSIZE;


I think this should be considered EBADMSG, because this error is cipher 
dependent. At least, that's what happens within OpenSSL. Also, EMSGSIZE 
is usually used only for too long messages.



+   goto read_failure;
+   }
+
+   if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.version) ||
+   header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.version)) {
+   ret = -EINVAL;
+   goto read_failure;
+   }
+
+   return data_len + TLS_HEADER_SIZE;
+
+read_failure:
+   tls_err_abort(strp->sk, ret);
+
+   return ret;
+}
+

...


[PATCH net-next 5/6] tls: RX path for ktls

2018-03-20 Thread Dave Watson
Add 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 EMSGSIZE is passed for
framing errors (either framing too big *or* too small with crypto
overhead). EINVAL is returned for TLS versions that do not match the
original setsockopt call.  All 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.

Signed-off-by: Dave Watson 
---
 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 | 587 ++-
 5 files changed, 609 insertions(+), 70 deletions(-)

diff --git a/include/net/tls.h b/include/net/tls.h
index 095b722..437a746 100644
--- a/include/net/tls.h
+++ b/include/net/tls.h
@@ -40,6 +40,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 
@@ -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 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);
diff --git a/include/uapi/linux/tls.h b/include/uapi/linux/tls.h
index 293b2cd..c6633e9 100644
--- a/include/uapi/linux/tls.h
+++ b/include/uapi/linux/tls.h
@@ -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_SIZE8
 
 #define TLS_SET_RECORD_TYPE1
+#define TLS_GET_RECORD_TYPE2
 
 struct tls_crypto_info {
__u16 version;
diff --git a/net/tls/Kconfig b/net/tls/Kconfig
index eb58303..89b8745a 100644
--- a/net/tls/Kconfig
+++ b/net/tls/Kconfig
@@ -7,6 +7,7 @@ config TLS
select CRYPTO
select CRYPTO_AES
select CRYPTO_GCM
+   select STREAM_PARSER
default n
---help---