| 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 | * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io |
| 8 | * |
| 9 | * This software is available to you under a choice of one of two |
| 10 | * licenses. You may choose to be licensed under the terms of the GNU |
| 11 | * General Public License (GPL) Version 2, available from the file |
| 12 | * COPYING in the main directory of this source tree, or the |
| 13 | * OpenIB.org BSD license below: |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or |
| 16 | * without modification, are permitted provided that the following |
| 17 | * conditions are met: |
| 18 | * |
| 19 | * - Redistributions of source code must retain the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer. |
| 22 | * |
| 23 | * - Redistributions in binary form must reproduce the above |
| 24 | * copyright notice, this list of conditions and the following |
| 25 | * disclaimer in the documentation and/or other materials |
| 26 | * provided with the distribution. |
| 27 | * |
| 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 31 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 32 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 33 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 34 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | * SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/bug.h> |
| 39 | #include <linux/sched/signal.h> |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/kernel.h> |
| 42 | #include <linux/splice.h> |
| 43 | #include <crypto/aead.h> |
| 44 | |
| 45 | #include <net/strparser.h> |
| 46 | #include <net/tls.h> |
| 47 | #include <trace/events/sock.h> |
| 48 | |
| 49 | #include "tls.h" |
| 50 | |
| 51 | struct tls_decrypt_arg { |
| 52 | struct_group(inargs, |
| 53 | bool zc; |
| 54 | bool async; |
| 55 | bool async_done; |
| 56 | u8 tail; |
| 57 | ); |
| 58 | |
| 59 | struct sk_buff *skb; |
| 60 | }; |
| 61 | |
| 62 | struct tls_decrypt_ctx { |
| 63 | struct sock *sk; |
| 64 | u8 iv[TLS_MAX_IV_SIZE]; |
| 65 | u8 aad[TLS_MAX_AAD_SIZE]; |
| 66 | u8 tail; |
| 67 | bool free_sgout; |
| 68 | struct scatterlist sg[]; |
| 69 | }; |
| 70 | |
| 71 | noinline void tls_err_abort(struct sock *sk, int err) |
| 72 | { |
| 73 | WARN_ON_ONCE(err >= 0); |
| 74 | /* sk->sk_err should contain a positive error code. */ |
| 75 | WRITE_ONCE(sk->sk_err, -err); |
| 76 | /* Paired with smp_rmb() in tcp_poll() */ |
| 77 | smp_wmb(); |
| 78 | sk_error_report(sk); |
| 79 | } |
| 80 | |
| 81 | static int __skb_nsg(struct sk_buff *skb, int offset, int len, |
| 82 | unsigned int recursion_level) |
| 83 | { |
| 84 | int start = skb_headlen(skb); |
| 85 | int i, chunk = start - offset; |
| 86 | struct sk_buff *frag_iter; |
| 87 | int elt = 0; |
| 88 | |
| 89 | if (unlikely(recursion_level >= 24)) |
| 90 | return -EMSGSIZE; |
| 91 | |
| 92 | if (chunk > 0) { |
| 93 | if (chunk > len) |
| 94 | chunk = len; |
| 95 | elt++; |
| 96 | len -= chunk; |
| 97 | if (len == 0) |
| 98 | return elt; |
| 99 | offset += chunk; |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 103 | int end; |
| 104 | |
| 105 | WARN_ON(start > offset + len); |
| 106 | |
| 107 | end = start + skb_frag_size(frag: &skb_shinfo(skb)->frags[i]); |
| 108 | chunk = end - offset; |
| 109 | if (chunk > 0) { |
| 110 | if (chunk > len) |
| 111 | chunk = len; |
| 112 | elt++; |
| 113 | len -= chunk; |
| 114 | if (len == 0) |
| 115 | return elt; |
| 116 | offset += chunk; |
| 117 | } |
| 118 | start = end; |
| 119 | } |
| 120 | |
| 121 | if (unlikely(skb_has_frag_list(skb))) { |
| 122 | skb_walk_frags(skb, frag_iter) { |
| 123 | int end, ret; |
| 124 | |
| 125 | WARN_ON(start > offset + len); |
| 126 | |
| 127 | end = start + frag_iter->len; |
| 128 | chunk = end - offset; |
| 129 | if (chunk > 0) { |
| 130 | if (chunk > len) |
| 131 | chunk = len; |
| 132 | ret = __skb_nsg(skb: frag_iter, offset: offset - start, len: chunk, |
| 133 | recursion_level: recursion_level + 1); |
| 134 | if (unlikely(ret < 0)) |
| 135 | return ret; |
| 136 | elt += ret; |
| 137 | len -= chunk; |
| 138 | if (len == 0) |
| 139 | return elt; |
| 140 | offset += chunk; |
| 141 | } |
| 142 | start = end; |
| 143 | } |
| 144 | } |
| 145 | BUG_ON(len); |
| 146 | return elt; |
| 147 | } |
| 148 | |
| 149 | /* Return the number of scatterlist elements required to completely map the |
| 150 | * skb, or -EMSGSIZE if the recursion depth is exceeded. |
| 151 | */ |
| 152 | static int skb_nsg(struct sk_buff *skb, int offset, int len) |
| 153 | { |
| 154 | return __skb_nsg(skb, offset, len, recursion_level: 0); |
| 155 | } |
| 156 | |
| 157 | static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb, |
| 158 | struct tls_decrypt_arg *darg) |
| 159 | { |
| 160 | struct strp_msg *rxm = strp_msg(skb); |
| 161 | struct tls_msg *tlm = tls_msg(skb); |
| 162 | int sub = 0; |
| 163 | |
| 164 | /* Determine zero-padding length */ |
| 165 | if (prot->version == TLS_1_3_VERSION) { |
| 166 | int offset = rxm->full_len - TLS_TAG_SIZE - 1; |
| 167 | char content_type = darg->zc ? darg->tail : 0; |
| 168 | int err; |
| 169 | |
| 170 | while (content_type == 0) { |
| 171 | if (offset < prot->prepend_size) |
| 172 | return -EBADMSG; |
| 173 | err = skb_copy_bits(skb, offset: rxm->offset + offset, |
| 174 | to: &content_type, len: 1); |
| 175 | if (err) |
| 176 | return err; |
| 177 | if (content_type) |
| 178 | break; |
| 179 | sub++; |
| 180 | offset--; |
| 181 | } |
| 182 | tlm->control = content_type; |
| 183 | } |
| 184 | return sub; |
| 185 | } |
| 186 | |
| 187 | static void tls_decrypt_done(void *data, int err) |
| 188 | { |
| 189 | struct aead_request *aead_req = data; |
| 190 | struct crypto_aead *aead = crypto_aead_reqtfm(req: aead_req); |
| 191 | struct scatterlist *sgout = aead_req->dst; |
| 192 | struct tls_sw_context_rx *ctx; |
| 193 | struct tls_decrypt_ctx *dctx; |
| 194 | struct tls_context *tls_ctx; |
| 195 | struct scatterlist *sg; |
| 196 | unsigned int pages; |
| 197 | struct sock *sk; |
| 198 | int aead_size; |
| 199 | |
| 200 | /* If requests get too backlogged crypto API returns -EBUSY and calls |
| 201 | * ->complete(-EINPROGRESS) immediately followed by ->complete(0) |
| 202 | * to make waiting for backlog to flush with crypto_wait_req() easier. |
| 203 | * First wait converts -EBUSY -> -EINPROGRESS, and the second one |
| 204 | * -EINPROGRESS -> 0. |
| 205 | * We have a single struct crypto_async_request per direction, this |
| 206 | * scheme doesn't help us, so just ignore the first ->complete(). |
| 207 | */ |
| 208 | if (err == -EINPROGRESS) |
| 209 | return; |
| 210 | |
| 211 | aead_size = sizeof(*aead_req) + crypto_aead_reqsize(tfm: aead); |
| 212 | aead_size = ALIGN(aead_size, __alignof__(*dctx)); |
| 213 | dctx = (void *)((u8 *)aead_req + aead_size); |
| 214 | |
| 215 | sk = dctx->sk; |
| 216 | tls_ctx = tls_get_ctx(sk); |
| 217 | ctx = tls_sw_ctx_rx(tls_ctx); |
| 218 | |
| 219 | /* Propagate if there was an err */ |
| 220 | if (err) { |
| 221 | if (err == -EBADMSG) |
| 222 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR); |
| 223 | ctx->async_wait.err = err; |
| 224 | tls_err_abort(sk, err); |
| 225 | } |
| 226 | |
| 227 | /* Free the destination pages if skb was not decrypted inplace */ |
| 228 | if (dctx->free_sgout) { |
| 229 | /* Skip the first S/G entry as it points to AAD */ |
| 230 | for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) { |
| 231 | if (!sg) |
| 232 | break; |
| 233 | put_page(page: sg_page(sg)); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | kfree(objp: aead_req); |
| 238 | |
| 239 | if (atomic_dec_and_test(v: &ctx->decrypt_pending)) |
| 240 | complete(&ctx->async_wait.completion); |
| 241 | } |
| 242 | |
| 243 | static int tls_decrypt_async_wait(struct tls_sw_context_rx *ctx) |
| 244 | { |
| 245 | if (!atomic_dec_and_test(v: &ctx->decrypt_pending)) |
| 246 | crypto_wait_req(err: -EINPROGRESS, wait: &ctx->async_wait); |
| 247 | atomic_inc(v: &ctx->decrypt_pending); |
| 248 | |
| 249 | return ctx->async_wait.err; |
| 250 | } |
| 251 | |
| 252 | static int tls_do_decryption(struct sock *sk, |
| 253 | struct scatterlist *sgin, |
| 254 | struct scatterlist *sgout, |
| 255 | char *iv_recv, |
| 256 | size_t data_len, |
| 257 | struct aead_request *aead_req, |
| 258 | struct tls_decrypt_arg *darg) |
| 259 | { |
| 260 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 261 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 262 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 263 | int ret; |
| 264 | |
| 265 | aead_request_set_tfm(req: aead_req, tfm: ctx->aead_recv); |
| 266 | aead_request_set_ad(req: aead_req, assoclen: prot->aad_size); |
| 267 | aead_request_set_crypt(req: aead_req, src: sgin, dst: sgout, |
| 268 | cryptlen: data_len + prot->tag_size, |
| 269 | iv: (u8 *)iv_recv); |
| 270 | |
| 271 | if (darg->async) { |
| 272 | aead_request_set_callback(req: aead_req, |
| 273 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 274 | compl: tls_decrypt_done, data: aead_req); |
| 275 | DEBUG_NET_WARN_ON_ONCE(atomic_read(&ctx->decrypt_pending) < 1); |
| 276 | atomic_inc(v: &ctx->decrypt_pending); |
| 277 | } else { |
| 278 | DECLARE_CRYPTO_WAIT(wait); |
| 279 | |
| 280 | aead_request_set_callback(req: aead_req, |
| 281 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 282 | compl: crypto_req_done, data: &wait); |
| 283 | ret = crypto_aead_decrypt(req: aead_req); |
| 284 | if (ret == -EINPROGRESS || ret == -EBUSY) |
| 285 | ret = crypto_wait_req(err: ret, wait: &wait); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | ret = crypto_aead_decrypt(req: aead_req); |
| 290 | if (ret == -EINPROGRESS) |
| 291 | return 0; |
| 292 | |
| 293 | if (ret == -EBUSY) { |
| 294 | ret = tls_decrypt_async_wait(ctx); |
| 295 | darg->async_done = true; |
| 296 | /* all completions have run, we're not doing async anymore */ |
| 297 | darg->async = false; |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | atomic_dec(v: &ctx->decrypt_pending); |
| 302 | darg->async = false; |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static void tls_trim_both_msgs(struct sock *sk, int target_size) |
| 308 | { |
| 309 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 310 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 311 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 312 | struct tls_rec *rec = ctx->open_rec; |
| 313 | |
| 314 | sk_msg_trim(sk, msg: &rec->msg_plaintext, len: target_size); |
| 315 | if (target_size > 0) |
| 316 | target_size += prot->overhead_size; |
| 317 | sk_msg_trim(sk, msg: &rec->msg_encrypted, len: target_size); |
| 318 | } |
| 319 | |
| 320 | static int tls_alloc_encrypted_msg(struct sock *sk, int len) |
| 321 | { |
| 322 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 323 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 324 | struct tls_rec *rec = ctx->open_rec; |
| 325 | struct sk_msg *msg_en = &rec->msg_encrypted; |
| 326 | |
| 327 | return sk_msg_alloc(sk, msg: msg_en, len, elem_first_coalesce: 0); |
| 328 | } |
| 329 | |
| 330 | static int tls_clone_plaintext_msg(struct sock *sk, int required) |
| 331 | { |
| 332 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 333 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 334 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 335 | struct tls_rec *rec = ctx->open_rec; |
| 336 | struct sk_msg *msg_pl = &rec->msg_plaintext; |
| 337 | struct sk_msg *msg_en = &rec->msg_encrypted; |
| 338 | int skip, len; |
| 339 | |
| 340 | /* We add page references worth len bytes from encrypted sg |
| 341 | * at the end of plaintext sg. It is guaranteed that msg_en |
| 342 | * has enough required room (ensured by caller). |
| 343 | */ |
| 344 | len = required - msg_pl->sg.size; |
| 345 | |
| 346 | /* Skip initial bytes in msg_en's data to be able to use |
| 347 | * same offset of both plain and encrypted data. |
| 348 | */ |
| 349 | skip = prot->prepend_size + msg_pl->sg.size; |
| 350 | |
| 351 | return sk_msg_clone(sk, dst: msg_pl, src: msg_en, off: skip, len); |
| 352 | } |
| 353 | |
| 354 | static struct tls_rec *tls_get_rec(struct sock *sk) |
| 355 | { |
| 356 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 357 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 358 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 359 | struct sk_msg *msg_pl, *msg_en; |
| 360 | struct tls_rec *rec; |
| 361 | int mem_size; |
| 362 | |
| 363 | mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(tfm: ctx->aead_send); |
| 364 | |
| 365 | rec = kzalloc(mem_size, sk->sk_allocation); |
| 366 | if (!rec) |
| 367 | return NULL; |
| 368 | |
| 369 | msg_pl = &rec->msg_plaintext; |
| 370 | msg_en = &rec->msg_encrypted; |
| 371 | |
| 372 | sk_msg_init(msg: msg_pl); |
| 373 | sk_msg_init(msg: msg_en); |
| 374 | |
| 375 | sg_init_table(rec->sg_aead_in, 2); |
| 376 | sg_set_buf(sg: &rec->sg_aead_in[0], buf: rec->aad_space, buflen: prot->aad_size); |
| 377 | sg_unmark_end(sg: &rec->sg_aead_in[1]); |
| 378 | |
| 379 | sg_init_table(rec->sg_aead_out, 2); |
| 380 | sg_set_buf(sg: &rec->sg_aead_out[0], buf: rec->aad_space, buflen: prot->aad_size); |
| 381 | sg_unmark_end(sg: &rec->sg_aead_out[1]); |
| 382 | |
| 383 | rec->sk = sk; |
| 384 | |
| 385 | return rec; |
| 386 | } |
| 387 | |
| 388 | static void tls_free_rec(struct sock *sk, struct tls_rec *rec) |
| 389 | { |
| 390 | sk_msg_free(sk, msg: &rec->msg_encrypted); |
| 391 | sk_msg_free(sk, msg: &rec->msg_plaintext); |
| 392 | kfree(objp: rec); |
| 393 | } |
| 394 | |
| 395 | static void tls_free_open_rec(struct sock *sk) |
| 396 | { |
| 397 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 398 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 399 | struct tls_rec *rec = ctx->open_rec; |
| 400 | |
| 401 | if (rec) { |
| 402 | tls_free_rec(sk, rec); |
| 403 | ctx->open_rec = NULL; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | int tls_tx_records(struct sock *sk, int flags) |
| 408 | { |
| 409 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 410 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 411 | struct tls_rec *rec, *tmp; |
| 412 | struct sk_msg *msg_en; |
| 413 | int tx_flags, rc = 0; |
| 414 | |
| 415 | if (tls_is_partially_sent_record(ctx: tls_ctx)) { |
| 416 | rec = list_first_entry(&ctx->tx_list, |
| 417 | struct tls_rec, list); |
| 418 | |
| 419 | if (flags == -1) |
| 420 | tx_flags = rec->tx_flags; |
| 421 | else |
| 422 | tx_flags = flags; |
| 423 | |
| 424 | rc = tls_push_partial_record(sk, ctx: tls_ctx, flags: tx_flags); |
| 425 | if (rc) |
| 426 | goto tx_err; |
| 427 | |
| 428 | /* Full record has been transmitted. |
| 429 | * Remove the head of tx_list |
| 430 | */ |
| 431 | list_del(entry: &rec->list); |
| 432 | sk_msg_free(sk, msg: &rec->msg_plaintext); |
| 433 | kfree(objp: rec); |
| 434 | } |
| 435 | |
| 436 | /* Tx all ready records */ |
| 437 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
| 438 | if (READ_ONCE(rec->tx_ready)) { |
| 439 | if (flags == -1) |
| 440 | tx_flags = rec->tx_flags; |
| 441 | else |
| 442 | tx_flags = flags; |
| 443 | |
| 444 | msg_en = &rec->msg_encrypted; |
| 445 | rc = tls_push_sg(sk, ctx: tls_ctx, |
| 446 | sg: &msg_en->sg.data[msg_en->sg.curr], |
| 447 | first_offset: 0, flags: tx_flags); |
| 448 | if (rc) |
| 449 | goto tx_err; |
| 450 | |
| 451 | list_del(entry: &rec->list); |
| 452 | sk_msg_free(sk, msg: &rec->msg_plaintext); |
| 453 | kfree(objp: rec); |
| 454 | } else { |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | tx_err: |
| 460 | if (rc < 0 && rc != -EAGAIN) |
| 461 | tls_err_abort(sk, err: rc); |
| 462 | |
| 463 | return rc; |
| 464 | } |
| 465 | |
| 466 | static void tls_encrypt_done(void *data, int err) |
| 467 | { |
| 468 | struct tls_sw_context_tx *ctx; |
| 469 | struct tls_context *tls_ctx; |
| 470 | struct tls_prot_info *prot; |
| 471 | struct tls_rec *rec = data; |
| 472 | struct scatterlist *sge; |
| 473 | struct sk_msg *msg_en; |
| 474 | struct sock *sk; |
| 475 | |
| 476 | if (err == -EINPROGRESS) /* see the comment in tls_decrypt_done() */ |
| 477 | return; |
| 478 | |
| 479 | msg_en = &rec->msg_encrypted; |
| 480 | |
| 481 | sk = rec->sk; |
| 482 | tls_ctx = tls_get_ctx(sk); |
| 483 | prot = &tls_ctx->prot_info; |
| 484 | ctx = tls_sw_ctx_tx(tls_ctx); |
| 485 | |
| 486 | sge = sk_msg_elem(msg: msg_en, which: msg_en->sg.curr); |
| 487 | sge->offset -= prot->prepend_size; |
| 488 | sge->length += prot->prepend_size; |
| 489 | |
| 490 | /* Check if error is previously set on socket */ |
| 491 | if (err || sk->sk_err) { |
| 492 | rec = NULL; |
| 493 | |
| 494 | /* If err is already set on socket, return the same code */ |
| 495 | if (sk->sk_err) { |
| 496 | ctx->async_wait.err = -sk->sk_err; |
| 497 | } else { |
| 498 | ctx->async_wait.err = err; |
| 499 | tls_err_abort(sk, err); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (rec) { |
| 504 | struct tls_rec *first_rec; |
| 505 | |
| 506 | /* Mark the record as ready for transmission */ |
| 507 | smp_store_mb(rec->tx_ready, true); |
| 508 | |
| 509 | /* If received record is at head of tx_list, schedule tx */ |
| 510 | first_rec = list_first_entry(&ctx->tx_list, |
| 511 | struct tls_rec, list); |
| 512 | if (rec == first_rec) { |
| 513 | /* Schedule the transmission */ |
| 514 | if (!test_and_set_bit(BIT_TX_SCHEDULED, |
| 515 | addr: &ctx->tx_bitmask)) |
| 516 | schedule_delayed_work(dwork: &ctx->tx_work.work, delay: 1); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (atomic_dec_and_test(v: &ctx->encrypt_pending)) |
| 521 | complete(&ctx->async_wait.completion); |
| 522 | } |
| 523 | |
| 524 | static int tls_encrypt_async_wait(struct tls_sw_context_tx *ctx) |
| 525 | { |
| 526 | if (!atomic_dec_and_test(v: &ctx->encrypt_pending)) |
| 527 | crypto_wait_req(err: -EINPROGRESS, wait: &ctx->async_wait); |
| 528 | atomic_inc(v: &ctx->encrypt_pending); |
| 529 | |
| 530 | return ctx->async_wait.err; |
| 531 | } |
| 532 | |
| 533 | static int tls_do_encryption(struct sock *sk, |
| 534 | struct tls_context *tls_ctx, |
| 535 | struct tls_sw_context_tx *ctx, |
| 536 | struct aead_request *aead_req, |
| 537 | size_t data_len, u32 start) |
| 538 | { |
| 539 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 540 | struct tls_rec *rec = ctx->open_rec; |
| 541 | struct sk_msg *msg_en = &rec->msg_encrypted; |
| 542 | struct scatterlist *sge = sk_msg_elem(msg: msg_en, which: start); |
| 543 | int rc, iv_offset = 0; |
| 544 | |
| 545 | /* For CCM based ciphers, first byte of IV is a constant */ |
| 546 | switch (prot->cipher_type) { |
| 547 | case TLS_CIPHER_AES_CCM_128: |
| 548 | rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE; |
| 549 | iv_offset = 1; |
| 550 | break; |
| 551 | case TLS_CIPHER_SM4_CCM: |
| 552 | rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE; |
| 553 | iv_offset = 1; |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv, |
| 558 | prot->iv_size + prot->salt_size); |
| 559 | |
| 560 | tls_xor_iv_with_seq(prot, iv: rec->iv_data + iv_offset, |
| 561 | seq: tls_ctx->tx.rec_seq); |
| 562 | |
| 563 | sge->offset += prot->prepend_size; |
| 564 | sge->length -= prot->prepend_size; |
| 565 | |
| 566 | msg_en->sg.curr = start; |
| 567 | |
| 568 | aead_request_set_tfm(req: aead_req, tfm: ctx->aead_send); |
| 569 | aead_request_set_ad(req: aead_req, assoclen: prot->aad_size); |
| 570 | aead_request_set_crypt(req: aead_req, src: rec->sg_aead_in, |
| 571 | dst: rec->sg_aead_out, |
| 572 | cryptlen: data_len, iv: rec->iv_data); |
| 573 | |
| 574 | aead_request_set_callback(req: aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 575 | compl: tls_encrypt_done, data: rec); |
| 576 | |
| 577 | /* Add the record in tx_list */ |
| 578 | list_add_tail(new: (struct list_head *)&rec->list, head: &ctx->tx_list); |
| 579 | DEBUG_NET_WARN_ON_ONCE(atomic_read(&ctx->encrypt_pending) < 1); |
| 580 | atomic_inc(v: &ctx->encrypt_pending); |
| 581 | |
| 582 | rc = crypto_aead_encrypt(req: aead_req); |
| 583 | if (rc == -EBUSY) { |
| 584 | rc = tls_encrypt_async_wait(ctx); |
| 585 | rc = rc ?: -EINPROGRESS; |
| 586 | } |
| 587 | if (!rc || rc != -EINPROGRESS) { |
| 588 | atomic_dec(v: &ctx->encrypt_pending); |
| 589 | sge->offset -= prot->prepend_size; |
| 590 | sge->length += prot->prepend_size; |
| 591 | } |
| 592 | |
| 593 | if (!rc) { |
| 594 | WRITE_ONCE(rec->tx_ready, true); |
| 595 | } else if (rc != -EINPROGRESS) { |
| 596 | list_del(entry: &rec->list); |
| 597 | return rc; |
| 598 | } |
| 599 | |
| 600 | /* Unhook the record from context if encryption is not failure */ |
| 601 | ctx->open_rec = NULL; |
| 602 | tls_advance_record_sn(sk, prot, ctx: &tls_ctx->tx); |
| 603 | return rc; |
| 604 | } |
| 605 | |
| 606 | static int tls_split_open_record(struct sock *sk, struct tls_rec *from, |
| 607 | struct tls_rec **to, struct sk_msg *msg_opl, |
| 608 | struct sk_msg *msg_oen, u32 split_point, |
| 609 | u32 tx_overhead_size, u32 *orig_end) |
| 610 | { |
| 611 | u32 i, j, bytes = 0, apply = msg_opl->apply_bytes; |
| 612 | struct scatterlist *sge, *osge, *nsge; |
| 613 | u32 orig_size = msg_opl->sg.size; |
| 614 | struct scatterlist tmp = { }; |
| 615 | struct sk_msg *msg_npl; |
| 616 | struct tls_rec *new; |
| 617 | int ret; |
| 618 | |
| 619 | new = tls_get_rec(sk); |
| 620 | if (!new) |
| 621 | return -ENOMEM; |
| 622 | ret = sk_msg_alloc(sk, msg: &new->msg_encrypted, len: msg_opl->sg.size + |
| 623 | tx_overhead_size, elem_first_coalesce: 0); |
| 624 | if (ret < 0) { |
| 625 | tls_free_rec(sk, rec: new); |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | *orig_end = msg_opl->sg.end; |
| 630 | i = msg_opl->sg.start; |
| 631 | sge = sk_msg_elem(msg: msg_opl, which: i); |
| 632 | while (apply && sge->length) { |
| 633 | if (sge->length > apply) { |
| 634 | u32 len = sge->length - apply; |
| 635 | |
| 636 | get_page(page: sg_page(sg: sge)); |
| 637 | sg_set_page(sg: &tmp, page: sg_page(sg: sge), len, |
| 638 | offset: sge->offset + apply); |
| 639 | sge->length = apply; |
| 640 | bytes += apply; |
| 641 | apply = 0; |
| 642 | } else { |
| 643 | apply -= sge->length; |
| 644 | bytes += sge->length; |
| 645 | } |
| 646 | |
| 647 | sk_msg_iter_var_next(i); |
| 648 | if (i == msg_opl->sg.end) |
| 649 | break; |
| 650 | sge = sk_msg_elem(msg: msg_opl, which: i); |
| 651 | } |
| 652 | |
| 653 | msg_opl->sg.end = i; |
| 654 | msg_opl->sg.curr = i; |
| 655 | msg_opl->sg.copybreak = 0; |
| 656 | msg_opl->apply_bytes = 0; |
| 657 | msg_opl->sg.size = bytes; |
| 658 | |
| 659 | msg_npl = &new->msg_plaintext; |
| 660 | msg_npl->apply_bytes = apply; |
| 661 | msg_npl->sg.size = orig_size - bytes; |
| 662 | |
| 663 | j = msg_npl->sg.start; |
| 664 | nsge = sk_msg_elem(msg: msg_npl, which: j); |
| 665 | if (tmp.length) { |
| 666 | memcpy(nsge, &tmp, sizeof(*nsge)); |
| 667 | sk_msg_iter_var_next(j); |
| 668 | nsge = sk_msg_elem(msg: msg_npl, which: j); |
| 669 | } |
| 670 | |
| 671 | osge = sk_msg_elem(msg: msg_opl, which: i); |
| 672 | while (osge->length) { |
| 673 | memcpy(nsge, osge, sizeof(*nsge)); |
| 674 | sg_unmark_end(sg: nsge); |
| 675 | sk_msg_iter_var_next(i); |
| 676 | sk_msg_iter_var_next(j); |
| 677 | if (i == *orig_end) |
| 678 | break; |
| 679 | osge = sk_msg_elem(msg: msg_opl, which: i); |
| 680 | nsge = sk_msg_elem(msg: msg_npl, which: j); |
| 681 | } |
| 682 | |
| 683 | msg_npl->sg.end = j; |
| 684 | msg_npl->sg.curr = j; |
| 685 | msg_npl->sg.copybreak = 0; |
| 686 | |
| 687 | *to = new; |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static void tls_merge_open_record(struct sock *sk, struct tls_rec *to, |
| 692 | struct tls_rec *from, u32 orig_end) |
| 693 | { |
| 694 | struct sk_msg *msg_npl = &from->msg_plaintext; |
| 695 | struct sk_msg *msg_opl = &to->msg_plaintext; |
| 696 | struct scatterlist *osge, *nsge; |
| 697 | u32 i, j; |
| 698 | |
| 699 | i = msg_opl->sg.end; |
| 700 | sk_msg_iter_var_prev(i); |
| 701 | j = msg_npl->sg.start; |
| 702 | |
| 703 | osge = sk_msg_elem(msg: msg_opl, which: i); |
| 704 | nsge = sk_msg_elem(msg: msg_npl, which: j); |
| 705 | |
| 706 | if (sg_page(sg: osge) == sg_page(sg: nsge) && |
| 707 | osge->offset + osge->length == nsge->offset) { |
| 708 | osge->length += nsge->length; |
| 709 | put_page(page: sg_page(sg: nsge)); |
| 710 | } |
| 711 | |
| 712 | msg_opl->sg.end = orig_end; |
| 713 | msg_opl->sg.curr = orig_end; |
| 714 | msg_opl->sg.copybreak = 0; |
| 715 | msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size; |
| 716 | msg_opl->sg.size += msg_npl->sg.size; |
| 717 | |
| 718 | sk_msg_free(sk, msg: &to->msg_encrypted); |
| 719 | sk_msg_xfer_full(dst: &to->msg_encrypted, src: &from->msg_encrypted); |
| 720 | |
| 721 | kfree(objp: from); |
| 722 | } |
| 723 | |
| 724 | static int tls_push_record(struct sock *sk, int flags, |
| 725 | unsigned char record_type) |
| 726 | { |
| 727 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 728 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 729 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 730 | struct tls_rec *rec = ctx->open_rec, *tmp = NULL; |
| 731 | u32 i, split_point, orig_end; |
| 732 | struct sk_msg *msg_pl, *msg_en; |
| 733 | struct aead_request *req; |
| 734 | bool split; |
| 735 | int rc; |
| 736 | |
| 737 | if (!rec) |
| 738 | return 0; |
| 739 | |
| 740 | msg_pl = &rec->msg_plaintext; |
| 741 | msg_en = &rec->msg_encrypted; |
| 742 | |
| 743 | split_point = msg_pl->apply_bytes; |
| 744 | split = split_point && split_point < msg_pl->sg.size; |
| 745 | if (unlikely((!split && |
| 746 | msg_pl->sg.size + |
| 747 | prot->overhead_size > msg_en->sg.size) || |
| 748 | (split && |
| 749 | split_point + |
| 750 | prot->overhead_size > msg_en->sg.size))) { |
| 751 | split = true; |
| 752 | split_point = msg_en->sg.size; |
| 753 | } |
| 754 | if (split) { |
| 755 | rc = tls_split_open_record(sk, from: rec, to: &tmp, msg_opl: msg_pl, msg_oen: msg_en, |
| 756 | split_point, tx_overhead_size: prot->overhead_size, |
| 757 | orig_end: &orig_end); |
| 758 | if (rc < 0) |
| 759 | return rc; |
| 760 | /* This can happen if above tls_split_open_record allocates |
| 761 | * a single large encryption buffer instead of two smaller |
| 762 | * ones. In this case adjust pointers and continue without |
| 763 | * split. |
| 764 | */ |
| 765 | if (!msg_pl->sg.size) { |
| 766 | tls_merge_open_record(sk, to: rec, from: tmp, orig_end); |
| 767 | msg_pl = &rec->msg_plaintext; |
| 768 | msg_en = &rec->msg_encrypted; |
| 769 | split = false; |
| 770 | } |
| 771 | sk_msg_trim(sk, msg: msg_en, len: msg_pl->sg.size + |
| 772 | prot->overhead_size); |
| 773 | } |
| 774 | |
| 775 | rec->tx_flags = flags; |
| 776 | req = &rec->aead_req; |
| 777 | |
| 778 | i = msg_pl->sg.end; |
| 779 | sk_msg_iter_var_prev(i); |
| 780 | |
| 781 | rec->content_type = record_type; |
| 782 | if (prot->version == TLS_1_3_VERSION) { |
| 783 | /* Add content type to end of message. No padding added */ |
| 784 | sg_set_buf(sg: &rec->sg_content_type, buf: &rec->content_type, buflen: 1); |
| 785 | sg_mark_end(sg: &rec->sg_content_type); |
| 786 | sg_chain(prv: msg_pl->sg.data, prv_nents: msg_pl->sg.end + 1, |
| 787 | sgl: &rec->sg_content_type); |
| 788 | } else { |
| 789 | sg_mark_end(sg: sk_msg_elem(msg: msg_pl, which: i)); |
| 790 | } |
| 791 | |
| 792 | if (msg_pl->sg.end < msg_pl->sg.start) { |
| 793 | sg_chain(prv: &msg_pl->sg.data[msg_pl->sg.start], |
| 794 | MAX_SKB_FRAGS - msg_pl->sg.start + 1, |
| 795 | sgl: msg_pl->sg.data); |
| 796 | } |
| 797 | |
| 798 | i = msg_pl->sg.start; |
| 799 | sg_chain(prv: rec->sg_aead_in, prv_nents: 2, sgl: &msg_pl->sg.data[i]); |
| 800 | |
| 801 | i = msg_en->sg.end; |
| 802 | sk_msg_iter_var_prev(i); |
| 803 | sg_mark_end(sg: sk_msg_elem(msg: msg_en, which: i)); |
| 804 | |
| 805 | i = msg_en->sg.start; |
| 806 | sg_chain(prv: rec->sg_aead_out, prv_nents: 2, sgl: &msg_en->sg.data[i]); |
| 807 | |
| 808 | tls_make_aad(buf: rec->aad_space, size: msg_pl->sg.size + prot->tail_size, |
| 809 | record_sequence: tls_ctx->tx.rec_seq, record_type, prot); |
| 810 | |
| 811 | tls_fill_prepend(ctx: tls_ctx, |
| 812 | page_address(sg_page(&msg_en->sg.data[i])) + |
| 813 | msg_en->sg.data[i].offset, |
| 814 | plaintext_len: msg_pl->sg.size + prot->tail_size, |
| 815 | record_type); |
| 816 | |
| 817 | tls_ctx->pending_open_record_frags = false; |
| 818 | |
| 819 | rc = tls_do_encryption(sk, tls_ctx, ctx, aead_req: req, |
| 820 | data_len: msg_pl->sg.size + prot->tail_size, start: i); |
| 821 | if (rc < 0) { |
| 822 | if (rc != -EINPROGRESS) { |
| 823 | tls_err_abort(sk, err: -EBADMSG); |
| 824 | if (split) { |
| 825 | tls_ctx->pending_open_record_frags = true; |
| 826 | tls_merge_open_record(sk, to: rec, from: tmp, orig_end); |
| 827 | } |
| 828 | } |
| 829 | ctx->async_capable = 1; |
| 830 | return rc; |
| 831 | } else if (split) { |
| 832 | msg_pl = &tmp->msg_plaintext; |
| 833 | msg_en = &tmp->msg_encrypted; |
| 834 | sk_msg_trim(sk, msg: msg_en, len: msg_pl->sg.size + prot->overhead_size); |
| 835 | tls_ctx->pending_open_record_frags = true; |
| 836 | ctx->open_rec = tmp; |
| 837 | } |
| 838 | |
| 839 | return tls_tx_records(sk, flags); |
| 840 | } |
| 841 | |
| 842 | static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk, |
| 843 | bool full_record, u8 record_type, |
| 844 | ssize_t *copied, int flags) |
| 845 | { |
| 846 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 847 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 848 | struct sk_msg msg_redir = { }; |
| 849 | struct sk_psock *psock; |
| 850 | struct sock *sk_redir; |
| 851 | struct tls_rec *rec; |
| 852 | bool enospc, policy, redir_ingress; |
| 853 | int err = 0, send; |
| 854 | u32 delta = 0; |
| 855 | |
| 856 | policy = !(flags & MSG_SENDPAGE_NOPOLICY); |
| 857 | psock = sk_psock_get(sk); |
| 858 | if (!psock || !policy) { |
| 859 | err = tls_push_record(sk, flags, record_type); |
| 860 | if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) { |
| 861 | *copied -= sk_msg_free(sk, msg); |
| 862 | tls_free_open_rec(sk); |
| 863 | err = -sk->sk_err; |
| 864 | } |
| 865 | if (psock) |
| 866 | sk_psock_put(sk, psock); |
| 867 | return err; |
| 868 | } |
| 869 | more_data: |
| 870 | enospc = sk_msg_full(msg); |
| 871 | if (psock->eval == __SK_NONE) { |
| 872 | delta = msg->sg.size; |
| 873 | psock->eval = sk_psock_msg_verdict(sk, psock, msg); |
| 874 | delta -= msg->sg.size; |
| 875 | |
| 876 | if ((s32)delta > 0) { |
| 877 | /* It indicates that we executed bpf_msg_pop_data(), |
| 878 | * causing the plaintext data size to decrease. |
| 879 | * Therefore the encrypted data size also needs to |
| 880 | * correspondingly decrease. We only need to subtract |
| 881 | * delta to calculate the new ciphertext length since |
| 882 | * ktls does not support block encryption. |
| 883 | */ |
| 884 | struct sk_msg *enc = &ctx->open_rec->msg_encrypted; |
| 885 | |
| 886 | sk_msg_trim(sk, msg: enc, len: enc->sg.size - delta); |
| 887 | } |
| 888 | } |
| 889 | if (msg->cork_bytes && msg->cork_bytes > msg->sg.size && |
| 890 | !enospc && !full_record) { |
| 891 | err = -ENOSPC; |
| 892 | goto out_err; |
| 893 | } |
| 894 | msg->cork_bytes = 0; |
| 895 | send = msg->sg.size; |
| 896 | if (msg->apply_bytes && msg->apply_bytes < send) |
| 897 | send = msg->apply_bytes; |
| 898 | |
| 899 | switch (psock->eval) { |
| 900 | case __SK_PASS: |
| 901 | err = tls_push_record(sk, flags, record_type); |
| 902 | if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) { |
| 903 | *copied -= sk_msg_free(sk, msg); |
| 904 | tls_free_open_rec(sk); |
| 905 | err = -sk->sk_err; |
| 906 | goto out_err; |
| 907 | } |
| 908 | break; |
| 909 | case __SK_REDIRECT: |
| 910 | redir_ingress = psock->redir_ingress; |
| 911 | sk_redir = psock->sk_redir; |
| 912 | memcpy(&msg_redir, msg, sizeof(*msg)); |
| 913 | if (msg->apply_bytes < send) |
| 914 | msg->apply_bytes = 0; |
| 915 | else |
| 916 | msg->apply_bytes -= send; |
| 917 | sk_msg_return_zero(sk, msg, bytes: send); |
| 918 | msg->sg.size -= send; |
| 919 | release_sock(sk); |
| 920 | err = tcp_bpf_sendmsg_redir(sk: sk_redir, ingress: redir_ingress, |
| 921 | msg: &msg_redir, bytes: send, flags); |
| 922 | lock_sock(sk); |
| 923 | if (err < 0) { |
| 924 | /* Regardless of whether the data represented by |
| 925 | * msg_redir is sent successfully, we have already |
| 926 | * uncharged it via sk_msg_return_zero(). The |
| 927 | * msg->sg.size represents the remaining unprocessed |
| 928 | * data, which needs to be uncharged here. |
| 929 | */ |
| 930 | sk_mem_uncharge(sk, size: msg->sg.size); |
| 931 | *copied -= sk_msg_free_nocharge(sk, msg: &msg_redir); |
| 932 | msg->sg.size = 0; |
| 933 | } |
| 934 | if (msg->sg.size == 0) |
| 935 | tls_free_open_rec(sk); |
| 936 | break; |
| 937 | case __SK_DROP: |
| 938 | default: |
| 939 | sk_msg_free_partial(sk, msg, bytes: send); |
| 940 | if (msg->apply_bytes < send) |
| 941 | msg->apply_bytes = 0; |
| 942 | else |
| 943 | msg->apply_bytes -= send; |
| 944 | if (msg->sg.size == 0) |
| 945 | tls_free_open_rec(sk); |
| 946 | *copied -= (send + delta); |
| 947 | err = -EACCES; |
| 948 | } |
| 949 | |
| 950 | if (likely(!err)) { |
| 951 | bool reset_eval = !ctx->open_rec; |
| 952 | |
| 953 | rec = ctx->open_rec; |
| 954 | if (rec) { |
| 955 | msg = &rec->msg_plaintext; |
| 956 | if (!msg->apply_bytes) |
| 957 | reset_eval = true; |
| 958 | } |
| 959 | if (reset_eval) { |
| 960 | psock->eval = __SK_NONE; |
| 961 | if (psock->sk_redir) { |
| 962 | sock_put(sk: psock->sk_redir); |
| 963 | psock->sk_redir = NULL; |
| 964 | } |
| 965 | } |
| 966 | if (rec) |
| 967 | goto more_data; |
| 968 | } |
| 969 | out_err: |
| 970 | sk_psock_put(sk, psock); |
| 971 | return err; |
| 972 | } |
| 973 | |
| 974 | static int tls_sw_push_pending_record(struct sock *sk, int flags) |
| 975 | { |
| 976 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 977 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 978 | struct tls_rec *rec = ctx->open_rec; |
| 979 | struct sk_msg *msg_pl; |
| 980 | size_t copied; |
| 981 | |
| 982 | if (!rec) |
| 983 | return 0; |
| 984 | |
| 985 | msg_pl = &rec->msg_plaintext; |
| 986 | copied = msg_pl->sg.size; |
| 987 | if (!copied) |
| 988 | return 0; |
| 989 | |
| 990 | return bpf_exec_tx_verdict(msg: msg_pl, sk, full_record: true, record_type: TLS_RECORD_TYPE_DATA, |
| 991 | copied: &copied, flags); |
| 992 | } |
| 993 | |
| 994 | static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg, |
| 995 | struct sk_msg *msg_pl, size_t try_to_copy, |
| 996 | ssize_t *copied) |
| 997 | { |
| 998 | struct page *page = NULL, **pages = &page; |
| 999 | |
| 1000 | do { |
| 1001 | ssize_t part; |
| 1002 | size_t off; |
| 1003 | |
| 1004 | part = iov_iter_extract_pages(i: &msg->msg_iter, pages: &pages, |
| 1005 | maxsize: try_to_copy, maxpages: 1, extraction_flags: 0, offset0: &off); |
| 1006 | if (part <= 0) |
| 1007 | return part ?: -EIO; |
| 1008 | |
| 1009 | if (WARN_ON_ONCE(!sendpage_ok(page))) { |
| 1010 | iov_iter_revert(i: &msg->msg_iter, bytes: part); |
| 1011 | return -EIO; |
| 1012 | } |
| 1013 | |
| 1014 | sk_msg_page_add(msg: msg_pl, page, len: part, offset: off); |
| 1015 | msg_pl->sg.copybreak = 0; |
| 1016 | msg_pl->sg.curr = msg_pl->sg.end; |
| 1017 | sk_mem_charge(sk, size: part); |
| 1018 | *copied += part; |
| 1019 | try_to_copy -= part; |
| 1020 | } while (try_to_copy && !sk_msg_full(msg: msg_pl)); |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg, |
| 1026 | size_t size) |
| 1027 | { |
| 1028 | long timeo = sock_sndtimeo(sk, noblock: msg->msg_flags & MSG_DONTWAIT); |
| 1029 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1030 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1031 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 1032 | bool async_capable = ctx->async_capable; |
| 1033 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 1034 | bool is_kvec = iov_iter_is_kvec(i: &msg->msg_iter); |
| 1035 | bool eor = !(msg->msg_flags & MSG_MORE); |
| 1036 | size_t try_to_copy; |
| 1037 | ssize_t copied = 0; |
| 1038 | struct sk_msg *msg_pl, *msg_en; |
| 1039 | struct tls_rec *rec; |
| 1040 | int required_size; |
| 1041 | int num_async = 0; |
| 1042 | bool full_record; |
| 1043 | int record_room; |
| 1044 | int num_zc = 0; |
| 1045 | int orig_size; |
| 1046 | int ret = 0; |
| 1047 | |
| 1048 | if (!eor && (msg->msg_flags & MSG_EOR)) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | if (unlikely(msg->msg_controllen)) { |
| 1052 | ret = tls_process_cmsg(sk, msg, record_type: &record_type); |
| 1053 | if (ret) { |
| 1054 | if (ret == -EINPROGRESS) |
| 1055 | num_async++; |
| 1056 | else if (ret != -EAGAIN) |
| 1057 | goto end; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | while (msg_data_left(msg)) { |
| 1062 | if (sk->sk_err) { |
| 1063 | ret = -sk->sk_err; |
| 1064 | goto send_end; |
| 1065 | } |
| 1066 | |
| 1067 | if (ctx->open_rec) |
| 1068 | rec = ctx->open_rec; |
| 1069 | else |
| 1070 | rec = ctx->open_rec = tls_get_rec(sk); |
| 1071 | if (!rec) { |
| 1072 | ret = -ENOMEM; |
| 1073 | goto send_end; |
| 1074 | } |
| 1075 | |
| 1076 | msg_pl = &rec->msg_plaintext; |
| 1077 | msg_en = &rec->msg_encrypted; |
| 1078 | |
| 1079 | orig_size = msg_pl->sg.size; |
| 1080 | full_record = false; |
| 1081 | try_to_copy = msg_data_left(msg); |
| 1082 | record_room = tls_ctx->tx_max_payload_len - msg_pl->sg.size; |
| 1083 | if (try_to_copy >= record_room) { |
| 1084 | try_to_copy = record_room; |
| 1085 | full_record = true; |
| 1086 | } |
| 1087 | |
| 1088 | required_size = msg_pl->sg.size + try_to_copy + |
| 1089 | prot->overhead_size; |
| 1090 | |
| 1091 | if (!sk_stream_memory_free(sk)) |
| 1092 | goto wait_for_sndbuf; |
| 1093 | |
| 1094 | alloc_encrypted: |
| 1095 | ret = tls_alloc_encrypted_msg(sk, len: required_size); |
| 1096 | if (ret) { |
| 1097 | if (ret != -ENOSPC) |
| 1098 | goto wait_for_memory; |
| 1099 | |
| 1100 | /* Adjust try_to_copy according to the amount that was |
| 1101 | * actually allocated. The difference is due |
| 1102 | * to max sg elements limit |
| 1103 | */ |
| 1104 | try_to_copy -= required_size - msg_en->sg.size; |
| 1105 | full_record = true; |
| 1106 | } |
| 1107 | |
| 1108 | if (try_to_copy && (msg->msg_flags & MSG_SPLICE_PAGES)) { |
| 1109 | ret = tls_sw_sendmsg_splice(sk, msg, msg_pl, |
| 1110 | try_to_copy, copied: &copied); |
| 1111 | if (ret < 0) |
| 1112 | goto send_end; |
| 1113 | tls_ctx->pending_open_record_frags = true; |
| 1114 | |
| 1115 | if (sk_msg_full(msg: msg_pl)) { |
| 1116 | full_record = true; |
| 1117 | sk_msg_trim(sk, msg: msg_en, |
| 1118 | len: msg_pl->sg.size + prot->overhead_size); |
| 1119 | } |
| 1120 | |
| 1121 | if (full_record || eor) |
| 1122 | goto copied; |
| 1123 | continue; |
| 1124 | } |
| 1125 | |
| 1126 | if (!is_kvec && (full_record || eor) && !async_capable) { |
| 1127 | u32 first = msg_pl->sg.end; |
| 1128 | |
| 1129 | ret = sk_msg_zerocopy_from_iter(sk, from: &msg->msg_iter, |
| 1130 | msg: msg_pl, bytes: try_to_copy); |
| 1131 | if (ret) |
| 1132 | goto fallback_to_reg_send; |
| 1133 | |
| 1134 | num_zc++; |
| 1135 | copied += try_to_copy; |
| 1136 | |
| 1137 | sk_msg_sg_copy_set(msg: msg_pl, start: first); |
| 1138 | ret = bpf_exec_tx_verdict(msg: msg_pl, sk, full_record, |
| 1139 | record_type, copied: &copied, |
| 1140 | flags: msg->msg_flags); |
| 1141 | if (ret) { |
| 1142 | if (ret == -EINPROGRESS) |
| 1143 | num_async++; |
| 1144 | else if (ret == -ENOMEM) |
| 1145 | goto wait_for_memory; |
| 1146 | else if (ctx->open_rec && ret == -ENOSPC) { |
| 1147 | if (msg_pl->cork_bytes) { |
| 1148 | ret = 0; |
| 1149 | goto send_end; |
| 1150 | } |
| 1151 | goto rollback_iter; |
| 1152 | } else if (ret != -EAGAIN) |
| 1153 | goto send_end; |
| 1154 | } |
| 1155 | |
| 1156 | /* Transmit if any encryptions have completed */ |
| 1157 | if (test_and_clear_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) { |
| 1158 | cancel_delayed_work(dwork: &ctx->tx_work.work); |
| 1159 | tls_tx_records(sk, flags: msg->msg_flags); |
| 1160 | } |
| 1161 | |
| 1162 | continue; |
| 1163 | rollback_iter: |
| 1164 | copied -= try_to_copy; |
| 1165 | sk_msg_sg_copy_clear(msg: msg_pl, start: first); |
| 1166 | iov_iter_revert(i: &msg->msg_iter, |
| 1167 | bytes: msg_pl->sg.size - orig_size); |
| 1168 | fallback_to_reg_send: |
| 1169 | sk_msg_trim(sk, msg: msg_pl, len: orig_size); |
| 1170 | } |
| 1171 | |
| 1172 | required_size = msg_pl->sg.size + try_to_copy; |
| 1173 | |
| 1174 | ret = tls_clone_plaintext_msg(sk, required: required_size); |
| 1175 | if (ret) { |
| 1176 | if (ret != -ENOSPC) |
| 1177 | goto send_end; |
| 1178 | |
| 1179 | /* Adjust try_to_copy according to the amount that was |
| 1180 | * actually allocated. The difference is due |
| 1181 | * to max sg elements limit |
| 1182 | */ |
| 1183 | try_to_copy -= required_size - msg_pl->sg.size; |
| 1184 | full_record = true; |
| 1185 | sk_msg_trim(sk, msg: msg_en, |
| 1186 | len: msg_pl->sg.size + prot->overhead_size); |
| 1187 | } |
| 1188 | |
| 1189 | if (try_to_copy) { |
| 1190 | ret = sk_msg_memcopy_from_iter(sk, from: &msg->msg_iter, |
| 1191 | msg: msg_pl, bytes: try_to_copy); |
| 1192 | if (ret < 0) |
| 1193 | goto trim_sgl; |
| 1194 | } |
| 1195 | |
| 1196 | /* Open records defined only if successfully copied, otherwise |
| 1197 | * we would trim the sg but not reset the open record frags. |
| 1198 | */ |
| 1199 | tls_ctx->pending_open_record_frags = true; |
| 1200 | copied += try_to_copy; |
| 1201 | copied: |
| 1202 | if (full_record || eor) { |
| 1203 | ret = bpf_exec_tx_verdict(msg: msg_pl, sk, full_record, |
| 1204 | record_type, copied: &copied, |
| 1205 | flags: msg->msg_flags); |
| 1206 | if (ret) { |
| 1207 | if (ret == -EINPROGRESS) |
| 1208 | num_async++; |
| 1209 | else if (ret == -ENOMEM) |
| 1210 | goto wait_for_memory; |
| 1211 | else if (ret != -EAGAIN) { |
| 1212 | if (ret == -ENOSPC) |
| 1213 | ret = 0; |
| 1214 | goto send_end; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | /* Transmit if any encryptions have completed */ |
| 1219 | if (test_and_clear_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) { |
| 1220 | cancel_delayed_work(dwork: &ctx->tx_work.work); |
| 1221 | tls_tx_records(sk, flags: msg->msg_flags); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | continue; |
| 1226 | |
| 1227 | wait_for_sndbuf: |
| 1228 | set_bit(nr: SOCK_NOSPACE, addr: &sk->sk_socket->flags); |
| 1229 | wait_for_memory: |
| 1230 | ret = sk_stream_wait_memory(sk, timeo_p: &timeo); |
| 1231 | if (ret) { |
| 1232 | trim_sgl: |
| 1233 | if (ctx->open_rec) |
| 1234 | tls_trim_both_msgs(sk, target_size: orig_size); |
| 1235 | goto send_end; |
| 1236 | } |
| 1237 | |
| 1238 | if (ctx->open_rec && msg_en->sg.size < required_size) |
| 1239 | goto alloc_encrypted; |
| 1240 | } |
| 1241 | |
| 1242 | send_end: |
| 1243 | if (!num_async) { |
| 1244 | goto end; |
| 1245 | } else if (num_zc || eor) { |
| 1246 | int err; |
| 1247 | |
| 1248 | /* Wait for pending encryptions to get completed */ |
| 1249 | err = tls_encrypt_async_wait(ctx); |
| 1250 | if (err) { |
| 1251 | ret = err; |
| 1252 | copied = 0; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* Transmit if any encryptions have completed */ |
| 1257 | if (test_and_clear_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) { |
| 1258 | cancel_delayed_work(dwork: &ctx->tx_work.work); |
| 1259 | tls_tx_records(sk, flags: msg->msg_flags); |
| 1260 | } |
| 1261 | |
| 1262 | end: |
| 1263 | ret = sk_stream_error(sk, flags: msg->msg_flags, err: ret); |
| 1264 | return copied > 0 ? copied : ret; |
| 1265 | } |
| 1266 | |
| 1267 | int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
| 1268 | { |
| 1269 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1270 | int ret; |
| 1271 | |
| 1272 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 1273 | MSG_CMSG_COMPAT | MSG_SPLICE_PAGES | MSG_EOR | |
| 1274 | MSG_SENDPAGE_NOPOLICY)) |
| 1275 | return -EOPNOTSUPP; |
| 1276 | |
| 1277 | ret = mutex_lock_interruptible(&tls_ctx->tx_lock); |
| 1278 | if (ret) |
| 1279 | return ret; |
| 1280 | lock_sock(sk); |
| 1281 | ret = tls_sw_sendmsg_locked(sk, msg, size); |
| 1282 | release_sock(sk); |
| 1283 | mutex_unlock(lock: &tls_ctx->tx_lock); |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * Handle unexpected EOF during splice without SPLICE_F_MORE set. |
| 1289 | */ |
| 1290 | void tls_sw_splice_eof(struct socket *sock) |
| 1291 | { |
| 1292 | struct sock *sk = sock->sk; |
| 1293 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1294 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 1295 | struct tls_rec *rec; |
| 1296 | struct sk_msg *msg_pl; |
| 1297 | ssize_t copied = 0; |
| 1298 | bool retrying = false; |
| 1299 | int ret = 0; |
| 1300 | |
| 1301 | if (!ctx->open_rec) |
| 1302 | return; |
| 1303 | |
| 1304 | mutex_lock(&tls_ctx->tx_lock); |
| 1305 | lock_sock(sk); |
| 1306 | |
| 1307 | retry: |
| 1308 | /* same checks as in tls_sw_push_pending_record() */ |
| 1309 | rec = ctx->open_rec; |
| 1310 | if (!rec) |
| 1311 | goto unlock; |
| 1312 | |
| 1313 | msg_pl = &rec->msg_plaintext; |
| 1314 | if (msg_pl->sg.size == 0) |
| 1315 | goto unlock; |
| 1316 | |
| 1317 | /* Check the BPF advisor and perform transmission. */ |
| 1318 | ret = bpf_exec_tx_verdict(msg: msg_pl, sk, full_record: false, record_type: TLS_RECORD_TYPE_DATA, |
| 1319 | copied: &copied, flags: 0); |
| 1320 | switch (ret) { |
| 1321 | case 0: |
| 1322 | case -EAGAIN: |
| 1323 | if (retrying) |
| 1324 | goto unlock; |
| 1325 | retrying = true; |
| 1326 | goto retry; |
| 1327 | case -EINPROGRESS: |
| 1328 | break; |
| 1329 | default: |
| 1330 | goto unlock; |
| 1331 | } |
| 1332 | |
| 1333 | /* Wait for pending encryptions to get completed */ |
| 1334 | if (tls_encrypt_async_wait(ctx)) |
| 1335 | goto unlock; |
| 1336 | |
| 1337 | /* Transmit if any encryptions have completed */ |
| 1338 | if (test_and_clear_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) { |
| 1339 | cancel_delayed_work(dwork: &ctx->tx_work.work); |
| 1340 | tls_tx_records(sk, flags: 0); |
| 1341 | } |
| 1342 | |
| 1343 | unlock: |
| 1344 | release_sock(sk); |
| 1345 | mutex_unlock(lock: &tls_ctx->tx_lock); |
| 1346 | } |
| 1347 | |
| 1348 | static int |
| 1349 | tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock, |
| 1350 | bool released) |
| 1351 | { |
| 1352 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1353 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1354 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 1355 | int ret = 0; |
| 1356 | long timeo; |
| 1357 | |
| 1358 | /* a rekey is pending, let userspace deal with it */ |
| 1359 | if (unlikely(ctx->key_update_pending)) |
| 1360 | return -EKEYEXPIRED; |
| 1361 | |
| 1362 | timeo = sock_rcvtimeo(sk, noblock: nonblock); |
| 1363 | |
| 1364 | while (!tls_strp_msg_ready(ctx)) { |
| 1365 | if (!sk_psock_queue_empty(psock)) |
| 1366 | return 0; |
| 1367 | |
| 1368 | if (sk->sk_err) |
| 1369 | return sock_error(sk); |
| 1370 | |
| 1371 | if (ret < 0) |
| 1372 | return ret; |
| 1373 | |
| 1374 | if (!skb_queue_empty(list: &sk->sk_receive_queue)) { |
| 1375 | tls_strp_check_rcv(strp: &ctx->strp); |
| 1376 | if (tls_strp_msg_ready(ctx)) |
| 1377 | break; |
| 1378 | } |
| 1379 | |
| 1380 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1381 | return 0; |
| 1382 | |
| 1383 | if (sock_flag(sk, flag: SOCK_DONE)) |
| 1384 | return 0; |
| 1385 | |
| 1386 | if (!timeo) |
| 1387 | return -EAGAIN; |
| 1388 | |
| 1389 | released = true; |
| 1390 | add_wait_queue(wq_head: sk_sleep(sk), wq_entry: &wait); |
| 1391 | sk_set_bit(nr: SOCKWQ_ASYNC_WAITDATA, sk); |
| 1392 | ret = sk_wait_event(sk, &timeo, |
| 1393 | tls_strp_msg_ready(ctx) || |
| 1394 | !sk_psock_queue_empty(psock), |
| 1395 | &wait); |
| 1396 | sk_clear_bit(nr: SOCKWQ_ASYNC_WAITDATA, sk); |
| 1397 | remove_wait_queue(wq_head: sk_sleep(sk), wq_entry: &wait); |
| 1398 | |
| 1399 | /* Handle signals */ |
| 1400 | if (signal_pending(current)) |
| 1401 | return sock_intr_errno(timeo); |
| 1402 | } |
| 1403 | |
| 1404 | if (unlikely(!tls_strp_msg_load(&ctx->strp, released))) |
| 1405 | return tls_rx_rec_wait(sk, psock, nonblock, released: false); |
| 1406 | |
| 1407 | return 1; |
| 1408 | } |
| 1409 | |
| 1410 | static int tls_setup_from_iter(struct iov_iter *from, |
| 1411 | int length, int *pages_used, |
| 1412 | struct scatterlist *to, |
| 1413 | int to_max_pages) |
| 1414 | { |
| 1415 | int rc = 0, i = 0, num_elem = *pages_used, maxpages; |
| 1416 | struct page *pages[MAX_SKB_FRAGS]; |
| 1417 | unsigned int size = 0; |
| 1418 | ssize_t copied, use; |
| 1419 | size_t offset; |
| 1420 | |
| 1421 | while (length > 0) { |
| 1422 | i = 0; |
| 1423 | maxpages = to_max_pages - num_elem; |
| 1424 | if (maxpages == 0) { |
| 1425 | rc = -EFAULT; |
| 1426 | goto out; |
| 1427 | } |
| 1428 | copied = iov_iter_get_pages2(i: from, pages, |
| 1429 | maxsize: length, |
| 1430 | maxpages, start: &offset); |
| 1431 | if (copied <= 0) { |
| 1432 | rc = -EFAULT; |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | length -= copied; |
| 1437 | size += copied; |
| 1438 | while (copied) { |
| 1439 | use = min_t(int, copied, PAGE_SIZE - offset); |
| 1440 | |
| 1441 | sg_set_page(sg: &to[num_elem], |
| 1442 | page: pages[i], len: use, offset); |
| 1443 | sg_unmark_end(sg: &to[num_elem]); |
| 1444 | /* We do not uncharge memory from this API */ |
| 1445 | |
| 1446 | offset = 0; |
| 1447 | copied -= use; |
| 1448 | |
| 1449 | i++; |
| 1450 | num_elem++; |
| 1451 | } |
| 1452 | } |
| 1453 | /* Mark the end in the last sg entry if newly added */ |
| 1454 | if (num_elem > *pages_used) |
| 1455 | sg_mark_end(sg: &to[num_elem - 1]); |
| 1456 | out: |
| 1457 | if (rc) |
| 1458 | iov_iter_revert(i: from, bytes: size); |
| 1459 | *pages_used = num_elem; |
| 1460 | |
| 1461 | return rc; |
| 1462 | } |
| 1463 | |
| 1464 | static struct sk_buff * |
| 1465 | tls_alloc_clrtxt_skb(struct sock *sk, struct sk_buff *skb, |
| 1466 | unsigned int full_len) |
| 1467 | { |
| 1468 | struct strp_msg *clr_rxm; |
| 1469 | struct sk_buff *clr_skb; |
| 1470 | int err; |
| 1471 | |
| 1472 | clr_skb = alloc_skb_with_frags(header_len: 0, data_len: full_len, TLS_PAGE_ORDER, |
| 1473 | errcode: &err, gfp_mask: sk->sk_allocation); |
| 1474 | if (!clr_skb) |
| 1475 | return NULL; |
| 1476 | |
| 1477 | skb_copy_header(new: clr_skb, old: skb); |
| 1478 | clr_skb->len = full_len; |
| 1479 | clr_skb->data_len = full_len; |
| 1480 | |
| 1481 | clr_rxm = strp_msg(skb: clr_skb); |
| 1482 | clr_rxm->offset = 0; |
| 1483 | |
| 1484 | return clr_skb; |
| 1485 | } |
| 1486 | |
| 1487 | /* Decrypt handlers |
| 1488 | * |
| 1489 | * tls_decrypt_sw() and tls_decrypt_device() are decrypt handlers. |
| 1490 | * They must transform the darg in/out argument are as follows: |
| 1491 | * | Input | Output |
| 1492 | * ------------------------------------------------------------------- |
| 1493 | * zc | Zero-copy decrypt allowed | Zero-copy performed |
| 1494 | * async | Async decrypt allowed | Async crypto used / in progress |
| 1495 | * skb | * | Output skb |
| 1496 | * |
| 1497 | * If ZC decryption was performed darg.skb will point to the input skb. |
| 1498 | */ |
| 1499 | |
| 1500 | /* This function decrypts the input skb into either out_iov or in out_sg |
| 1501 | * or in skb buffers itself. The input parameter 'darg->zc' indicates if |
| 1502 | * zero-copy mode needs to be tried or not. With zero-copy mode, either |
| 1503 | * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are |
| 1504 | * NULL, then the decryption happens inside skb buffers itself, i.e. |
| 1505 | * zero-copy gets disabled and 'darg->zc' is updated. |
| 1506 | */ |
| 1507 | static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov, |
| 1508 | struct scatterlist *out_sg, |
| 1509 | struct tls_decrypt_arg *darg) |
| 1510 | { |
| 1511 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1512 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1513 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1514 | int n_sgin, n_sgout, aead_size, err, pages = 0; |
| 1515 | struct sk_buff *skb = tls_strp_msg(ctx); |
| 1516 | const struct strp_msg *rxm = strp_msg(skb); |
| 1517 | const struct tls_msg *tlm = tls_msg(skb); |
| 1518 | struct aead_request *aead_req; |
| 1519 | struct scatterlist *sgin = NULL; |
| 1520 | struct scatterlist *sgout = NULL; |
| 1521 | const int data_len = rxm->full_len - prot->overhead_size; |
| 1522 | int tail_pages = !!prot->tail_size; |
| 1523 | struct tls_decrypt_ctx *dctx; |
| 1524 | struct sk_buff *clear_skb; |
| 1525 | int iv_offset = 0; |
| 1526 | u8 *mem; |
| 1527 | |
| 1528 | n_sgin = skb_nsg(skb, offset: rxm->offset + prot->prepend_size, |
| 1529 | len: rxm->full_len - prot->prepend_size); |
| 1530 | if (n_sgin < 1) |
| 1531 | return n_sgin ?: -EBADMSG; |
| 1532 | |
| 1533 | if (darg->zc && (out_iov || out_sg)) { |
| 1534 | clear_skb = NULL; |
| 1535 | |
| 1536 | if (out_iov) |
| 1537 | n_sgout = 1 + tail_pages + |
| 1538 | iov_iter_npages_cap(i: out_iov, INT_MAX, max_bytes: data_len); |
| 1539 | else |
| 1540 | n_sgout = sg_nents(sg: out_sg); |
| 1541 | } else { |
| 1542 | darg->zc = false; |
| 1543 | |
| 1544 | clear_skb = tls_alloc_clrtxt_skb(sk, skb, full_len: rxm->full_len); |
| 1545 | if (!clear_skb) |
| 1546 | return -ENOMEM; |
| 1547 | |
| 1548 | n_sgout = 1 + skb_shinfo(clear_skb)->nr_frags; |
| 1549 | } |
| 1550 | |
| 1551 | /* Increment to accommodate AAD */ |
| 1552 | n_sgin = n_sgin + 1; |
| 1553 | |
| 1554 | /* Allocate a single block of memory which contains |
| 1555 | * aead_req || tls_decrypt_ctx. |
| 1556 | * Both structs are variable length. |
| 1557 | */ |
| 1558 | aead_size = sizeof(*aead_req) + crypto_aead_reqsize(tfm: ctx->aead_recv); |
| 1559 | aead_size = ALIGN(aead_size, __alignof__(*dctx)); |
| 1560 | mem = kmalloc(aead_size + struct_size(dctx, sg, size_add(n_sgin, n_sgout)), |
| 1561 | sk->sk_allocation); |
| 1562 | if (!mem) { |
| 1563 | err = -ENOMEM; |
| 1564 | goto exit_free_skb; |
| 1565 | } |
| 1566 | |
| 1567 | /* Segment the allocated memory */ |
| 1568 | aead_req = (struct aead_request *)mem; |
| 1569 | dctx = (struct tls_decrypt_ctx *)(mem + aead_size); |
| 1570 | dctx->sk = sk; |
| 1571 | sgin = &dctx->sg[0]; |
| 1572 | sgout = &dctx->sg[n_sgin]; |
| 1573 | |
| 1574 | /* For CCM based ciphers, first byte of nonce+iv is a constant */ |
| 1575 | switch (prot->cipher_type) { |
| 1576 | case TLS_CIPHER_AES_CCM_128: |
| 1577 | dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE; |
| 1578 | iv_offset = 1; |
| 1579 | break; |
| 1580 | case TLS_CIPHER_SM4_CCM: |
| 1581 | dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE; |
| 1582 | iv_offset = 1; |
| 1583 | break; |
| 1584 | } |
| 1585 | |
| 1586 | /* Prepare IV */ |
| 1587 | if (prot->version == TLS_1_3_VERSION || |
| 1588 | prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) { |
| 1589 | memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, |
| 1590 | prot->iv_size + prot->salt_size); |
| 1591 | } else { |
| 1592 | err = skb_copy_bits(skb, offset: rxm->offset + TLS_HEADER_SIZE, |
| 1593 | to: &dctx->iv[iv_offset] + prot->salt_size, |
| 1594 | len: prot->iv_size); |
| 1595 | if (err < 0) |
| 1596 | goto exit_free; |
| 1597 | memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size); |
| 1598 | } |
| 1599 | tls_xor_iv_with_seq(prot, iv: &dctx->iv[iv_offset], seq: tls_ctx->rx.rec_seq); |
| 1600 | |
| 1601 | /* Prepare AAD */ |
| 1602 | tls_make_aad(buf: dctx->aad, size: rxm->full_len - prot->overhead_size + |
| 1603 | prot->tail_size, |
| 1604 | record_sequence: tls_ctx->rx.rec_seq, record_type: tlm->control, prot); |
| 1605 | |
| 1606 | /* Prepare sgin */ |
| 1607 | sg_init_table(sgin, n_sgin); |
| 1608 | sg_set_buf(sg: &sgin[0], buf: dctx->aad, buflen: prot->aad_size); |
| 1609 | err = skb_to_sgvec(skb, sg: &sgin[1], |
| 1610 | offset: rxm->offset + prot->prepend_size, |
| 1611 | len: rxm->full_len - prot->prepend_size); |
| 1612 | if (err < 0) |
| 1613 | goto exit_free; |
| 1614 | |
| 1615 | if (clear_skb) { |
| 1616 | sg_init_table(sgout, n_sgout); |
| 1617 | sg_set_buf(sg: &sgout[0], buf: dctx->aad, buflen: prot->aad_size); |
| 1618 | |
| 1619 | err = skb_to_sgvec(skb: clear_skb, sg: &sgout[1], offset: prot->prepend_size, |
| 1620 | len: data_len + prot->tail_size); |
| 1621 | if (err < 0) |
| 1622 | goto exit_free; |
| 1623 | } else if (out_iov) { |
| 1624 | sg_init_table(sgout, n_sgout); |
| 1625 | sg_set_buf(sg: &sgout[0], buf: dctx->aad, buflen: prot->aad_size); |
| 1626 | |
| 1627 | err = tls_setup_from_iter(from: out_iov, length: data_len, pages_used: &pages, to: &sgout[1], |
| 1628 | to_max_pages: (n_sgout - 1 - tail_pages)); |
| 1629 | if (err < 0) |
| 1630 | goto exit_free_pages; |
| 1631 | |
| 1632 | if (prot->tail_size) { |
| 1633 | sg_unmark_end(sg: &sgout[pages]); |
| 1634 | sg_set_buf(sg: &sgout[pages + 1], buf: &dctx->tail, |
| 1635 | buflen: prot->tail_size); |
| 1636 | sg_mark_end(sg: &sgout[pages + 1]); |
| 1637 | } |
| 1638 | } else if (out_sg) { |
| 1639 | memcpy(sgout, out_sg, n_sgout * sizeof(*sgout)); |
| 1640 | } |
| 1641 | dctx->free_sgout = !!pages; |
| 1642 | |
| 1643 | /* Prepare and submit AEAD request */ |
| 1644 | err = tls_do_decryption(sk, sgin, sgout, iv_recv: dctx->iv, |
| 1645 | data_len: data_len + prot->tail_size, aead_req, darg); |
| 1646 | if (err) { |
| 1647 | if (darg->async_done) |
| 1648 | goto exit_free_skb; |
| 1649 | goto exit_free_pages; |
| 1650 | } |
| 1651 | |
| 1652 | darg->skb = clear_skb ?: tls_strp_msg(ctx); |
| 1653 | clear_skb = NULL; |
| 1654 | |
| 1655 | if (unlikely(darg->async)) { |
| 1656 | err = tls_strp_msg_hold(strp: &ctx->strp, dst: &ctx->async_hold); |
| 1657 | if (err) { |
| 1658 | err = tls_decrypt_async_wait(ctx); |
| 1659 | darg->async = false; |
| 1660 | } |
| 1661 | return err; |
| 1662 | } |
| 1663 | |
| 1664 | if (unlikely(darg->async_done)) |
| 1665 | return 0; |
| 1666 | |
| 1667 | if (prot->tail_size) |
| 1668 | darg->tail = dctx->tail; |
| 1669 | |
| 1670 | exit_free_pages: |
| 1671 | /* Release the pages in case iov was mapped to pages */ |
| 1672 | for (; pages > 0; pages--) |
| 1673 | put_page(page: sg_page(sg: &sgout[pages])); |
| 1674 | exit_free: |
| 1675 | kfree(objp: mem); |
| 1676 | exit_free_skb: |
| 1677 | consume_skb(skb: clear_skb); |
| 1678 | return err; |
| 1679 | } |
| 1680 | |
| 1681 | static int |
| 1682 | tls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx, |
| 1683 | struct msghdr *msg, struct tls_decrypt_arg *darg) |
| 1684 | { |
| 1685 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1686 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1687 | struct strp_msg *rxm; |
| 1688 | int pad, err; |
| 1689 | |
| 1690 | err = tls_decrypt_sg(sk, out_iov: &msg->msg_iter, NULL, darg); |
| 1691 | if (err < 0) { |
| 1692 | if (err == -EBADMSG) |
| 1693 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR); |
| 1694 | return err; |
| 1695 | } |
| 1696 | /* keep going even for ->async, the code below is TLS 1.3 */ |
| 1697 | |
| 1698 | /* If opportunistic TLS 1.3 ZC failed retry without ZC */ |
| 1699 | if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION && |
| 1700 | darg->tail != TLS_RECORD_TYPE_DATA)) { |
| 1701 | darg->zc = false; |
| 1702 | if (!darg->tail) |
| 1703 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL); |
| 1704 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY); |
| 1705 | return tls_decrypt_sw(sk, tls_ctx, msg, darg); |
| 1706 | } |
| 1707 | |
| 1708 | pad = tls_padding_length(prot, skb: darg->skb, darg); |
| 1709 | if (pad < 0) { |
| 1710 | if (darg->skb != tls_strp_msg(ctx)) |
| 1711 | consume_skb(skb: darg->skb); |
| 1712 | return pad; |
| 1713 | } |
| 1714 | |
| 1715 | rxm = strp_msg(skb: darg->skb); |
| 1716 | rxm->full_len -= pad; |
| 1717 | |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | static int |
| 1722 | tls_decrypt_device(struct sock *sk, struct msghdr *msg, |
| 1723 | struct tls_context *tls_ctx, struct tls_decrypt_arg *darg) |
| 1724 | { |
| 1725 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1726 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1727 | struct strp_msg *rxm; |
| 1728 | int pad, err; |
| 1729 | |
| 1730 | if (tls_ctx->rx_conf != TLS_HW) |
| 1731 | return 0; |
| 1732 | |
| 1733 | err = tls_device_decrypted(sk, tls_ctx); |
| 1734 | if (err <= 0) |
| 1735 | return err; |
| 1736 | |
| 1737 | pad = tls_padding_length(prot, skb: tls_strp_msg(ctx), darg); |
| 1738 | if (pad < 0) |
| 1739 | return pad; |
| 1740 | |
| 1741 | darg->async = false; |
| 1742 | darg->skb = tls_strp_msg(ctx); |
| 1743 | /* ->zc downgrade check, in case TLS 1.3 gets here */ |
| 1744 | darg->zc &= !(prot->version == TLS_1_3_VERSION && |
| 1745 | tls_msg(skb: darg->skb)->control != TLS_RECORD_TYPE_DATA); |
| 1746 | |
| 1747 | rxm = strp_msg(skb: darg->skb); |
| 1748 | rxm->full_len -= pad; |
| 1749 | |
| 1750 | if (!darg->zc) { |
| 1751 | /* Non-ZC case needs a real skb */ |
| 1752 | darg->skb = tls_strp_msg_detach(ctx); |
| 1753 | if (!darg->skb) |
| 1754 | return -ENOMEM; |
| 1755 | } else { |
| 1756 | unsigned int off, len; |
| 1757 | |
| 1758 | /* In ZC case nobody cares about the output skb. |
| 1759 | * Just copy the data here. Note the skb is not fully trimmed. |
| 1760 | */ |
| 1761 | off = rxm->offset + prot->prepend_size; |
| 1762 | len = rxm->full_len - prot->overhead_size; |
| 1763 | |
| 1764 | err = skb_copy_datagram_msg(from: darg->skb, offset: off, msg, size: len); |
| 1765 | if (err) |
| 1766 | return err; |
| 1767 | } |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | static int tls_check_pending_rekey(struct sock *sk, struct tls_context *ctx, |
| 1772 | struct sk_buff *skb) |
| 1773 | { |
| 1774 | const struct strp_msg *rxm = strp_msg(skb); |
| 1775 | const struct tls_msg *tlm = tls_msg(skb); |
| 1776 | char hs_type; |
| 1777 | int err; |
| 1778 | |
| 1779 | if (likely(tlm->control != TLS_RECORD_TYPE_HANDSHAKE)) |
| 1780 | return 0; |
| 1781 | |
| 1782 | if (rxm->full_len < 1) |
| 1783 | return 0; |
| 1784 | |
| 1785 | err = skb_copy_bits(skb, offset: rxm->offset, to: &hs_type, len: 1); |
| 1786 | if (err < 0) { |
| 1787 | DEBUG_NET_WARN_ON_ONCE(1); |
| 1788 | return err; |
| 1789 | } |
| 1790 | |
| 1791 | if (hs_type == TLS_HANDSHAKE_KEYUPDATE) { |
| 1792 | struct tls_sw_context_rx *rx_ctx = ctx->priv_ctx_rx; |
| 1793 | |
| 1794 | WRITE_ONCE(rx_ctx->key_update_pending, true); |
| 1795 | TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXREKEYRECEIVED); |
| 1796 | } |
| 1797 | |
| 1798 | return 0; |
| 1799 | } |
| 1800 | |
| 1801 | static int tls_rx_one_record(struct sock *sk, struct msghdr *msg, |
| 1802 | struct tls_decrypt_arg *darg) |
| 1803 | { |
| 1804 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1805 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1806 | struct strp_msg *rxm; |
| 1807 | int err; |
| 1808 | |
| 1809 | err = tls_decrypt_device(sk, msg, tls_ctx, darg); |
| 1810 | if (!err) |
| 1811 | err = tls_decrypt_sw(sk, tls_ctx, msg, darg); |
| 1812 | if (err < 0) |
| 1813 | return err; |
| 1814 | |
| 1815 | rxm = strp_msg(skb: darg->skb); |
| 1816 | rxm->offset += prot->prepend_size; |
| 1817 | rxm->full_len -= prot->overhead_size; |
| 1818 | tls_advance_record_sn(sk, prot, ctx: &tls_ctx->rx); |
| 1819 | |
| 1820 | return tls_check_pending_rekey(sk, ctx: tls_ctx, skb: darg->skb); |
| 1821 | } |
| 1822 | |
| 1823 | int decrypt_skb(struct sock *sk, struct scatterlist *sgout) |
| 1824 | { |
| 1825 | struct tls_decrypt_arg darg = { .zc = true, }; |
| 1826 | |
| 1827 | return tls_decrypt_sg(sk, NULL, out_sg: sgout, darg: &darg); |
| 1828 | } |
| 1829 | |
| 1830 | /* All records returned from a recvmsg() call must have the same type. |
| 1831 | * 0 is not a valid content type. Use it as "no type reported, yet". |
| 1832 | */ |
| 1833 | static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm, |
| 1834 | u8 *control) |
| 1835 | { |
| 1836 | int err; |
| 1837 | |
| 1838 | if (!*control) { |
| 1839 | *control = tlm->control; |
| 1840 | if (!*control) |
| 1841 | return -EBADMSG; |
| 1842 | |
| 1843 | err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, |
| 1844 | len: sizeof(*control), data: control); |
| 1845 | if (*control != TLS_RECORD_TYPE_DATA) { |
| 1846 | if (err || msg->msg_flags & MSG_CTRUNC) |
| 1847 | return -EIO; |
| 1848 | } |
| 1849 | } else if (*control != tlm->control) { |
| 1850 | return 0; |
| 1851 | } |
| 1852 | |
| 1853 | return 1; |
| 1854 | } |
| 1855 | |
| 1856 | static void tls_rx_rec_done(struct tls_sw_context_rx *ctx) |
| 1857 | { |
| 1858 | tls_strp_msg_done(strp: &ctx->strp); |
| 1859 | } |
| 1860 | |
| 1861 | /* This function traverses the rx_list in tls receive context to copies the |
| 1862 | * decrypted records into the buffer provided by caller zero copy is not |
| 1863 | * true. Further, the records are removed from the rx_list if it is not a peek |
| 1864 | * case and the record has been consumed completely. |
| 1865 | */ |
| 1866 | static int process_rx_list(struct tls_sw_context_rx *ctx, |
| 1867 | struct msghdr *msg, |
| 1868 | u8 *control, |
| 1869 | size_t skip, |
| 1870 | size_t len, |
| 1871 | bool is_peek, |
| 1872 | bool *more) |
| 1873 | { |
| 1874 | struct sk_buff *skb = skb_peek(list_: &ctx->rx_list); |
| 1875 | struct tls_msg *tlm; |
| 1876 | ssize_t copied = 0; |
| 1877 | int err; |
| 1878 | |
| 1879 | while (skip && skb) { |
| 1880 | struct strp_msg *rxm = strp_msg(skb); |
| 1881 | tlm = tls_msg(skb); |
| 1882 | |
| 1883 | err = tls_record_content_type(msg, tlm, control); |
| 1884 | if (err <= 0) |
| 1885 | goto more; |
| 1886 | |
| 1887 | if (skip < rxm->full_len) |
| 1888 | break; |
| 1889 | |
| 1890 | skip = skip - rxm->full_len; |
| 1891 | skb = skb_peek_next(skb, list_: &ctx->rx_list); |
| 1892 | } |
| 1893 | |
| 1894 | while (len && skb) { |
| 1895 | struct sk_buff *next_skb; |
| 1896 | struct strp_msg *rxm = strp_msg(skb); |
| 1897 | int chunk = min_t(unsigned int, rxm->full_len - skip, len); |
| 1898 | |
| 1899 | tlm = tls_msg(skb); |
| 1900 | |
| 1901 | err = tls_record_content_type(msg, tlm, control); |
| 1902 | if (err <= 0) |
| 1903 | goto more; |
| 1904 | |
| 1905 | err = skb_copy_datagram_msg(from: skb, offset: rxm->offset + skip, |
| 1906 | msg, size: chunk); |
| 1907 | if (err < 0) |
| 1908 | goto more; |
| 1909 | |
| 1910 | len = len - chunk; |
| 1911 | copied = copied + chunk; |
| 1912 | |
| 1913 | /* Consume the data from record if it is non-peek case*/ |
| 1914 | if (!is_peek) { |
| 1915 | rxm->offset = rxm->offset + chunk; |
| 1916 | rxm->full_len = rxm->full_len - chunk; |
| 1917 | |
| 1918 | /* Return if there is unconsumed data in the record */ |
| 1919 | if (rxm->full_len - skip) |
| 1920 | break; |
| 1921 | } |
| 1922 | |
| 1923 | /* The remaining skip-bytes must lie in 1st record in rx_list. |
| 1924 | * So from the 2nd record, 'skip' should be 0. |
| 1925 | */ |
| 1926 | skip = 0; |
| 1927 | |
| 1928 | if (msg) |
| 1929 | msg->msg_flags |= MSG_EOR; |
| 1930 | |
| 1931 | next_skb = skb_peek_next(skb, list_: &ctx->rx_list); |
| 1932 | |
| 1933 | if (!is_peek) { |
| 1934 | __skb_unlink(skb, list: &ctx->rx_list); |
| 1935 | consume_skb(skb); |
| 1936 | } |
| 1937 | |
| 1938 | skb = next_skb; |
| 1939 | } |
| 1940 | err = 0; |
| 1941 | |
| 1942 | out: |
| 1943 | return copied ? : err; |
| 1944 | more: |
| 1945 | if (more) |
| 1946 | *more = true; |
| 1947 | goto out; |
| 1948 | } |
| 1949 | |
| 1950 | static bool |
| 1951 | tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot, |
| 1952 | size_t len_left, size_t decrypted, ssize_t done, |
| 1953 | size_t *flushed_at) |
| 1954 | { |
| 1955 | size_t max_rec; |
| 1956 | |
| 1957 | if (len_left <= decrypted) |
| 1958 | return false; |
| 1959 | |
| 1960 | max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE; |
| 1961 | if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec) |
| 1962 | return false; |
| 1963 | |
| 1964 | *flushed_at = done; |
| 1965 | return sk_flush_backlog(sk); |
| 1966 | } |
| 1967 | |
| 1968 | static int tls_rx_reader_acquire(struct sock *sk, struct tls_sw_context_rx *ctx, |
| 1969 | bool nonblock) |
| 1970 | { |
| 1971 | long timeo; |
| 1972 | int ret; |
| 1973 | |
| 1974 | timeo = sock_rcvtimeo(sk, noblock: nonblock); |
| 1975 | |
| 1976 | while (unlikely(ctx->reader_present)) { |
| 1977 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 1978 | |
| 1979 | ctx->reader_contended = 1; |
| 1980 | |
| 1981 | add_wait_queue(wq_head: &ctx->wq, wq_entry: &wait); |
| 1982 | ret = sk_wait_event(sk, &timeo, |
| 1983 | !READ_ONCE(ctx->reader_present), &wait); |
| 1984 | remove_wait_queue(wq_head: &ctx->wq, wq_entry: &wait); |
| 1985 | |
| 1986 | if (timeo <= 0) |
| 1987 | return -EAGAIN; |
| 1988 | if (signal_pending(current)) |
| 1989 | return sock_intr_errno(timeo); |
| 1990 | if (ret < 0) |
| 1991 | return ret; |
| 1992 | } |
| 1993 | |
| 1994 | WRITE_ONCE(ctx->reader_present, 1); |
| 1995 | |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | static int tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx, |
| 2000 | bool nonblock) |
| 2001 | { |
| 2002 | int err; |
| 2003 | |
| 2004 | lock_sock(sk); |
| 2005 | err = tls_rx_reader_acquire(sk, ctx, nonblock); |
| 2006 | if (err) |
| 2007 | release_sock(sk); |
| 2008 | return err; |
| 2009 | } |
| 2010 | |
| 2011 | static void tls_rx_reader_release(struct sock *sk, struct tls_sw_context_rx *ctx) |
| 2012 | { |
| 2013 | if (unlikely(ctx->reader_contended)) { |
| 2014 | if (wq_has_sleeper(wq_head: &ctx->wq)) |
| 2015 | wake_up(&ctx->wq); |
| 2016 | else |
| 2017 | ctx->reader_contended = 0; |
| 2018 | |
| 2019 | WARN_ON_ONCE(!ctx->reader_present); |
| 2020 | } |
| 2021 | |
| 2022 | WRITE_ONCE(ctx->reader_present, 0); |
| 2023 | } |
| 2024 | |
| 2025 | static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx) |
| 2026 | { |
| 2027 | tls_rx_reader_release(sk, ctx); |
| 2028 | release_sock(sk); |
| 2029 | } |
| 2030 | |
| 2031 | int tls_sw_recvmsg(struct sock *sk, |
| 2032 | struct msghdr *msg, |
| 2033 | size_t len, |
| 2034 | int flags, |
| 2035 | int *addr_len) |
| 2036 | { |
| 2037 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2038 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2039 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 2040 | ssize_t decrypted = 0, async_copy_bytes = 0; |
| 2041 | struct sk_psock *psock; |
| 2042 | unsigned char control = 0; |
| 2043 | size_t flushed_at = 0; |
| 2044 | struct strp_msg *rxm; |
| 2045 | struct tls_msg *tlm; |
| 2046 | ssize_t copied = 0; |
| 2047 | ssize_t peeked = 0; |
| 2048 | bool async = false; |
| 2049 | int target, err; |
| 2050 | bool is_kvec = iov_iter_is_kvec(i: &msg->msg_iter); |
| 2051 | bool is_peek = flags & MSG_PEEK; |
| 2052 | bool rx_more = false; |
| 2053 | bool released = true; |
| 2054 | bool bpf_strp_enabled; |
| 2055 | bool zc_capable; |
| 2056 | |
| 2057 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 2058 | return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); |
| 2059 | |
| 2060 | err = tls_rx_reader_lock(sk, ctx, nonblock: flags & MSG_DONTWAIT); |
| 2061 | if (err < 0) |
| 2062 | return err; |
| 2063 | psock = sk_psock_get(sk); |
| 2064 | bpf_strp_enabled = sk_psock_strp_enabled(psock); |
| 2065 | |
| 2066 | /* If crypto failed the connection is broken */ |
| 2067 | err = ctx->async_wait.err; |
| 2068 | if (err) |
| 2069 | goto end; |
| 2070 | |
| 2071 | /* Process pending decrypted records. It must be non-zero-copy */ |
| 2072 | err = process_rx_list(ctx, msg, control: &control, skip: 0, len, is_peek, more: &rx_more); |
| 2073 | if (err < 0) |
| 2074 | goto end; |
| 2075 | |
| 2076 | /* process_rx_list() will set @control if it processed any records */ |
| 2077 | copied = err; |
| 2078 | if (len <= copied || rx_more || |
| 2079 | (control && control != TLS_RECORD_TYPE_DATA)) |
| 2080 | goto end; |
| 2081 | |
| 2082 | target = sock_rcvlowat(sk, waitall: flags & MSG_WAITALL, len); |
| 2083 | len = len - copied; |
| 2084 | |
| 2085 | zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek && |
| 2086 | ctx->zc_capable; |
| 2087 | decrypted = 0; |
| 2088 | while (len && (decrypted + copied < target || tls_strp_msg_ready(ctx))) { |
| 2089 | struct tls_decrypt_arg darg; |
| 2090 | int to_decrypt, chunk; |
| 2091 | |
| 2092 | err = tls_rx_rec_wait(sk, psock, nonblock: flags & MSG_DONTWAIT, |
| 2093 | released); |
| 2094 | if (err <= 0) { |
| 2095 | if (psock) { |
| 2096 | chunk = sk_msg_recvmsg(sk, psock, msg, len, |
| 2097 | flags); |
| 2098 | if (chunk > 0) { |
| 2099 | decrypted += chunk; |
| 2100 | len -= chunk; |
| 2101 | continue; |
| 2102 | } |
| 2103 | } |
| 2104 | goto recv_end; |
| 2105 | } |
| 2106 | |
| 2107 | memset(&darg.inargs, 0, sizeof(darg.inargs)); |
| 2108 | |
| 2109 | rxm = strp_msg(skb: tls_strp_msg(ctx)); |
| 2110 | tlm = tls_msg(skb: tls_strp_msg(ctx)); |
| 2111 | |
| 2112 | to_decrypt = rxm->full_len - prot->overhead_size; |
| 2113 | |
| 2114 | if (zc_capable && to_decrypt <= len && |
| 2115 | tlm->control == TLS_RECORD_TYPE_DATA) |
| 2116 | darg.zc = true; |
| 2117 | |
| 2118 | /* Do not use async mode if record is non-data */ |
| 2119 | if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled) |
| 2120 | darg.async = ctx->async_capable; |
| 2121 | else |
| 2122 | darg.async = false; |
| 2123 | |
| 2124 | err = tls_rx_one_record(sk, msg, darg: &darg); |
| 2125 | if (err < 0) { |
| 2126 | tls_err_abort(sk, err: -EBADMSG); |
| 2127 | goto recv_end; |
| 2128 | } |
| 2129 | |
| 2130 | async |= darg.async; |
| 2131 | |
| 2132 | /* If the type of records being processed is not known yet, |
| 2133 | * set it to record type just dequeued. If it is already known, |
| 2134 | * but does not match the record type just dequeued, go to end. |
| 2135 | * We always get record type here since for tls1.2, record type |
| 2136 | * is known just after record is dequeued from stream parser. |
| 2137 | * For tls1.3, we disable async. |
| 2138 | */ |
| 2139 | err = tls_record_content_type(msg, tlm: tls_msg(skb: darg.skb), control: &control); |
| 2140 | if (err <= 0) { |
| 2141 | DEBUG_NET_WARN_ON_ONCE(darg.zc); |
| 2142 | tls_rx_rec_done(ctx); |
| 2143 | put_on_rx_list_err: |
| 2144 | __skb_queue_tail(list: &ctx->rx_list, newsk: darg.skb); |
| 2145 | goto recv_end; |
| 2146 | } |
| 2147 | |
| 2148 | /* periodically flush backlog, and feed strparser */ |
| 2149 | released = tls_read_flush_backlog(sk, prot, len_left: len, decrypted: to_decrypt, |
| 2150 | done: decrypted + copied, |
| 2151 | flushed_at: &flushed_at); |
| 2152 | |
| 2153 | /* TLS 1.3 may have updated the length by more than overhead */ |
| 2154 | rxm = strp_msg(skb: darg.skb); |
| 2155 | chunk = rxm->full_len; |
| 2156 | tls_rx_rec_done(ctx); |
| 2157 | |
| 2158 | if (!darg.zc) { |
| 2159 | bool partially_consumed = chunk > len; |
| 2160 | struct sk_buff *skb = darg.skb; |
| 2161 | |
| 2162 | DEBUG_NET_WARN_ON_ONCE(darg.skb == ctx->strp.anchor); |
| 2163 | |
| 2164 | if (async) { |
| 2165 | /* TLS 1.2-only, to_decrypt must be text len */ |
| 2166 | chunk = min_t(int, to_decrypt, len); |
| 2167 | async_copy_bytes += chunk; |
| 2168 | put_on_rx_list: |
| 2169 | decrypted += chunk; |
| 2170 | len -= chunk; |
| 2171 | __skb_queue_tail(list: &ctx->rx_list, newsk: skb); |
| 2172 | if (unlikely(control != TLS_RECORD_TYPE_DATA)) |
| 2173 | break; |
| 2174 | continue; |
| 2175 | } |
| 2176 | |
| 2177 | if (bpf_strp_enabled) { |
| 2178 | released = true; |
| 2179 | err = sk_psock_tls_strp_read(psock, skb); |
| 2180 | if (err != __SK_PASS) { |
| 2181 | rxm->offset = rxm->offset + rxm->full_len; |
| 2182 | rxm->full_len = 0; |
| 2183 | if (err == __SK_DROP) |
| 2184 | consume_skb(skb); |
| 2185 | continue; |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | if (partially_consumed) |
| 2190 | chunk = len; |
| 2191 | |
| 2192 | err = skb_copy_datagram_msg(from: skb, offset: rxm->offset, |
| 2193 | msg, size: chunk); |
| 2194 | if (err < 0) |
| 2195 | goto put_on_rx_list_err; |
| 2196 | |
| 2197 | if (is_peek) { |
| 2198 | peeked += chunk; |
| 2199 | goto put_on_rx_list; |
| 2200 | } |
| 2201 | |
| 2202 | if (partially_consumed) { |
| 2203 | rxm->offset += chunk; |
| 2204 | rxm->full_len -= chunk; |
| 2205 | goto put_on_rx_list; |
| 2206 | } |
| 2207 | |
| 2208 | consume_skb(skb); |
| 2209 | } |
| 2210 | |
| 2211 | decrypted += chunk; |
| 2212 | len -= chunk; |
| 2213 | |
| 2214 | /* Return full control message to userspace before trying |
| 2215 | * to parse another message type |
| 2216 | */ |
| 2217 | msg->msg_flags |= MSG_EOR; |
| 2218 | if (control != TLS_RECORD_TYPE_DATA) |
| 2219 | break; |
| 2220 | } |
| 2221 | |
| 2222 | recv_end: |
| 2223 | if (async) { |
| 2224 | int ret; |
| 2225 | |
| 2226 | /* Wait for all previously submitted records to be decrypted */ |
| 2227 | ret = tls_decrypt_async_wait(ctx); |
| 2228 | __skb_queue_purge(list: &ctx->async_hold); |
| 2229 | |
| 2230 | if (ret) { |
| 2231 | if (err >= 0 || err == -EINPROGRESS) |
| 2232 | err = ret; |
| 2233 | goto end; |
| 2234 | } |
| 2235 | |
| 2236 | /* Drain records from the rx_list & copy if required */ |
| 2237 | if (is_peek) |
| 2238 | err = process_rx_list(ctx, msg, control: &control, skip: copied + peeked, |
| 2239 | len: decrypted - peeked, is_peek, NULL); |
| 2240 | else |
| 2241 | err = process_rx_list(ctx, msg, control: &control, skip: 0, |
| 2242 | len: async_copy_bytes, is_peek, NULL); |
| 2243 | |
| 2244 | /* we could have copied less than we wanted, and possibly nothing */ |
| 2245 | decrypted += max(err, 0) - async_copy_bytes; |
| 2246 | } |
| 2247 | |
| 2248 | copied += decrypted; |
| 2249 | |
| 2250 | end: |
| 2251 | tls_rx_reader_unlock(sk, ctx); |
| 2252 | if (psock) |
| 2253 | sk_psock_put(sk, psock); |
| 2254 | return copied ? : err; |
| 2255 | } |
| 2256 | |
| 2257 | ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 2258 | struct pipe_inode_info *pipe, |
| 2259 | size_t len, unsigned int flags) |
| 2260 | { |
| 2261 | struct tls_context *tls_ctx = tls_get_ctx(sk: sock->sk); |
| 2262 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2263 | struct strp_msg *rxm = NULL; |
| 2264 | struct sock *sk = sock->sk; |
| 2265 | struct tls_msg *tlm; |
| 2266 | struct sk_buff *skb; |
| 2267 | ssize_t copied = 0; |
| 2268 | int chunk; |
| 2269 | int err; |
| 2270 | |
| 2271 | err = tls_rx_reader_lock(sk, ctx, nonblock: flags & SPLICE_F_NONBLOCK); |
| 2272 | if (err < 0) |
| 2273 | return err; |
| 2274 | |
| 2275 | if (!skb_queue_empty(list: &ctx->rx_list)) { |
| 2276 | skb = __skb_dequeue(list: &ctx->rx_list); |
| 2277 | } else { |
| 2278 | struct tls_decrypt_arg darg; |
| 2279 | |
| 2280 | err = tls_rx_rec_wait(sk, NULL, nonblock: flags & SPLICE_F_NONBLOCK, |
| 2281 | released: true); |
| 2282 | if (err <= 0) |
| 2283 | goto splice_read_end; |
| 2284 | |
| 2285 | memset(&darg.inargs, 0, sizeof(darg.inargs)); |
| 2286 | |
| 2287 | err = tls_rx_one_record(sk, NULL, darg: &darg); |
| 2288 | if (err < 0) { |
| 2289 | tls_err_abort(sk, err: -EBADMSG); |
| 2290 | goto splice_read_end; |
| 2291 | } |
| 2292 | |
| 2293 | tls_rx_rec_done(ctx); |
| 2294 | skb = darg.skb; |
| 2295 | } |
| 2296 | |
| 2297 | rxm = strp_msg(skb); |
| 2298 | tlm = tls_msg(skb); |
| 2299 | |
| 2300 | /* splice does not support reading control messages */ |
| 2301 | if (tlm->control != TLS_RECORD_TYPE_DATA) { |
| 2302 | err = -EINVAL; |
| 2303 | goto splice_requeue; |
| 2304 | } |
| 2305 | |
| 2306 | chunk = min_t(unsigned int, rxm->full_len, len); |
| 2307 | copied = skb_splice_bits(skb, sk, offset: rxm->offset, pipe, len: chunk, flags); |
| 2308 | if (copied < 0) |
| 2309 | goto splice_requeue; |
| 2310 | |
| 2311 | if (chunk < rxm->full_len) { |
| 2312 | rxm->offset += len; |
| 2313 | rxm->full_len -= len; |
| 2314 | goto splice_requeue; |
| 2315 | } |
| 2316 | |
| 2317 | consume_skb(skb); |
| 2318 | |
| 2319 | splice_read_end: |
| 2320 | tls_rx_reader_unlock(sk, ctx); |
| 2321 | return copied ? : err; |
| 2322 | |
| 2323 | splice_requeue: |
| 2324 | __skb_queue_head(list: &ctx->rx_list, newsk: skb); |
| 2325 | goto splice_read_end; |
| 2326 | } |
| 2327 | |
| 2328 | int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc, |
| 2329 | sk_read_actor_t read_actor) |
| 2330 | { |
| 2331 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2332 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2333 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 2334 | struct strp_msg *rxm = NULL; |
| 2335 | struct sk_buff *skb = NULL; |
| 2336 | struct sk_psock *psock; |
| 2337 | size_t flushed_at = 0; |
| 2338 | bool released = true; |
| 2339 | struct tls_msg *tlm; |
| 2340 | ssize_t copied = 0; |
| 2341 | ssize_t decrypted; |
| 2342 | int err, used; |
| 2343 | |
| 2344 | psock = sk_psock_get(sk); |
| 2345 | if (psock) { |
| 2346 | sk_psock_put(sk, psock); |
| 2347 | return -EINVAL; |
| 2348 | } |
| 2349 | err = tls_rx_reader_acquire(sk, ctx, nonblock: true); |
| 2350 | if (err < 0) |
| 2351 | return err; |
| 2352 | |
| 2353 | /* If crypto failed the connection is broken */ |
| 2354 | err = ctx->async_wait.err; |
| 2355 | if (err) |
| 2356 | goto read_sock_end; |
| 2357 | |
| 2358 | decrypted = 0; |
| 2359 | do { |
| 2360 | if (!skb_queue_empty(list: &ctx->rx_list)) { |
| 2361 | skb = __skb_dequeue(list: &ctx->rx_list); |
| 2362 | rxm = strp_msg(skb); |
| 2363 | tlm = tls_msg(skb); |
| 2364 | } else { |
| 2365 | struct tls_decrypt_arg darg; |
| 2366 | |
| 2367 | err = tls_rx_rec_wait(sk, NULL, nonblock: true, released); |
| 2368 | if (err <= 0) |
| 2369 | goto read_sock_end; |
| 2370 | |
| 2371 | memset(&darg.inargs, 0, sizeof(darg.inargs)); |
| 2372 | |
| 2373 | err = tls_rx_one_record(sk, NULL, darg: &darg); |
| 2374 | if (err < 0) { |
| 2375 | tls_err_abort(sk, err: -EBADMSG); |
| 2376 | goto read_sock_end; |
| 2377 | } |
| 2378 | |
| 2379 | released = tls_read_flush_backlog(sk, prot, INT_MAX, |
| 2380 | decrypted: 0, done: decrypted, |
| 2381 | flushed_at: &flushed_at); |
| 2382 | skb = darg.skb; |
| 2383 | rxm = strp_msg(skb); |
| 2384 | tlm = tls_msg(skb); |
| 2385 | decrypted += rxm->full_len; |
| 2386 | |
| 2387 | tls_rx_rec_done(ctx); |
| 2388 | } |
| 2389 | |
| 2390 | /* read_sock does not support reading control messages */ |
| 2391 | if (tlm->control != TLS_RECORD_TYPE_DATA) { |
| 2392 | err = -EINVAL; |
| 2393 | goto read_sock_requeue; |
| 2394 | } |
| 2395 | |
| 2396 | used = read_actor(desc, skb, rxm->offset, rxm->full_len); |
| 2397 | if (used <= 0) { |
| 2398 | if (!copied) |
| 2399 | err = used; |
| 2400 | goto read_sock_requeue; |
| 2401 | } |
| 2402 | copied += used; |
| 2403 | if (used < rxm->full_len) { |
| 2404 | rxm->offset += used; |
| 2405 | rxm->full_len -= used; |
| 2406 | if (!desc->count) |
| 2407 | goto read_sock_requeue; |
| 2408 | } else { |
| 2409 | consume_skb(skb); |
| 2410 | if (!desc->count) |
| 2411 | skb = NULL; |
| 2412 | } |
| 2413 | } while (skb); |
| 2414 | |
| 2415 | read_sock_end: |
| 2416 | tls_rx_reader_release(sk, ctx); |
| 2417 | return copied ? : err; |
| 2418 | |
| 2419 | read_sock_requeue: |
| 2420 | __skb_queue_head(list: &ctx->rx_list, newsk: skb); |
| 2421 | goto read_sock_end; |
| 2422 | } |
| 2423 | |
| 2424 | bool tls_sw_sock_is_readable(struct sock *sk) |
| 2425 | { |
| 2426 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2427 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2428 | bool ingress_empty = true; |
| 2429 | struct sk_psock *psock; |
| 2430 | |
| 2431 | rcu_read_lock(); |
| 2432 | psock = sk_psock(sk); |
| 2433 | if (psock) |
| 2434 | ingress_empty = list_empty(head: &psock->ingress_msg); |
| 2435 | rcu_read_unlock(); |
| 2436 | |
| 2437 | return !ingress_empty || tls_strp_msg_ready(ctx) || |
| 2438 | !skb_queue_empty(list: &ctx->rx_list); |
| 2439 | } |
| 2440 | |
| 2441 | int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb) |
| 2442 | { |
| 2443 | struct tls_context *tls_ctx = tls_get_ctx(sk: strp->sk); |
| 2444 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 2445 | char [TLS_HEADER_SIZE + TLS_MAX_IV_SIZE]; |
| 2446 | size_t cipher_overhead; |
| 2447 | size_t data_len = 0; |
| 2448 | int ret; |
| 2449 | |
| 2450 | /* Verify that we have a full TLS header, or wait for more data */ |
| 2451 | if (strp->stm.offset + prot->prepend_size > skb->len) |
| 2452 | return 0; |
| 2453 | |
| 2454 | /* Sanity-check size of on-stack buffer. */ |
| 2455 | if (WARN_ON(prot->prepend_size > sizeof(header))) { |
| 2456 | ret = -EINVAL; |
| 2457 | goto read_failure; |
| 2458 | } |
| 2459 | |
| 2460 | /* Linearize header to local buffer */ |
| 2461 | ret = skb_copy_bits(skb, offset: strp->stm.offset, to: header, len: prot->prepend_size); |
| 2462 | if (ret < 0) |
| 2463 | goto read_failure; |
| 2464 | |
| 2465 | strp->mark = header[0]; |
| 2466 | |
| 2467 | data_len = ((header[4] & 0xFF) | (header[3] << 8)); |
| 2468 | |
| 2469 | cipher_overhead = prot->tag_size; |
| 2470 | if (prot->version != TLS_1_3_VERSION && |
| 2471 | prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) |
| 2472 | cipher_overhead += prot->iv_size; |
| 2473 | |
| 2474 | if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead + |
| 2475 | prot->tail_size) { |
| 2476 | ret = -EMSGSIZE; |
| 2477 | goto read_failure; |
| 2478 | } |
| 2479 | if (data_len < cipher_overhead) { |
| 2480 | ret = -EBADMSG; |
| 2481 | goto read_failure; |
| 2482 | } |
| 2483 | |
| 2484 | /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */ |
| 2485 | if (header[1] != TLS_1_2_VERSION_MINOR || |
| 2486 | header[2] != TLS_1_2_VERSION_MAJOR) { |
| 2487 | ret = -EINVAL; |
| 2488 | goto read_failure; |
| 2489 | } |
| 2490 | |
| 2491 | tls_device_rx_resync_new_rec(sk: strp->sk, rcd_len: data_len + TLS_HEADER_SIZE, |
| 2492 | TCP_SKB_CB(skb)->seq + strp->stm.offset); |
| 2493 | return data_len + TLS_HEADER_SIZE; |
| 2494 | |
| 2495 | read_failure: |
| 2496 | tls_strp_abort_strp(strp, err: ret); |
| 2497 | return ret; |
| 2498 | } |
| 2499 | |
| 2500 | void tls_rx_msg_ready(struct tls_strparser *strp) |
| 2501 | { |
| 2502 | struct tls_sw_context_rx *ctx; |
| 2503 | |
| 2504 | ctx = container_of(strp, struct tls_sw_context_rx, strp); |
| 2505 | ctx->saved_data_ready(strp->sk); |
| 2506 | } |
| 2507 | |
| 2508 | static void tls_data_ready(struct sock *sk) |
| 2509 | { |
| 2510 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2511 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2512 | struct sk_psock *psock; |
| 2513 | gfp_t alloc_save; |
| 2514 | |
| 2515 | trace_sk_data_ready(sk); |
| 2516 | |
| 2517 | alloc_save = sk->sk_allocation; |
| 2518 | sk->sk_allocation = GFP_ATOMIC; |
| 2519 | tls_strp_data_ready(strp: &ctx->strp); |
| 2520 | sk->sk_allocation = alloc_save; |
| 2521 | |
| 2522 | psock = sk_psock_get(sk); |
| 2523 | if (psock) { |
| 2524 | if (!list_empty(head: &psock->ingress_msg)) |
| 2525 | ctx->saved_data_ready(sk); |
| 2526 | sk_psock_put(sk, psock); |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | void tls_sw_cancel_work_tx(struct tls_context *tls_ctx) |
| 2531 | { |
| 2532 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 2533 | |
| 2534 | set_bit(BIT_TX_CLOSING, addr: &ctx->tx_bitmask); |
| 2535 | set_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask); |
| 2536 | cancel_delayed_work_sync(dwork: &ctx->tx_work.work); |
| 2537 | } |
| 2538 | |
| 2539 | void tls_sw_release_resources_tx(struct sock *sk) |
| 2540 | { |
| 2541 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2542 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 2543 | struct tls_rec *rec, *tmp; |
| 2544 | |
| 2545 | /* Wait for any pending async encryptions to complete */ |
| 2546 | tls_encrypt_async_wait(ctx); |
| 2547 | |
| 2548 | tls_tx_records(sk, flags: -1); |
| 2549 | |
| 2550 | /* Free up un-sent records in tx_list. First, free |
| 2551 | * the partially sent record if any at head of tx_list. |
| 2552 | */ |
| 2553 | if (tls_ctx->partially_sent_record) { |
| 2554 | tls_free_partial_record(sk, ctx: tls_ctx); |
| 2555 | rec = list_first_entry(&ctx->tx_list, |
| 2556 | struct tls_rec, list); |
| 2557 | list_del(entry: &rec->list); |
| 2558 | sk_msg_free(sk, msg: &rec->msg_plaintext); |
| 2559 | kfree(objp: rec); |
| 2560 | } |
| 2561 | |
| 2562 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
| 2563 | list_del(entry: &rec->list); |
| 2564 | sk_msg_free(sk, msg: &rec->msg_encrypted); |
| 2565 | sk_msg_free(sk, msg: &rec->msg_plaintext); |
| 2566 | kfree(objp: rec); |
| 2567 | } |
| 2568 | |
| 2569 | crypto_free_aead(tfm: ctx->aead_send); |
| 2570 | tls_free_open_rec(sk); |
| 2571 | } |
| 2572 | |
| 2573 | void tls_sw_free_ctx_tx(struct tls_context *tls_ctx) |
| 2574 | { |
| 2575 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 2576 | |
| 2577 | kfree(objp: ctx); |
| 2578 | } |
| 2579 | |
| 2580 | void tls_sw_release_resources_rx(struct sock *sk) |
| 2581 | { |
| 2582 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2583 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2584 | |
| 2585 | if (ctx->aead_recv) { |
| 2586 | __skb_queue_purge(list: &ctx->rx_list); |
| 2587 | crypto_free_aead(tfm: ctx->aead_recv); |
| 2588 | tls_strp_stop(strp: &ctx->strp); |
| 2589 | /* If tls_sw_strparser_arm() was not called (cleanup paths) |
| 2590 | * we still want to tls_strp_stop(), but sk->sk_data_ready was |
| 2591 | * never swapped. |
| 2592 | */ |
| 2593 | if (ctx->saved_data_ready) { |
| 2594 | write_lock_bh(&sk->sk_callback_lock); |
| 2595 | sk->sk_data_ready = ctx->saved_data_ready; |
| 2596 | write_unlock_bh(&sk->sk_callback_lock); |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | void tls_sw_strparser_done(struct tls_context *tls_ctx) |
| 2602 | { |
| 2603 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2604 | |
| 2605 | tls_strp_done(strp: &ctx->strp); |
| 2606 | } |
| 2607 | |
| 2608 | void tls_sw_free_ctx_rx(struct tls_context *tls_ctx) |
| 2609 | { |
| 2610 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2611 | |
| 2612 | kfree(objp: ctx); |
| 2613 | } |
| 2614 | |
| 2615 | void tls_sw_free_resources_rx(struct sock *sk) |
| 2616 | { |
| 2617 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2618 | |
| 2619 | tls_sw_release_resources_rx(sk); |
| 2620 | tls_sw_free_ctx_rx(tls_ctx); |
| 2621 | } |
| 2622 | |
| 2623 | /* The work handler to transmitt the encrypted records in tx_list */ |
| 2624 | static void tx_work_handler(struct work_struct *work) |
| 2625 | { |
| 2626 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 2627 | struct tx_work *tx_work = container_of(delayed_work, |
| 2628 | struct tx_work, work); |
| 2629 | struct sock *sk = tx_work->sk; |
| 2630 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2631 | struct tls_sw_context_tx *ctx; |
| 2632 | |
| 2633 | if (unlikely(!tls_ctx)) |
| 2634 | return; |
| 2635 | |
| 2636 | ctx = tls_sw_ctx_tx(tls_ctx); |
| 2637 | if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask)) |
| 2638 | return; |
| 2639 | |
| 2640 | if (!test_and_clear_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) |
| 2641 | return; |
| 2642 | |
| 2643 | if (mutex_trylock(&tls_ctx->tx_lock)) { |
| 2644 | lock_sock(sk); |
| 2645 | tls_tx_records(sk, flags: -1); |
| 2646 | release_sock(sk); |
| 2647 | mutex_unlock(lock: &tls_ctx->tx_lock); |
| 2648 | } else if (!test_and_set_bit(BIT_TX_SCHEDULED, addr: &ctx->tx_bitmask)) { |
| 2649 | /* Someone is holding the tx_lock, they will likely run Tx |
| 2650 | * and cancel the work on their way out of the lock section. |
| 2651 | * Schedule a long delay just in case. |
| 2652 | */ |
| 2653 | schedule_delayed_work(dwork: &ctx->tx_work.work, delay: msecs_to_jiffies(m: 10)); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx) |
| 2658 | { |
| 2659 | struct tls_rec *rec; |
| 2660 | |
| 2661 | rec = list_first_entry_or_null(&ctx->tx_list, struct tls_rec, list); |
| 2662 | if (!rec) |
| 2663 | return false; |
| 2664 | |
| 2665 | return READ_ONCE(rec->tx_ready); |
| 2666 | } |
| 2667 | |
| 2668 | void tls_sw_write_space(struct sock *sk, struct tls_context *ctx) |
| 2669 | { |
| 2670 | struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(tls_ctx: ctx); |
| 2671 | |
| 2672 | /* Schedule the transmission if tx list is ready */ |
| 2673 | if (tls_is_tx_ready(ctx: tx_ctx) && |
| 2674 | !test_and_set_bit(BIT_TX_SCHEDULED, addr: &tx_ctx->tx_bitmask)) |
| 2675 | schedule_delayed_work(dwork: &tx_ctx->tx_work.work, delay: 0); |
| 2676 | } |
| 2677 | |
| 2678 | void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx) |
| 2679 | { |
| 2680 | struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx); |
| 2681 | |
| 2682 | write_lock_bh(&sk->sk_callback_lock); |
| 2683 | rx_ctx->saved_data_ready = sk->sk_data_ready; |
| 2684 | sk->sk_data_ready = tls_data_ready; |
| 2685 | write_unlock_bh(&sk->sk_callback_lock); |
| 2686 | } |
| 2687 | |
| 2688 | void tls_update_rx_zc_capable(struct tls_context *tls_ctx) |
| 2689 | { |
| 2690 | struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx); |
| 2691 | |
| 2692 | rx_ctx->zc_capable = tls_ctx->rx_no_pad || |
| 2693 | tls_ctx->prot_info.version != TLS_1_3_VERSION; |
| 2694 | } |
| 2695 | |
| 2696 | static struct tls_sw_context_tx *init_ctx_tx(struct tls_context *ctx, struct sock *sk) |
| 2697 | { |
| 2698 | struct tls_sw_context_tx *sw_ctx_tx; |
| 2699 | |
| 2700 | if (!ctx->priv_ctx_tx) { |
| 2701 | sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); |
| 2702 | if (!sw_ctx_tx) |
| 2703 | return NULL; |
| 2704 | } else { |
| 2705 | sw_ctx_tx = ctx->priv_ctx_tx; |
| 2706 | } |
| 2707 | |
| 2708 | crypto_init_wait(wait: &sw_ctx_tx->async_wait); |
| 2709 | atomic_set(v: &sw_ctx_tx->encrypt_pending, i: 1); |
| 2710 | INIT_LIST_HEAD(list: &sw_ctx_tx->tx_list); |
| 2711 | INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler); |
| 2712 | sw_ctx_tx->tx_work.sk = sk; |
| 2713 | |
| 2714 | return sw_ctx_tx; |
| 2715 | } |
| 2716 | |
| 2717 | static struct tls_sw_context_rx *init_ctx_rx(struct tls_context *ctx) |
| 2718 | { |
| 2719 | struct tls_sw_context_rx *sw_ctx_rx; |
| 2720 | |
| 2721 | if (!ctx->priv_ctx_rx) { |
| 2722 | sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); |
| 2723 | if (!sw_ctx_rx) |
| 2724 | return NULL; |
| 2725 | } else { |
| 2726 | sw_ctx_rx = ctx->priv_ctx_rx; |
| 2727 | } |
| 2728 | |
| 2729 | crypto_init_wait(wait: &sw_ctx_rx->async_wait); |
| 2730 | atomic_set(v: &sw_ctx_rx->decrypt_pending, i: 1); |
| 2731 | init_waitqueue_head(&sw_ctx_rx->wq); |
| 2732 | skb_queue_head_init(list: &sw_ctx_rx->rx_list); |
| 2733 | skb_queue_head_init(list: &sw_ctx_rx->async_hold); |
| 2734 | |
| 2735 | return sw_ctx_rx; |
| 2736 | } |
| 2737 | |
| 2738 | int init_prot_info(struct tls_prot_info *prot, |
| 2739 | const struct tls_crypto_info *crypto_info, |
| 2740 | const struct tls_cipher_desc *cipher_desc) |
| 2741 | { |
| 2742 | u16 nonce_size = cipher_desc->nonce; |
| 2743 | |
| 2744 | if (crypto_info->version == TLS_1_3_VERSION) { |
| 2745 | nonce_size = 0; |
| 2746 | prot->aad_size = TLS_HEADER_SIZE; |
| 2747 | prot->tail_size = 1; |
| 2748 | } else { |
| 2749 | prot->aad_size = TLS_AAD_SPACE_SIZE; |
| 2750 | prot->tail_size = 0; |
| 2751 | } |
| 2752 | |
| 2753 | /* Sanity-check the sizes for stack allocations. */ |
| 2754 | if (nonce_size > TLS_MAX_IV_SIZE || prot->aad_size > TLS_MAX_AAD_SIZE) |
| 2755 | return -EINVAL; |
| 2756 | |
| 2757 | prot->version = crypto_info->version; |
| 2758 | prot->cipher_type = crypto_info->cipher_type; |
| 2759 | prot->prepend_size = TLS_HEADER_SIZE + nonce_size; |
| 2760 | prot->tag_size = cipher_desc->tag; |
| 2761 | prot->overhead_size = prot->prepend_size + prot->tag_size + prot->tail_size; |
| 2762 | prot->iv_size = cipher_desc->iv; |
| 2763 | prot->salt_size = cipher_desc->salt; |
| 2764 | prot->rec_seq_size = cipher_desc->rec_seq; |
| 2765 | |
| 2766 | return 0; |
| 2767 | } |
| 2768 | |
| 2769 | static void tls_finish_key_update(struct sock *sk, struct tls_context *tls_ctx) |
| 2770 | { |
| 2771 | struct tls_sw_context_rx *ctx = tls_ctx->priv_ctx_rx; |
| 2772 | |
| 2773 | WRITE_ONCE(ctx->key_update_pending, false); |
| 2774 | /* wake-up pre-existing poll() */ |
| 2775 | ctx->saved_data_ready(sk); |
| 2776 | } |
| 2777 | |
| 2778 | int tls_set_sw_offload(struct sock *sk, int tx, |
| 2779 | struct tls_crypto_info *new_crypto_info) |
| 2780 | { |
| 2781 | struct tls_crypto_info *crypto_info, *src_crypto_info; |
| 2782 | struct tls_sw_context_tx *sw_ctx_tx = NULL; |
| 2783 | struct tls_sw_context_rx *sw_ctx_rx = NULL; |
| 2784 | const struct tls_cipher_desc *cipher_desc; |
| 2785 | char *iv, *rec_seq, *key, *salt; |
| 2786 | struct cipher_context *cctx; |
| 2787 | struct tls_prot_info *prot; |
| 2788 | struct crypto_aead **aead; |
| 2789 | struct tls_context *ctx; |
| 2790 | struct crypto_tfm *tfm; |
| 2791 | int rc = 0; |
| 2792 | |
| 2793 | ctx = tls_get_ctx(sk); |
| 2794 | prot = &ctx->prot_info; |
| 2795 | |
| 2796 | /* new_crypto_info != NULL means rekey */ |
| 2797 | if (!new_crypto_info) { |
| 2798 | if (tx) { |
| 2799 | ctx->priv_ctx_tx = init_ctx_tx(ctx, sk); |
| 2800 | if (!ctx->priv_ctx_tx) |
| 2801 | return -ENOMEM; |
| 2802 | } else { |
| 2803 | ctx->priv_ctx_rx = init_ctx_rx(ctx); |
| 2804 | if (!ctx->priv_ctx_rx) |
| 2805 | return -ENOMEM; |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | if (tx) { |
| 2810 | sw_ctx_tx = ctx->priv_ctx_tx; |
| 2811 | crypto_info = &ctx->crypto_send.info; |
| 2812 | cctx = &ctx->tx; |
| 2813 | aead = &sw_ctx_tx->aead_send; |
| 2814 | } else { |
| 2815 | sw_ctx_rx = ctx->priv_ctx_rx; |
| 2816 | crypto_info = &ctx->crypto_recv.info; |
| 2817 | cctx = &ctx->rx; |
| 2818 | aead = &sw_ctx_rx->aead_recv; |
| 2819 | } |
| 2820 | |
| 2821 | src_crypto_info = new_crypto_info ?: crypto_info; |
| 2822 | |
| 2823 | cipher_desc = get_cipher_desc(cipher_type: src_crypto_info->cipher_type); |
| 2824 | if (!cipher_desc) { |
| 2825 | rc = -EINVAL; |
| 2826 | goto free_priv; |
| 2827 | } |
| 2828 | |
| 2829 | rc = init_prot_info(prot, crypto_info: src_crypto_info, cipher_desc); |
| 2830 | if (rc) |
| 2831 | goto free_priv; |
| 2832 | |
| 2833 | iv = crypto_info_iv(crypto_info: src_crypto_info, cipher_desc); |
| 2834 | key = crypto_info_key(crypto_info: src_crypto_info, cipher_desc); |
| 2835 | salt = crypto_info_salt(crypto_info: src_crypto_info, cipher_desc); |
| 2836 | rec_seq = crypto_info_rec_seq(crypto_info: src_crypto_info, cipher_desc); |
| 2837 | |
| 2838 | if (!*aead) { |
| 2839 | *aead = crypto_alloc_aead(alg_name: cipher_desc->cipher_name, type: 0, mask: 0); |
| 2840 | if (IS_ERR(ptr: *aead)) { |
| 2841 | rc = PTR_ERR(ptr: *aead); |
| 2842 | *aead = NULL; |
| 2843 | goto free_priv; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | ctx->push_pending_record = tls_sw_push_pending_record; |
| 2848 | |
| 2849 | /* setkey is the last operation that could fail during a |
| 2850 | * rekey. if it succeeds, we can start modifying the |
| 2851 | * context. |
| 2852 | */ |
| 2853 | rc = crypto_aead_setkey(tfm: *aead, key, keylen: cipher_desc->key); |
| 2854 | if (rc) { |
| 2855 | if (new_crypto_info) |
| 2856 | goto out; |
| 2857 | else |
| 2858 | goto free_aead; |
| 2859 | } |
| 2860 | |
| 2861 | if (!new_crypto_info) { |
| 2862 | rc = crypto_aead_setauthsize(tfm: *aead, authsize: prot->tag_size); |
| 2863 | if (rc) |
| 2864 | goto free_aead; |
| 2865 | } |
| 2866 | |
| 2867 | if (!tx && !new_crypto_info) { |
| 2868 | tfm = crypto_aead_tfm(tfm: sw_ctx_rx->aead_recv); |
| 2869 | |
| 2870 | tls_update_rx_zc_capable(tls_ctx: ctx); |
| 2871 | sw_ctx_rx->async_capable = |
| 2872 | src_crypto_info->version != TLS_1_3_VERSION && |
| 2873 | !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC); |
| 2874 | |
| 2875 | rc = tls_strp_init(strp: &sw_ctx_rx->strp, sk); |
| 2876 | if (rc) |
| 2877 | goto free_aead; |
| 2878 | } |
| 2879 | |
| 2880 | memcpy(cctx->iv, salt, cipher_desc->salt); |
| 2881 | memcpy(cctx->iv + cipher_desc->salt, iv, cipher_desc->iv); |
| 2882 | memcpy(cctx->rec_seq, rec_seq, cipher_desc->rec_seq); |
| 2883 | |
| 2884 | if (new_crypto_info) { |
| 2885 | unsafe_memcpy(crypto_info, new_crypto_info, |
| 2886 | cipher_desc->crypto_info, |
| 2887 | /* size was checked in do_tls_setsockopt_conf */); |
| 2888 | memzero_explicit(s: new_crypto_info, count: cipher_desc->crypto_info); |
| 2889 | if (!tx) |
| 2890 | tls_finish_key_update(sk, tls_ctx: ctx); |
| 2891 | } |
| 2892 | |
| 2893 | goto out; |
| 2894 | |
| 2895 | free_aead: |
| 2896 | crypto_free_aead(tfm: *aead); |
| 2897 | *aead = NULL; |
| 2898 | free_priv: |
| 2899 | if (!new_crypto_info) { |
| 2900 | if (tx) { |
| 2901 | kfree(objp: ctx->priv_ctx_tx); |
| 2902 | ctx->priv_ctx_tx = NULL; |
| 2903 | } else { |
| 2904 | kfree(objp: ctx->priv_ctx_rx); |
| 2905 | ctx->priv_ctx_rx = NULL; |
| 2906 | } |
| 2907 | } |
| 2908 | out: |
| 2909 | return rc; |
| 2910 | } |
| 2911 | |