Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. |
| 3 | * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. |
| 4 | * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved. |
| 5 | * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved. |
| 6 | * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved. |
| 7 | * |
| 8 | * This software is available to you under a choice of one of two |
| 9 | * licenses. You may choose to be licensed under the terms of the GNU |
| 10 | * General Public License (GPL) Version 2, available from the file |
| 11 | * COPYING in the main directory of this source tree, or the |
| 12 | * OpenIB.org BSD license below: |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or |
| 15 | * without modification, are permitted provided that the following |
| 16 | * conditions are met: |
| 17 | * |
| 18 | * - Redistributions of source code must retain the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer. |
| 21 | * |
| 22 | * - Redistributions in binary form must reproduce the above |
| 23 | * copyright notice, this list of conditions and the following |
| 24 | * disclaimer in the documentation and/or other materials |
| 25 | * provided with the distribution. |
| 26 | * |
| 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 31 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 32 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 33 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 | * SOFTWARE. |
| 35 | */ |
| 36 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 37 | #include <linux/sched/signal.h> |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | #include <crypto/aead.h> |
| 40 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 41 | #include <net/strparser.h> |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 42 | #include <net/tls.h> |
| 43 | |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 44 | #define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE |
| 45 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 46 | static int tls_do_decryption(struct sock *sk, |
| 47 | struct scatterlist *sgin, |
| 48 | struct scatterlist *sgout, |
| 49 | char *iv_recv, |
| 50 | size_t data_len, |
| 51 | struct sk_buff *skb, |
| 52 | gfp_t flags) |
| 53 | { |
| 54 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 55 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 56 | struct aead_request *aead_req; |
| 57 | |
| 58 | int ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 59 | |
Vakul Garg | d2bdd26 | 2018-07-11 14:32:20 +0530 | [diff] [blame] | 60 | aead_req = aead_request_alloc(ctx->aead_recv, flags); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 61 | if (!aead_req) |
| 62 | return -ENOMEM; |
| 63 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 64 | aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); |
| 65 | aead_request_set_crypt(aead_req, sgin, sgout, |
| 66 | data_len + tls_ctx->rx.tag_size, |
| 67 | (u8 *)iv_recv); |
| 68 | aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 69 | crypto_req_done, &ctx->async_wait); |
| 70 | |
| 71 | ret = crypto_wait_req(crypto_aead_decrypt(aead_req), &ctx->async_wait); |
| 72 | |
Vakul Garg | d2bdd26 | 2018-07-11 14:32:20 +0530 | [diff] [blame] | 73 | aead_request_free(aead_req); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 77 | static void trim_sg(struct sock *sk, struct scatterlist *sg, |
| 78 | int *sg_num_elem, unsigned int *sg_size, int target_size) |
| 79 | { |
| 80 | int i = *sg_num_elem - 1; |
| 81 | int trim = *sg_size - target_size; |
| 82 | |
| 83 | if (trim <= 0) { |
| 84 | WARN_ON(trim < 0); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | *sg_size = target_size; |
| 89 | while (trim >= sg[i].length) { |
| 90 | trim -= sg[i].length; |
| 91 | sk_mem_uncharge(sk, sg[i].length); |
| 92 | put_page(sg_page(&sg[i])); |
| 93 | i--; |
| 94 | |
| 95 | if (i < 0) |
| 96 | goto out; |
| 97 | } |
| 98 | |
| 99 | sg[i].length -= trim; |
| 100 | sk_mem_uncharge(sk, trim); |
| 101 | |
| 102 | out: |
| 103 | *sg_num_elem = i + 1; |
| 104 | } |
| 105 | |
| 106 | static void trim_both_sgl(struct sock *sk, int target_size) |
| 107 | { |
| 108 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 109 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 110 | |
| 111 | trim_sg(sk, ctx->sg_plaintext_data, |
| 112 | &ctx->sg_plaintext_num_elem, |
| 113 | &ctx->sg_plaintext_size, |
| 114 | target_size); |
| 115 | |
| 116 | if (target_size > 0) |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 117 | target_size += tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 118 | |
| 119 | trim_sg(sk, ctx->sg_encrypted_data, |
| 120 | &ctx->sg_encrypted_num_elem, |
| 121 | &ctx->sg_encrypted_size, |
| 122 | target_size); |
| 123 | } |
| 124 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 125 | static int alloc_encrypted_sg(struct sock *sk, int len) |
| 126 | { |
| 127 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 128 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 129 | int rc = 0; |
| 130 | |
John Fastabend | 2c3682f | 2018-03-18 12:56:49 -0700 | [diff] [blame] | 131 | rc = sk_alloc_sg(sk, len, |
John Fastabend | 8c05dbf | 2018-03-18 12:57:05 -0700 | [diff] [blame] | 132 | ctx->sg_encrypted_data, 0, |
John Fastabend | 2c3682f | 2018-03-18 12:56:49 -0700 | [diff] [blame] | 133 | &ctx->sg_encrypted_num_elem, |
| 134 | &ctx->sg_encrypted_size, 0); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 135 | |
| 136 | return rc; |
| 137 | } |
| 138 | |
| 139 | static int alloc_plaintext_sg(struct sock *sk, int len) |
| 140 | { |
| 141 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 142 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 143 | int rc = 0; |
| 144 | |
John Fastabend | 8c05dbf | 2018-03-18 12:57:05 -0700 | [diff] [blame] | 145 | rc = sk_alloc_sg(sk, len, ctx->sg_plaintext_data, 0, |
John Fastabend | 2c3682f | 2018-03-18 12:56:49 -0700 | [diff] [blame] | 146 | &ctx->sg_plaintext_num_elem, &ctx->sg_plaintext_size, |
| 147 | tls_ctx->pending_open_record_frags); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 148 | |
| 149 | return rc; |
| 150 | } |
| 151 | |
| 152 | static void free_sg(struct sock *sk, struct scatterlist *sg, |
| 153 | int *sg_num_elem, unsigned int *sg_size) |
| 154 | { |
| 155 | int i, n = *sg_num_elem; |
| 156 | |
| 157 | for (i = 0; i < n; ++i) { |
| 158 | sk_mem_uncharge(sk, sg[i].length); |
| 159 | put_page(sg_page(&sg[i])); |
| 160 | } |
| 161 | *sg_num_elem = 0; |
| 162 | *sg_size = 0; |
| 163 | } |
| 164 | |
| 165 | static void tls_free_both_sg(struct sock *sk) |
| 166 | { |
| 167 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 168 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 169 | |
| 170 | free_sg(sk, ctx->sg_encrypted_data, &ctx->sg_encrypted_num_elem, |
| 171 | &ctx->sg_encrypted_size); |
| 172 | |
| 173 | free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, |
| 174 | &ctx->sg_plaintext_size); |
| 175 | } |
| 176 | |
| 177 | static int tls_do_encryption(struct tls_context *tls_ctx, |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 178 | struct tls_sw_context_tx *ctx, |
| 179 | struct aead_request *aead_req, |
| 180 | size_t data_len) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 181 | { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 182 | int rc; |
| 183 | |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 184 | ctx->sg_encrypted_data[0].offset += tls_ctx->tx.prepend_size; |
| 185 | ctx->sg_encrypted_data[0].length -= tls_ctx->tx.prepend_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 186 | |
| 187 | aead_request_set_tfm(aead_req, ctx->aead_send); |
| 188 | aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); |
| 189 | aead_request_set_crypt(aead_req, ctx->sg_aead_in, ctx->sg_aead_out, |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 190 | data_len, tls_ctx->tx.iv); |
Vakul Garg | a54667f | 2018-01-31 21:34:37 +0530 | [diff] [blame] | 191 | |
| 192 | aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 193 | crypto_req_done, &ctx->async_wait); |
| 194 | |
| 195 | rc = crypto_wait_req(crypto_aead_encrypt(aead_req), &ctx->async_wait); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 196 | |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 197 | ctx->sg_encrypted_data[0].offset -= tls_ctx->tx.prepend_size; |
| 198 | ctx->sg_encrypted_data[0].length += tls_ctx->tx.prepend_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 199 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | static int tls_push_record(struct sock *sk, int flags, |
| 204 | unsigned char record_type) |
| 205 | { |
| 206 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 207 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 208 | struct aead_request *req; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 209 | int rc; |
| 210 | |
Vakul Garg | d2bdd26 | 2018-07-11 14:32:20 +0530 | [diff] [blame] | 211 | req = aead_request_alloc(ctx->aead_send, sk->sk_allocation); |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 212 | if (!req) |
| 213 | return -ENOMEM; |
| 214 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 215 | sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1); |
| 216 | sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1); |
| 217 | |
Ilya Lesokhin | 213ef6e | 2017-11-13 10:22:47 +0200 | [diff] [blame] | 218 | tls_make_aad(ctx->aad_space, ctx->sg_plaintext_size, |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 219 | tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 220 | record_type); |
| 221 | |
| 222 | tls_fill_prepend(tls_ctx, |
| 223 | page_address(sg_page(&ctx->sg_encrypted_data[0])) + |
| 224 | ctx->sg_encrypted_data[0].offset, |
| 225 | ctx->sg_plaintext_size, record_type); |
| 226 | |
| 227 | tls_ctx->pending_open_record_frags = 0; |
| 228 | set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags); |
| 229 | |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 230 | rc = tls_do_encryption(tls_ctx, ctx, req, ctx->sg_plaintext_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 231 | if (rc < 0) { |
| 232 | /* If we are called from write_space and |
| 233 | * we fail, we need to set this SOCK_NOSPACE |
| 234 | * to trigger another write_space in the future. |
| 235 | */ |
| 236 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 237 | goto out_req; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, |
| 241 | &ctx->sg_plaintext_size); |
| 242 | |
| 243 | ctx->sg_encrypted_num_elem = 0; |
| 244 | ctx->sg_encrypted_size = 0; |
| 245 | |
| 246 | /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */ |
| 247 | rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags); |
| 248 | if (rc < 0 && rc != -EAGAIN) |
Dave Watson | f4a8e43 | 2018-03-22 10:10:15 -0700 | [diff] [blame] | 249 | tls_err_abort(sk, EBADMSG); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 250 | |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 251 | tls_advance_record_sn(sk, &tls_ctx->tx); |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 252 | out_req: |
Vakul Garg | d2bdd26 | 2018-07-11 14:32:20 +0530 | [diff] [blame] | 253 | aead_request_free(req); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 254 | return rc; |
| 255 | } |
| 256 | |
| 257 | static int tls_sw_push_pending_record(struct sock *sk, int flags) |
| 258 | { |
| 259 | return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA); |
| 260 | } |
| 261 | |
| 262 | static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from, |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 263 | int length, int *pages_used, |
| 264 | unsigned int *size_used, |
| 265 | struct scatterlist *to, int to_max_pages, |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 266 | bool charge) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 267 | { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 268 | struct page *pages[MAX_SKB_FRAGS]; |
| 269 | |
| 270 | size_t offset; |
| 271 | ssize_t copied, use; |
| 272 | int i = 0; |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 273 | unsigned int size = *size_used; |
| 274 | int num_elem = *pages_used; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 275 | int rc = 0; |
| 276 | int maxpages; |
| 277 | |
| 278 | while (length > 0) { |
| 279 | i = 0; |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 280 | maxpages = to_max_pages - num_elem; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 281 | if (maxpages == 0) { |
| 282 | rc = -EFAULT; |
| 283 | goto out; |
| 284 | } |
| 285 | copied = iov_iter_get_pages(from, pages, |
| 286 | length, |
| 287 | maxpages, &offset); |
| 288 | if (copied <= 0) { |
| 289 | rc = -EFAULT; |
| 290 | goto out; |
| 291 | } |
| 292 | |
| 293 | iov_iter_advance(from, copied); |
| 294 | |
| 295 | length -= copied; |
| 296 | size += copied; |
| 297 | while (copied) { |
| 298 | use = min_t(int, copied, PAGE_SIZE - offset); |
| 299 | |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 300 | sg_set_page(&to[num_elem], |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 301 | pages[i], use, offset); |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 302 | sg_unmark_end(&to[num_elem]); |
| 303 | if (charge) |
| 304 | sk_mem_charge(sk, use); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 305 | |
| 306 | offset = 0; |
| 307 | copied -= use; |
| 308 | |
| 309 | ++i; |
| 310 | ++num_elem; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | out: |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 315 | if (rc) |
| 316 | iov_iter_revert(from, size - *size_used); |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 317 | *size_used = size; |
| 318 | *pages_used = num_elem; |
| 319 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 320 | return rc; |
| 321 | } |
| 322 | |
| 323 | static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, |
| 324 | int bytes) |
| 325 | { |
| 326 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 327 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 328 | struct scatterlist *sg = ctx->sg_plaintext_data; |
| 329 | int copy, i, rc = 0; |
| 330 | |
| 331 | for (i = tls_ctx->pending_open_record_frags; |
| 332 | i < ctx->sg_plaintext_num_elem; ++i) { |
| 333 | copy = sg[i].length; |
| 334 | if (copy_from_iter( |
| 335 | page_address(sg_page(&sg[i])) + sg[i].offset, |
| 336 | copy, from) != copy) { |
| 337 | rc = -EFAULT; |
| 338 | goto out; |
| 339 | } |
| 340 | bytes -= copy; |
| 341 | |
| 342 | ++tls_ctx->pending_open_record_frags; |
| 343 | |
| 344 | if (!bytes) |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | out: |
| 349 | return rc; |
| 350 | } |
| 351 | |
| 352 | int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
| 353 | { |
| 354 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 355 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 356 | int ret = 0; |
| 357 | int required_size; |
| 358 | long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 359 | bool eor = !(msg->msg_flags & MSG_MORE); |
| 360 | size_t try_to_copy, copied = 0; |
| 361 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 362 | int record_room; |
| 363 | bool full_record; |
| 364 | int orig_size; |
Doron Roberts-Kedes | 0a26cf3 | 2018-07-25 14:48:21 -0700 | [diff] [blame] | 365 | bool is_kvec = msg->msg_iter.type & ITER_KVEC; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 366 | |
| 367 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) |
| 368 | return -ENOTSUPP; |
| 369 | |
| 370 | lock_sock(sk); |
| 371 | |
| 372 | if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo)) |
| 373 | goto send_end; |
| 374 | |
| 375 | if (unlikely(msg->msg_controllen)) { |
| 376 | ret = tls_proccess_cmsg(sk, msg, &record_type); |
| 377 | if (ret) |
| 378 | goto send_end; |
| 379 | } |
| 380 | |
| 381 | while (msg_data_left(msg)) { |
| 382 | if (sk->sk_err) { |
r.hering@avm.de | 30be8f8 | 2018-01-12 15:42:06 +0100 | [diff] [blame] | 383 | ret = -sk->sk_err; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 384 | goto send_end; |
| 385 | } |
| 386 | |
| 387 | orig_size = ctx->sg_plaintext_size; |
| 388 | full_record = false; |
| 389 | try_to_copy = msg_data_left(msg); |
| 390 | record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; |
| 391 | if (try_to_copy >= record_room) { |
| 392 | try_to_copy = record_room; |
| 393 | full_record = true; |
| 394 | } |
| 395 | |
| 396 | required_size = ctx->sg_plaintext_size + try_to_copy + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 397 | tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 398 | |
| 399 | if (!sk_stream_memory_free(sk)) |
| 400 | goto wait_for_sndbuf; |
| 401 | alloc_encrypted: |
| 402 | ret = alloc_encrypted_sg(sk, required_size); |
| 403 | if (ret) { |
| 404 | if (ret != -ENOSPC) |
| 405 | goto wait_for_memory; |
| 406 | |
| 407 | /* Adjust try_to_copy according to the amount that was |
| 408 | * actually allocated. The difference is due |
| 409 | * to max sg elements limit |
| 410 | */ |
| 411 | try_to_copy -= required_size - ctx->sg_encrypted_size; |
| 412 | full_record = true; |
| 413 | } |
Doron Roberts-Kedes | 0a26cf3 | 2018-07-25 14:48:21 -0700 | [diff] [blame] | 414 | if (!is_kvec && (full_record || eor)) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 415 | ret = zerocopy_from_iter(sk, &msg->msg_iter, |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 416 | try_to_copy, &ctx->sg_plaintext_num_elem, |
| 417 | &ctx->sg_plaintext_size, |
| 418 | ctx->sg_plaintext_data, |
| 419 | ARRAY_SIZE(ctx->sg_plaintext_data), |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 420 | true); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 421 | if (ret) |
| 422 | goto fallback_to_reg_send; |
| 423 | |
| 424 | copied += try_to_copy; |
| 425 | ret = tls_push_record(sk, msg->msg_flags, record_type); |
Doron Roberts-Kedes | 5a3611e | 2018-07-26 07:59:35 -0700 | [diff] [blame] | 426 | if (ret) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 427 | goto send_end; |
Doron Roberts-Kedes | 5a3611e | 2018-07-26 07:59:35 -0700 | [diff] [blame] | 428 | continue; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 429 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 430 | fallback_to_reg_send: |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 431 | trim_sg(sk, ctx->sg_plaintext_data, |
| 432 | &ctx->sg_plaintext_num_elem, |
| 433 | &ctx->sg_plaintext_size, |
| 434 | orig_size); |
| 435 | } |
| 436 | |
| 437 | required_size = ctx->sg_plaintext_size + try_to_copy; |
| 438 | alloc_plaintext: |
| 439 | ret = alloc_plaintext_sg(sk, required_size); |
| 440 | if (ret) { |
| 441 | if (ret != -ENOSPC) |
| 442 | goto wait_for_memory; |
| 443 | |
| 444 | /* Adjust try_to_copy according to the amount that was |
| 445 | * actually allocated. The difference is due |
| 446 | * to max sg elements limit |
| 447 | */ |
| 448 | try_to_copy -= required_size - ctx->sg_plaintext_size; |
| 449 | full_record = true; |
| 450 | |
| 451 | trim_sg(sk, ctx->sg_encrypted_data, |
| 452 | &ctx->sg_encrypted_num_elem, |
| 453 | &ctx->sg_encrypted_size, |
| 454 | ctx->sg_plaintext_size + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 455 | tls_ctx->tx.overhead_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy); |
| 459 | if (ret) |
| 460 | goto trim_sgl; |
| 461 | |
| 462 | copied += try_to_copy; |
| 463 | if (full_record || eor) { |
| 464 | push_record: |
| 465 | ret = tls_push_record(sk, msg->msg_flags, record_type); |
| 466 | if (ret) { |
| 467 | if (ret == -ENOMEM) |
| 468 | goto wait_for_memory; |
| 469 | |
| 470 | goto send_end; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | continue; |
| 475 | |
| 476 | wait_for_sndbuf: |
| 477 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 478 | wait_for_memory: |
| 479 | ret = sk_stream_wait_memory(sk, &timeo); |
| 480 | if (ret) { |
| 481 | trim_sgl: |
| 482 | trim_both_sgl(sk, orig_size); |
| 483 | goto send_end; |
| 484 | } |
| 485 | |
| 486 | if (tls_is_pending_closed_record(tls_ctx)) |
| 487 | goto push_record; |
| 488 | |
| 489 | if (ctx->sg_encrypted_size < required_size) |
| 490 | goto alloc_encrypted; |
| 491 | |
| 492 | goto alloc_plaintext; |
| 493 | } |
| 494 | |
| 495 | send_end: |
| 496 | ret = sk_stream_error(sk, msg->msg_flags, ret); |
| 497 | |
| 498 | release_sock(sk); |
| 499 | return copied ? copied : ret; |
| 500 | } |
| 501 | |
| 502 | int tls_sw_sendpage(struct sock *sk, struct page *page, |
| 503 | int offset, size_t size, int flags) |
| 504 | { |
| 505 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 506 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 507 | int ret = 0; |
| 508 | long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
| 509 | bool eor; |
| 510 | size_t orig_size = size; |
| 511 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 512 | struct scatterlist *sg; |
| 513 | bool full_record; |
| 514 | int record_room; |
| 515 | |
| 516 | if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 517 | MSG_SENDPAGE_NOTLAST)) |
| 518 | return -ENOTSUPP; |
| 519 | |
| 520 | /* No MSG_EOR from splice, only look at MSG_MORE */ |
| 521 | eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST)); |
| 522 | |
| 523 | lock_sock(sk); |
| 524 | |
| 525 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 526 | |
| 527 | if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo)) |
| 528 | goto sendpage_end; |
| 529 | |
| 530 | /* Call the sk_stream functions to manage the sndbuf mem. */ |
| 531 | while (size > 0) { |
| 532 | size_t copy, required_size; |
| 533 | |
| 534 | if (sk->sk_err) { |
r.hering@avm.de | 30be8f8 | 2018-01-12 15:42:06 +0100 | [diff] [blame] | 535 | ret = -sk->sk_err; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 536 | goto sendpage_end; |
| 537 | } |
| 538 | |
| 539 | full_record = false; |
| 540 | record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; |
| 541 | copy = size; |
| 542 | if (copy >= record_room) { |
| 543 | copy = record_room; |
| 544 | full_record = true; |
| 545 | } |
| 546 | required_size = ctx->sg_plaintext_size + copy + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 547 | tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 548 | |
| 549 | if (!sk_stream_memory_free(sk)) |
| 550 | goto wait_for_sndbuf; |
| 551 | alloc_payload: |
| 552 | ret = alloc_encrypted_sg(sk, required_size); |
| 553 | if (ret) { |
| 554 | if (ret != -ENOSPC) |
| 555 | goto wait_for_memory; |
| 556 | |
| 557 | /* Adjust copy according to the amount that was |
| 558 | * actually allocated. The difference is due |
| 559 | * to max sg elements limit |
| 560 | */ |
| 561 | copy -= required_size - ctx->sg_plaintext_size; |
| 562 | full_record = true; |
| 563 | } |
| 564 | |
| 565 | get_page(page); |
| 566 | sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem; |
| 567 | sg_set_page(sg, page, copy, offset); |
Dave Watson | 7a8c4dd | 2018-01-19 12:30:13 -0800 | [diff] [blame] | 568 | sg_unmark_end(sg); |
| 569 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 570 | ctx->sg_plaintext_num_elem++; |
| 571 | |
| 572 | sk_mem_charge(sk, copy); |
| 573 | offset += copy; |
| 574 | size -= copy; |
| 575 | ctx->sg_plaintext_size += copy; |
| 576 | tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem; |
| 577 | |
| 578 | if (full_record || eor || |
| 579 | ctx->sg_plaintext_num_elem == |
| 580 | ARRAY_SIZE(ctx->sg_plaintext_data)) { |
| 581 | push_record: |
| 582 | ret = tls_push_record(sk, flags, record_type); |
| 583 | if (ret) { |
| 584 | if (ret == -ENOMEM) |
| 585 | goto wait_for_memory; |
| 586 | |
| 587 | goto sendpage_end; |
| 588 | } |
| 589 | } |
| 590 | continue; |
| 591 | wait_for_sndbuf: |
| 592 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 593 | wait_for_memory: |
| 594 | ret = sk_stream_wait_memory(sk, &timeo); |
| 595 | if (ret) { |
| 596 | trim_both_sgl(sk, ctx->sg_plaintext_size); |
| 597 | goto sendpage_end; |
| 598 | } |
| 599 | |
| 600 | if (tls_is_pending_closed_record(tls_ctx)) |
| 601 | goto push_record; |
| 602 | |
| 603 | goto alloc_payload; |
| 604 | } |
| 605 | |
| 606 | sendpage_end: |
| 607 | if (orig_size > size) |
| 608 | ret = orig_size - size; |
| 609 | else |
| 610 | ret = sk_stream_error(sk, flags, ret); |
| 611 | |
| 612 | release_sock(sk); |
| 613 | return ret; |
| 614 | } |
| 615 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 616 | static struct sk_buff *tls_wait_data(struct sock *sk, int flags, |
| 617 | long timeo, int *err) |
| 618 | { |
| 619 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 620 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 621 | struct sk_buff *skb; |
| 622 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 623 | |
| 624 | while (!(skb = ctx->recv_pkt)) { |
| 625 | if (sk->sk_err) { |
| 626 | *err = sock_error(sk); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
Doron Roberts-Kedes | fcf4793 | 2018-07-18 16:22:27 -0700 | [diff] [blame] | 630 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 631 | return NULL; |
| 632 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 633 | if (sock_flag(sk, SOCK_DONE)) |
| 634 | return NULL; |
| 635 | |
| 636 | if ((flags & MSG_DONTWAIT) || !timeo) { |
| 637 | *err = -EAGAIN; |
| 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | add_wait_queue(sk_sleep(sk), &wait); |
| 642 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 643 | sk_wait_event(sk, &timeo, ctx->recv_pkt != skb, &wait); |
| 644 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 645 | remove_wait_queue(sk_sleep(sk), &wait); |
| 646 | |
| 647 | /* Handle signals */ |
| 648 | if (signal_pending(current)) { |
| 649 | *err = sock_intr_errno(timeo); |
| 650 | return NULL; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return skb; |
| 655 | } |
| 656 | |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 657 | static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb, |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 658 | struct scatterlist *sgout, bool *zc) |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 659 | { |
| 660 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 661 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 662 | struct strp_msg *rxm = strp_msg(skb); |
| 663 | int err = 0; |
| 664 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 665 | #ifdef CONFIG_TLS_DEVICE |
| 666 | err = tls_device_decrypted(sk, skb); |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 667 | if (err < 0) |
| 668 | return err; |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 669 | #endif |
| 670 | if (!ctx->decrypted) { |
| 671 | err = decrypt_skb(sk, skb, sgout); |
| 672 | if (err < 0) |
| 673 | return err; |
| 674 | } else { |
| 675 | *zc = false; |
| 676 | } |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 677 | |
| 678 | rxm->offset += tls_ctx->rx.prepend_size; |
| 679 | rxm->full_len -= tls_ctx->rx.overhead_size; |
| 680 | tls_advance_record_sn(sk, &tls_ctx->rx); |
| 681 | ctx->decrypted = true; |
| 682 | ctx->saved_data_ready(sk); |
| 683 | |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | int decrypt_skb(struct sock *sk, struct sk_buff *skb, |
| 688 | struct scatterlist *sgout) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 689 | { |
| 690 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 691 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 692 | char iv[TLS_CIPHER_AES_GCM_128_SALT_SIZE + MAX_IV_SIZE]; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 693 | struct scatterlist sgin_arr[MAX_SKB_FRAGS + 2]; |
| 694 | struct scatterlist *sgin = &sgin_arr[0]; |
| 695 | struct strp_msg *rxm = strp_msg(skb); |
| 696 | int ret, nsg = ARRAY_SIZE(sgin_arr); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 697 | struct sk_buff *unused; |
| 698 | |
| 699 | ret = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, |
| 700 | iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 701 | tls_ctx->rx.iv_size); |
| 702 | if (ret < 0) |
| 703 | return ret; |
| 704 | |
| 705 | memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE); |
| 706 | if (!sgout) { |
| 707 | nsg = skb_cow_data(skb, 0, &unused) + 1; |
| 708 | sgin = kmalloc_array(nsg, sizeof(*sgin), sk->sk_allocation); |
Colin Ian King | 95ad754 | 2018-04-24 13:36:58 +0100 | [diff] [blame] | 709 | sgout = sgin; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | sg_init_table(sgin, nsg); |
Matt Mullins | 8ab6ffb | 2018-05-16 10:48:40 -0700 | [diff] [blame] | 713 | sg_set_buf(&sgin[0], ctx->rx_aad_ciphertext, TLS_AAD_SPACE_SIZE); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 714 | |
| 715 | nsg = skb_to_sgvec(skb, &sgin[1], |
| 716 | rxm->offset + tls_ctx->rx.prepend_size, |
| 717 | rxm->full_len - tls_ctx->rx.prepend_size); |
Doron Roberts-Kedes | 52ee6ef | 2018-07-02 10:25:05 -0700 | [diff] [blame] | 718 | if (nsg < 0) { |
| 719 | ret = nsg; |
| 720 | goto out; |
| 721 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 722 | |
Matt Mullins | 8ab6ffb | 2018-05-16 10:48:40 -0700 | [diff] [blame] | 723 | tls_make_aad(ctx->rx_aad_ciphertext, |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 724 | rxm->full_len - tls_ctx->rx.overhead_size, |
| 725 | tls_ctx->rx.rec_seq, |
| 726 | tls_ctx->rx.rec_seq_size, |
| 727 | ctx->control); |
| 728 | |
| 729 | ret = tls_do_decryption(sk, sgin, sgout, iv, |
| 730 | rxm->full_len - tls_ctx->rx.overhead_size, |
| 731 | skb, sk->sk_allocation); |
| 732 | |
Doron Roberts-Kedes | 52ee6ef | 2018-07-02 10:25:05 -0700 | [diff] [blame] | 733 | out: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 734 | if (sgin != &sgin_arr[0]) |
| 735 | kfree(sgin); |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
| 740 | static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb, |
| 741 | unsigned int len) |
| 742 | { |
| 743 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 744 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 745 | struct strp_msg *rxm = strp_msg(skb); |
| 746 | |
| 747 | if (len < rxm->full_len) { |
| 748 | rxm->offset += len; |
| 749 | rxm->full_len -= len; |
| 750 | |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | /* Finished with message */ |
| 755 | ctx->recv_pkt = NULL; |
| 756 | kfree_skb(skb); |
Doron Roberts-Kedes | 7170e60 | 2018-06-06 09:33:28 -0700 | [diff] [blame] | 757 | __strp_unpause(&ctx->strp); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 758 | |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | int tls_sw_recvmsg(struct sock *sk, |
| 763 | struct msghdr *msg, |
| 764 | size_t len, |
| 765 | int nonblock, |
| 766 | int flags, |
| 767 | int *addr_len) |
| 768 | { |
| 769 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 770 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 771 | unsigned char control; |
| 772 | struct strp_msg *rxm; |
| 773 | struct sk_buff *skb; |
| 774 | ssize_t copied = 0; |
| 775 | bool cmsg = false; |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 776 | int target, err = 0; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 777 | long timeo; |
Doron Roberts-Kedes | 0a26cf3 | 2018-07-25 14:48:21 -0700 | [diff] [blame] | 778 | bool is_kvec = msg->msg_iter.type & ITER_KVEC; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 779 | |
| 780 | flags |= nonblock; |
| 781 | |
| 782 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 783 | return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); |
| 784 | |
| 785 | lock_sock(sk); |
| 786 | |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 787 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 788 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 789 | do { |
| 790 | bool zc = false; |
| 791 | int chunk = 0; |
| 792 | |
| 793 | skb = tls_wait_data(sk, flags, timeo, &err); |
| 794 | if (!skb) |
| 795 | goto recv_end; |
| 796 | |
| 797 | rxm = strp_msg(skb); |
| 798 | if (!cmsg) { |
| 799 | int cerr; |
| 800 | |
| 801 | cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, |
| 802 | sizeof(ctx->control), &ctx->control); |
| 803 | cmsg = true; |
| 804 | control = ctx->control; |
| 805 | if (ctx->control != TLS_RECORD_TYPE_DATA) { |
| 806 | if (cerr || msg->msg_flags & MSG_CTRUNC) { |
| 807 | err = -EIO; |
| 808 | goto recv_end; |
| 809 | } |
| 810 | } |
| 811 | } else if (control != ctx->control) { |
| 812 | goto recv_end; |
| 813 | } |
| 814 | |
| 815 | if (!ctx->decrypted) { |
| 816 | int page_count; |
| 817 | int to_copy; |
| 818 | |
| 819 | page_count = iov_iter_npages(&msg->msg_iter, |
| 820 | MAX_SKB_FRAGS); |
| 821 | to_copy = rxm->full_len - tls_ctx->rx.overhead_size; |
Doron Roberts-Kedes | 0a26cf3 | 2018-07-25 14:48:21 -0700 | [diff] [blame] | 822 | if (!is_kvec && to_copy <= len && page_count < MAX_SKB_FRAGS && |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 823 | likely(!(flags & MSG_PEEK))) { |
| 824 | struct scatterlist sgin[MAX_SKB_FRAGS + 1]; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 825 | int pages = 0; |
| 826 | |
| 827 | zc = true; |
| 828 | sg_init_table(sgin, MAX_SKB_FRAGS + 1); |
Matt Mullins | 8ab6ffb | 2018-05-16 10:48:40 -0700 | [diff] [blame] | 829 | sg_set_buf(&sgin[0], ctx->rx_aad_plaintext, |
| 830 | TLS_AAD_SPACE_SIZE); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 831 | |
| 832 | err = zerocopy_from_iter(sk, &msg->msg_iter, |
| 833 | to_copy, &pages, |
| 834 | &chunk, &sgin[1], |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 835 | MAX_SKB_FRAGS, false); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 836 | if (err < 0) |
| 837 | goto fallback_to_reg_recv; |
| 838 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 839 | err = decrypt_skb_update(sk, skb, sgin, &zc); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 840 | for (; pages > 0; pages--) |
| 841 | put_page(sg_page(&sgin[pages])); |
| 842 | if (err < 0) { |
| 843 | tls_err_abort(sk, EBADMSG); |
| 844 | goto recv_end; |
| 845 | } |
| 846 | } else { |
| 847 | fallback_to_reg_recv: |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 848 | err = decrypt_skb_update(sk, skb, NULL, &zc); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 849 | if (err < 0) { |
| 850 | tls_err_abort(sk, EBADMSG); |
| 851 | goto recv_end; |
| 852 | } |
| 853 | } |
| 854 | ctx->decrypted = true; |
| 855 | } |
| 856 | |
| 857 | if (!zc) { |
| 858 | chunk = min_t(unsigned int, rxm->full_len, len); |
| 859 | err = skb_copy_datagram_msg(skb, rxm->offset, msg, |
| 860 | chunk); |
| 861 | if (err < 0) |
| 862 | goto recv_end; |
| 863 | } |
| 864 | |
| 865 | copied += chunk; |
| 866 | len -= chunk; |
| 867 | if (likely(!(flags & MSG_PEEK))) { |
| 868 | u8 control = ctx->control; |
| 869 | |
| 870 | if (tls_sw_advance_skb(sk, skb, chunk)) { |
| 871 | /* Return full control message to |
| 872 | * userspace before trying to parse |
| 873 | * another message type |
| 874 | */ |
| 875 | msg->msg_flags |= MSG_EOR; |
| 876 | if (control != TLS_RECORD_TYPE_DATA) |
| 877 | goto recv_end; |
| 878 | } |
| 879 | } |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 880 | /* If we have a new message from strparser, continue now. */ |
| 881 | if (copied >= target && !ctx->recv_pkt) |
| 882 | break; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 883 | } while (len); |
| 884 | |
| 885 | recv_end: |
| 886 | release_sock(sk); |
| 887 | return copied ? : err; |
| 888 | } |
| 889 | |
| 890 | ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 891 | struct pipe_inode_info *pipe, |
| 892 | size_t len, unsigned int flags) |
| 893 | { |
| 894 | struct tls_context *tls_ctx = tls_get_ctx(sock->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 895 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 896 | struct strp_msg *rxm = NULL; |
| 897 | struct sock *sk = sock->sk; |
| 898 | struct sk_buff *skb; |
| 899 | ssize_t copied = 0; |
| 900 | int err = 0; |
| 901 | long timeo; |
| 902 | int chunk; |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 903 | bool zc; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 904 | |
| 905 | lock_sock(sk); |
| 906 | |
| 907 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 908 | |
| 909 | skb = tls_wait_data(sk, flags, timeo, &err); |
| 910 | if (!skb) |
| 911 | goto splice_read_end; |
| 912 | |
| 913 | /* splice does not support reading control messages */ |
| 914 | if (ctx->control != TLS_RECORD_TYPE_DATA) { |
| 915 | err = -ENOTSUPP; |
| 916 | goto splice_read_end; |
| 917 | } |
| 918 | |
| 919 | if (!ctx->decrypted) { |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 920 | err = decrypt_skb_update(sk, skb, NULL, &zc); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 921 | |
| 922 | if (err < 0) { |
| 923 | tls_err_abort(sk, EBADMSG); |
| 924 | goto splice_read_end; |
| 925 | } |
| 926 | ctx->decrypted = true; |
| 927 | } |
| 928 | rxm = strp_msg(skb); |
| 929 | |
| 930 | chunk = min_t(unsigned int, rxm->full_len, len); |
| 931 | copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags); |
| 932 | if (copied < 0) |
| 933 | goto splice_read_end; |
| 934 | |
| 935 | if (likely(!(flags & MSG_PEEK))) |
| 936 | tls_sw_advance_skb(sk, skb, copied); |
| 937 | |
| 938 | splice_read_end: |
| 939 | release_sock(sk); |
| 940 | return copied ? : err; |
| 941 | } |
| 942 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 943 | unsigned int tls_sw_poll(struct file *file, struct socket *sock, |
| 944 | struct poll_table_struct *wait) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 945 | { |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 946 | unsigned int ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 947 | struct sock *sk = sock->sk; |
| 948 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 949 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 950 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 951 | /* Grab POLLOUT and POLLHUP from the underlying socket */ |
| 952 | ret = ctx->sk_poll(file, sock, wait); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 953 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 954 | /* Clear POLLIN bits, and set based on recv_pkt */ |
| 955 | ret &= ~(POLLIN | POLLRDNORM); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 956 | if (ctx->recv_pkt) |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 957 | ret |= POLLIN | POLLRDNORM; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 958 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 959 | return ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static int tls_read_size(struct strparser *strp, struct sk_buff *skb) |
| 963 | { |
| 964 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 965 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 966 | char header[TLS_HEADER_SIZE + MAX_IV_SIZE]; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 967 | struct strp_msg *rxm = strp_msg(skb); |
| 968 | size_t cipher_overhead; |
| 969 | size_t data_len = 0; |
| 970 | int ret; |
| 971 | |
| 972 | /* Verify that we have a full TLS header, or wait for more data */ |
| 973 | if (rxm->offset + tls_ctx->rx.prepend_size > skb->len) |
| 974 | return 0; |
| 975 | |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 976 | /* Sanity-check size of on-stack buffer. */ |
| 977 | if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) { |
| 978 | ret = -EINVAL; |
| 979 | goto read_failure; |
| 980 | } |
| 981 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 982 | /* Linearize header to local buffer */ |
| 983 | ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size); |
| 984 | |
| 985 | if (ret < 0) |
| 986 | goto read_failure; |
| 987 | |
| 988 | ctx->control = header[0]; |
| 989 | |
| 990 | data_len = ((header[4] & 0xFF) | (header[3] << 8)); |
| 991 | |
| 992 | cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size; |
| 993 | |
| 994 | if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) { |
| 995 | ret = -EMSGSIZE; |
| 996 | goto read_failure; |
| 997 | } |
| 998 | if (data_len < cipher_overhead) { |
| 999 | ret = -EBADMSG; |
| 1000 | goto read_failure; |
| 1001 | } |
| 1002 | |
| 1003 | if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.version) || |
| 1004 | header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.version)) { |
| 1005 | ret = -EINVAL; |
| 1006 | goto read_failure; |
| 1007 | } |
| 1008 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1009 | #ifdef CONFIG_TLS_DEVICE |
| 1010 | handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset, |
| 1011 | *(u64*)tls_ctx->rx.rec_seq); |
| 1012 | #endif |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1013 | return data_len + TLS_HEADER_SIZE; |
| 1014 | |
| 1015 | read_failure: |
| 1016 | tls_err_abort(strp->sk, ret); |
| 1017 | |
| 1018 | return ret; |
| 1019 | } |
| 1020 | |
| 1021 | static void tls_queue(struct strparser *strp, struct sk_buff *skb) |
| 1022 | { |
| 1023 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1024 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1025 | |
| 1026 | ctx->decrypted = false; |
| 1027 | |
| 1028 | ctx->recv_pkt = skb; |
| 1029 | strp_pause(strp); |
| 1030 | |
Vakul Garg | ad13acc | 2018-07-30 16:08:33 +0530 | [diff] [blame^] | 1031 | ctx->saved_data_ready(strp->sk); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | static void tls_data_ready(struct sock *sk) |
| 1035 | { |
| 1036 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1037 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1038 | |
| 1039 | strp_data_ready(&ctx->strp); |
| 1040 | } |
| 1041 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1042 | void tls_sw_free_resources_tx(struct sock *sk) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1043 | { |
| 1044 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1045 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1046 | |
Vakul Garg | 201876b | 2018-07-24 16:54:27 +0530 | [diff] [blame] | 1047 | crypto_free_aead(ctx->aead_send); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1048 | tls_free_both_sg(sk); |
| 1049 | |
| 1050 | kfree(ctx); |
| 1051 | } |
| 1052 | |
Boris Pismenny | 39f56e1 | 2018-07-13 14:33:41 +0300 | [diff] [blame] | 1053 | void tls_sw_release_resources_rx(struct sock *sk) |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1054 | { |
| 1055 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1056 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1057 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1058 | if (ctx->aead_recv) { |
Vakul Garg | 201876b | 2018-07-24 16:54:27 +0530 | [diff] [blame] | 1059 | kfree_skb(ctx->recv_pkt); |
| 1060 | ctx->recv_pkt = NULL; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1061 | crypto_free_aead(ctx->aead_recv); |
| 1062 | strp_stop(&ctx->strp); |
| 1063 | write_lock_bh(&sk->sk_callback_lock); |
| 1064 | sk->sk_data_ready = ctx->saved_data_ready; |
| 1065 | write_unlock_bh(&sk->sk_callback_lock); |
| 1066 | release_sock(sk); |
| 1067 | strp_done(&ctx->strp); |
| 1068 | lock_sock(sk); |
| 1069 | } |
Boris Pismenny | 39f56e1 | 2018-07-13 14:33:41 +0300 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | void tls_sw_free_resources_rx(struct sock *sk) |
| 1073 | { |
| 1074 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1075 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1076 | |
| 1077 | tls_sw_release_resources_rx(sk); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1078 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1079 | kfree(ctx); |
| 1080 | } |
| 1081 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1082 | int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1083 | { |
| 1084 | char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE]; |
| 1085 | struct tls_crypto_info *crypto_info; |
| 1086 | struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1087 | struct tls_sw_context_tx *sw_ctx_tx = NULL; |
| 1088 | struct tls_sw_context_rx *sw_ctx_rx = NULL; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1089 | struct cipher_context *cctx; |
| 1090 | struct crypto_aead **aead; |
| 1091 | struct strp_callbacks cb; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1092 | u16 nonce_size, tag_size, iv_size, rec_seq_size; |
| 1093 | char *iv, *rec_seq; |
| 1094 | int rc = 0; |
| 1095 | |
| 1096 | if (!ctx) { |
| 1097 | rc = -EINVAL; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1101 | if (tx) { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1102 | if (!ctx->priv_ctx_tx) { |
| 1103 | sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); |
| 1104 | if (!sw_ctx_tx) { |
| 1105 | rc = -ENOMEM; |
| 1106 | goto out; |
| 1107 | } |
| 1108 | ctx->priv_ctx_tx = sw_ctx_tx; |
| 1109 | } else { |
| 1110 | sw_ctx_tx = |
| 1111 | (struct tls_sw_context_tx *)ctx->priv_ctx_tx; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1112 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1113 | } else { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1114 | if (!ctx->priv_ctx_rx) { |
| 1115 | sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); |
| 1116 | if (!sw_ctx_rx) { |
| 1117 | rc = -ENOMEM; |
| 1118 | goto out; |
| 1119 | } |
| 1120 | ctx->priv_ctx_rx = sw_ctx_rx; |
| 1121 | } else { |
| 1122 | sw_ctx_rx = |
| 1123 | (struct tls_sw_context_rx *)ctx->priv_ctx_rx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1124 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1127 | if (tx) { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1128 | crypto_init_wait(&sw_ctx_tx->async_wait); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1129 | crypto_info = &ctx->crypto_send; |
| 1130 | cctx = &ctx->tx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1131 | aead = &sw_ctx_tx->aead_send; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1132 | } else { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1133 | crypto_init_wait(&sw_ctx_rx->async_wait); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1134 | crypto_info = &ctx->crypto_recv; |
| 1135 | cctx = &ctx->rx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1136 | aead = &sw_ctx_rx->aead_recv; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1139 | switch (crypto_info->cipher_type) { |
| 1140 | case TLS_CIPHER_AES_GCM_128: { |
| 1141 | nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 1142 | tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 1143 | iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 1144 | iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; |
| 1145 | rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; |
| 1146 | rec_seq = |
| 1147 | ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; |
| 1148 | gcm_128_info = |
| 1149 | (struct tls12_crypto_info_aes_gcm_128 *)crypto_info; |
| 1150 | break; |
| 1151 | } |
| 1152 | default: |
| 1153 | rc = -EINVAL; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1154 | goto free_priv; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 1157 | /* Sanity-check the IV size for stack allocations. */ |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 1158 | if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) { |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 1159 | rc = -EINVAL; |
| 1160 | goto free_priv; |
| 1161 | } |
| 1162 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1163 | cctx->prepend_size = TLS_HEADER_SIZE + nonce_size; |
| 1164 | cctx->tag_size = tag_size; |
| 1165 | cctx->overhead_size = cctx->prepend_size + cctx->tag_size; |
| 1166 | cctx->iv_size = iv_size; |
| 1167 | cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 1168 | GFP_KERNEL); |
| 1169 | if (!cctx->iv) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1170 | rc = -ENOMEM; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1171 | goto free_priv; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1172 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1173 | memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE); |
| 1174 | memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); |
| 1175 | cctx->rec_seq_size = rec_seq_size; |
| 1176 | cctx->rec_seq = kmalloc(rec_seq_size, GFP_KERNEL); |
| 1177 | if (!cctx->rec_seq) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1178 | rc = -ENOMEM; |
| 1179 | goto free_iv; |
| 1180 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1181 | memcpy(cctx->rec_seq, rec_seq, rec_seq_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1182 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1183 | if (sw_ctx_tx) { |
| 1184 | sg_init_table(sw_ctx_tx->sg_encrypted_data, |
| 1185 | ARRAY_SIZE(sw_ctx_tx->sg_encrypted_data)); |
| 1186 | sg_init_table(sw_ctx_tx->sg_plaintext_data, |
| 1187 | ARRAY_SIZE(sw_ctx_tx->sg_plaintext_data)); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1188 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1189 | sg_init_table(sw_ctx_tx->sg_aead_in, 2); |
| 1190 | sg_set_buf(&sw_ctx_tx->sg_aead_in[0], sw_ctx_tx->aad_space, |
| 1191 | sizeof(sw_ctx_tx->aad_space)); |
| 1192 | sg_unmark_end(&sw_ctx_tx->sg_aead_in[1]); |
| 1193 | sg_chain(sw_ctx_tx->sg_aead_in, 2, |
| 1194 | sw_ctx_tx->sg_plaintext_data); |
| 1195 | sg_init_table(sw_ctx_tx->sg_aead_out, 2); |
| 1196 | sg_set_buf(&sw_ctx_tx->sg_aead_out[0], sw_ctx_tx->aad_space, |
| 1197 | sizeof(sw_ctx_tx->aad_space)); |
| 1198 | sg_unmark_end(&sw_ctx_tx->sg_aead_out[1]); |
| 1199 | sg_chain(sw_ctx_tx->sg_aead_out, 2, |
| 1200 | sw_ctx_tx->sg_encrypted_data); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1201 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1202 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1203 | if (!*aead) { |
| 1204 | *aead = crypto_alloc_aead("gcm(aes)", 0, 0); |
| 1205 | if (IS_ERR(*aead)) { |
| 1206 | rc = PTR_ERR(*aead); |
| 1207 | *aead = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1208 | goto free_rec_seq; |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | ctx->push_pending_record = tls_sw_push_pending_record; |
| 1213 | |
| 1214 | memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE); |
| 1215 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1216 | rc = crypto_aead_setkey(*aead, keyval, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1217 | TLS_CIPHER_AES_GCM_128_KEY_SIZE); |
| 1218 | if (rc) |
| 1219 | goto free_aead; |
| 1220 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1221 | rc = crypto_aead_setauthsize(*aead, cctx->tag_size); |
| 1222 | if (rc) |
| 1223 | goto free_aead; |
| 1224 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1225 | if (sw_ctx_rx) { |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1226 | /* Set up strparser */ |
| 1227 | memset(&cb, 0, sizeof(cb)); |
| 1228 | cb.rcv_msg = tls_queue; |
| 1229 | cb.parse_msg = tls_read_size; |
| 1230 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1231 | strp_init(&sw_ctx_rx->strp, sk, &cb); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1232 | |
| 1233 | write_lock_bh(&sk->sk_callback_lock); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1234 | sw_ctx_rx->saved_data_ready = sk->sk_data_ready; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1235 | sk->sk_data_ready = tls_data_ready; |
| 1236 | write_unlock_bh(&sk->sk_callback_lock); |
| 1237 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1238 | sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1239 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1240 | strp_check_rcv(&sw_ctx_rx->strp); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | goto out; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1244 | |
| 1245 | free_aead: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1246 | crypto_free_aead(*aead); |
| 1247 | *aead = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1248 | free_rec_seq: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1249 | kfree(cctx->rec_seq); |
| 1250 | cctx->rec_seq = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1251 | free_iv: |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1252 | kfree(cctx->iv); |
| 1253 | cctx->iv = NULL; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1254 | free_priv: |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1255 | if (tx) { |
| 1256 | kfree(ctx->priv_ctx_tx); |
| 1257 | ctx->priv_ctx_tx = NULL; |
| 1258 | } else { |
| 1259 | kfree(ctx->priv_ctx_rx); |
| 1260 | ctx->priv_ctx_rx = NULL; |
| 1261 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1262 | out: |
| 1263 | return rc; |
| 1264 | } |