LCOV - code coverage report
Current view: top level - tls - s2n_ktls_io.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 219 231 94.8 %
Date: 2025-09-30 07:28:05 Functions: 14 14 100.0 %
Branches: 171 302 56.6 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License").
       5                 :            :  * You may not use this file except in compliance with the License.
       6                 :            :  * A copy of the License is located at
       7                 :            :  *
       8                 :            :  *  http://aws.amazon.com/apache2.0
       9                 :            :  *
      10                 :            :  * or in the "license" file accompanying this file. This file is distributed
      11                 :            :  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
      12                 :            :  * express or implied. See the License for the specific language governing
      13                 :            :  * permissions and limitations under the License.
      14                 :            :  */
      15                 :            : 
      16                 :            : #if defined(__FreeBSD__) || defined(__APPLE__)
      17                 :            :     /* https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html
      18                 :            :      * The POSIX standard does not define the CMSG_LEN and CMSG_SPACE macros. FreeBSD
      19                 :            :      * and APPLE check and disable these macros if the _POSIX_C_SOURCE flag is set.
      20                 :            :      *
      21                 :            :      * Since s2n-tls already unsets the _POSIX_C_SOURCE in other files and is not
      22                 :            :      * POSIX compliant, we continue the pattern here.
      23                 :            :      */
      24                 :            :     #undef _POSIX_C_SOURCE
      25                 :            : #endif
      26                 :            : #include <sys/socket.h>
      27                 :            : 
      28                 :            : #ifdef S2N_LINUX_SENDFILE
      29                 :            :     #include <sys/sendfile.h>
      30                 :            : #endif
      31                 :            : 
      32                 :            : #include "crypto/s2n_sequence.h"
      33                 :            : #include "error/s2n_errno.h"
      34                 :            : #include "tls/s2n_ktls.h"
      35                 :            : #include "tls/s2n_tls.h"
      36                 :            : #include "utils/s2n_io.h"
      37                 :            : #include "utils/s2n_result.h"
      38                 :            : #include "utils/s2n_safety.h"
      39                 :            : #include "utils/s2n_socket.h"
      40                 :            : 
      41                 :            : /* record_type is of type uint8_t */
      42                 :            : #define S2N_KTLS_RECORD_TYPE_SIZE    (sizeof(uint8_t))
      43                 :            : #define S2N_KTLS_CONTROL_BUFFER_SIZE (CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE))
      44                 :            : 
      45                 :            : #define S2N_MAX_STACK_IOVECS     16
      46                 :            : #define S2N_MAX_STACK_IOVECS_MEM (S2N_MAX_STACK_IOVECS * sizeof(struct iovec))
      47                 :            : 
      48                 :            : /* Used to override sendmsg and recvmsg for testing. */
      49                 :            : static ssize_t s2n_ktls_default_sendmsg(void *io_context, const struct msghdr *msg);
      50                 :            : static ssize_t s2n_ktls_default_recvmsg(void *io_context, struct msghdr *msg);
      51                 :            : s2n_ktls_sendmsg_fn s2n_sendmsg_fn = s2n_ktls_default_sendmsg;
      52                 :            : s2n_ktls_recvmsg_fn s2n_recvmsg_fn = s2n_ktls_default_recvmsg;
      53                 :            : 
      54                 :            : S2N_RESULT s2n_ktls_set_sendmsg_cb(struct s2n_connection *conn, s2n_ktls_sendmsg_fn send_cb,
      55                 :            :         void *send_ctx)
      56                 :      82007 : {
      57 [ -  + ][ #  # ]:      82007 :     RESULT_ENSURE_REF(conn);
      58 [ -  + ][ #  # ]:      82007 :     RESULT_ENSURE_REF(send_ctx);
      59 [ #  # ][ -  + ]:      82007 :     RESULT_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST);
      60                 :      82007 :     conn->send_io_context = send_ctx;
      61                 :      82007 :     s2n_sendmsg_fn = send_cb;
      62                 :      82007 :     return S2N_RESULT_OK;
      63                 :      82007 : }
      64                 :            : 
      65                 :            : S2N_RESULT s2n_ktls_set_recvmsg_cb(struct s2n_connection *conn, s2n_ktls_recvmsg_fn recv_cb,
      66                 :            :         void *recv_ctx)
      67                 :      32833 : {
      68 [ -  + ][ #  # ]:      32833 :     RESULT_ENSURE_REF(conn);
      69 [ -  + ][ #  # ]:      32833 :     RESULT_ENSURE_REF(recv_ctx);
      70 [ -  + ][ #  # ]:      32833 :     RESULT_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST);
      71                 :      32833 :     conn->recv_io_context = recv_ctx;
      72                 :      32833 :     s2n_recvmsg_fn = recv_cb;
      73                 :      32833 :     return S2N_RESULT_OK;
      74                 :      32833 : }
      75                 :            : 
      76                 :            : static ssize_t s2n_ktls_default_recvmsg(void *io_context, struct msghdr *msg)
      77                 :          1 : {
      78 [ +  - ][ +  - ]:          1 :     POSIX_ENSURE_REF(io_context);
      79 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(msg);
      80                 :            : 
      81                 :          0 :     const struct s2n_socket_read_io_context *peer_socket_ctx = io_context;
      82 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(peer_socket_ctx);
      83                 :          0 :     int fd = peer_socket_ctx->fd;
      84                 :            : 
      85                 :          0 :     return recvmsg(fd, msg, 0);
      86                 :          0 : }
      87                 :            : 
      88                 :            : static ssize_t s2n_ktls_default_sendmsg(void *io_context, const struct msghdr *msg)
      89                 :          1 : {
      90 [ +  - ][ +  - ]:          1 :     POSIX_ENSURE_REF(io_context);
      91 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(msg);
      92                 :            : 
      93                 :          0 :     const struct s2n_socket_write_io_context *peer_socket_ctx = io_context;
      94 [ #  # ][ #  # ]:          0 :     POSIX_ENSURE_REF(peer_socket_ctx);
      95                 :          0 :     int fd = peer_socket_ctx->fd;
      96                 :            : 
      97                 :          0 :     return sendmsg(fd, msg, 0);
      98                 :          0 : }
      99                 :            : 
     100                 :            : S2N_RESULT s2n_ktls_set_control_data(struct msghdr *msg, char *buf, size_t buf_size,
     101                 :            :         int cmsg_type, uint8_t record_type)
     102                 :      82430 : {
     103 [ +  - ][ +  + ]:      82430 :     RESULT_ENSURE_REF(msg);
     104 [ +  + ][ +  - ]:      82429 :     RESULT_ENSURE_REF(buf);
     105                 :            : 
     106                 :            :     /*
     107                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     108                 :            :      * To create ancillary data, first initialize the msg_controllen
     109                 :            :      * member of the msghdr with the length of the control message
     110                 :            :      * buffer.
     111                 :            :      */
     112                 :      82428 :     msg->msg_control = buf;
     113                 :      82428 :     msg->msg_controllen = buf_size;
     114                 :            : 
     115                 :            :     /*
     116                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     117                 :            :      * Use CMSG_FIRSTHDR() on the msghdr to get the first
     118                 :            :      * control message and CMSG_NXTHDR() to get all subsequent ones.
     119                 :            :      */
     120                 :      82428 :     struct cmsghdr *hdr = CMSG_FIRSTHDR(msg);
     121 [ +  + ][ +  - ]:      82428 :     RESULT_ENSURE_REF(hdr);
     122                 :            : 
     123                 :            :     /*
     124                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     125                 :            :      * In each control message, initialize cmsg_len (with CMSG_LEN()), the
     126                 :            :      * other cmsghdr header fields, and the data portion using
     127                 :            :      * CMSG_DATA().
     128                 :            :      */
     129                 :      82427 :     hdr->cmsg_len = CMSG_LEN(S2N_KTLS_RECORD_TYPE_SIZE);
     130                 :      82427 :     hdr->cmsg_level = S2N_SOL_TLS;
     131                 :      82427 :     hdr->cmsg_type = cmsg_type;
     132                 :      82427 :     *CMSG_DATA(hdr) = record_type;
     133                 :            : 
     134                 :            :     /*
     135                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     136                 :            :      * Finally, the msg_controllen field of the msghdr
     137                 :            :      * should be set to the sum of the CMSG_SPACE() of the length of all
     138                 :            :      * control messages in the buffer
     139                 :            :      */
     140 [ -  + ][ #  # ]:      82427 :     RESULT_ENSURE_GTE(msg->msg_controllen, CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE));
     141                 :      82427 :     msg->msg_controllen = CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE);
     142                 :            : 
     143                 :      82427 :     return S2N_RESULT_OK;
     144                 :      82427 : }
     145                 :            : 
     146                 :            : /* Expect to receive a single cmsghdr containing the TLS record_type.
     147                 :            :  *
     148                 :            :  * s2n-tls allocates enough space to receive a single cmsghdr. Since this is
     149                 :            :  * used to get the record_type when receiving over kTLS (enabled via
     150                 :            :  * `s2n_connection_ktls_enable_recv`), the application should not configure
     151                 :            :  * the socket to receive additional control messages. In the event s2n-tls
     152                 :            :  * can not retrieve the record_type, it is safer to drop the record.
     153                 :            :  */
     154                 :            : S2N_RESULT s2n_ktls_get_control_data(struct msghdr *msg, int cmsg_type, uint8_t *record_type)
     155                 :      82387 : {
     156 [ +  + ][ +  - ]:      82387 :     RESULT_ENSURE_REF(msg);
     157 [ +  + ][ +  - ]:      82386 :     RESULT_ENSURE_REF(record_type);
     158                 :            : 
     159                 :            :     /* https://man7.org/linux/man-pages/man3/recvmsg.3p.html
     160                 :            :      * MSG_CTRUNC  Control data was truncated.
     161                 :            :      */
     162         [ +  + ]:      82385 :     if (msg->msg_flags & MSG_CTRUNC) {
     163         [ +  - ]:          1 :         RESULT_BAIL(S2N_ERR_KTLS_BAD_CMSG);
     164                 :          1 :     }
     165                 :            : 
     166                 :            :     /*
     167                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     168                 :            :      * To create ancillary data, first initialize the msg_controllen
     169                 :            :      * member of the msghdr with the length of the control message
     170                 :            :      * buffer.
     171                 :            :      */
     172 [ -  + ][ #  # ]:      82384 :     RESULT_ENSURE(msg->msg_control, S2N_ERR_SAFETY);
     173 [ -  + ][ #  # ]:      82384 :     RESULT_ENSURE(msg->msg_controllen >= CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE), S2N_ERR_SAFETY);
     174                 :            : 
     175                 :            :     /* https://man7.org/linux/man-pages/man3/cmsg.3.html
     176                 :            :      * Use CMSG_FIRSTHDR() on the msghdr to get the first
     177                 :            :      * control message and CMSG_NXTHDR() to get all subsequent ones.
     178                 :            :      */
     179                 :      82384 :     struct cmsghdr *hdr = CMSG_FIRSTHDR(msg);
     180 [ -  + ][ #  # ]:      82384 :     RESULT_ENSURE(hdr, S2N_ERR_KTLS_BAD_CMSG);
     181                 :            : 
     182                 :            :     /*
     183                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     184                 :            :      * In each control message, initialize cmsg_len (with CMSG_LEN()), the
     185                 :            :      * other cmsghdr header fields, and the data portion using
     186                 :            :      * CMSG_DATA().
     187                 :            :      */
     188 [ -  + ][ #  # ]:      82384 :     RESULT_ENSURE(hdr->cmsg_level == S2N_SOL_TLS, S2N_ERR_KTLS_BAD_CMSG);
     189 [ +  + ][ +  - ]:      82384 :     RESULT_ENSURE(hdr->cmsg_type == cmsg_type, S2N_ERR_KTLS_BAD_CMSG);
     190 [ -  + ][ #  # ]:      82383 :     RESULT_ENSURE(hdr->cmsg_len == CMSG_LEN(S2N_KTLS_RECORD_TYPE_SIZE), S2N_ERR_KTLS_BAD_CMSG);
     191                 :      82383 :     *record_type = *CMSG_DATA(hdr);
     192                 :            : 
     193                 :      82383 :     return S2N_RESULT_OK;
     194                 :      82383 : }
     195                 :            : 
     196                 :            : S2N_RESULT s2n_ktls_sendmsg(void *io_context, uint8_t record_type, const struct iovec *msg_iov,
     197                 :            :         size_t msg_iovlen, s2n_blocked_status *blocked, size_t *bytes_written)
     198                 :      49508 : {
     199 [ +  - ][ +  + ]:      49508 :     RESULT_ENSURE_REF(bytes_written);
     200 [ +  - ][ +  + ]:      49507 :     RESULT_ENSURE_REF(blocked);
     201 [ +  - ][ +  + ]:      49505 :     RESULT_ENSURE(msg_iov != NULL || msg_iovlen == 0, S2N_ERR_NULL);
                 [ +  + ]
     202                 :            : 
     203                 :      49504 :     *blocked = S2N_BLOCKED_ON_WRITE;
     204                 :      49504 :     *bytes_written = 0;
     205                 :            : 
     206                 :      49504 :     struct msghdr msg = {
     207                 :            :         /* msghdr requires a non-const iovec. This is safe because s2n-tls does
     208                 :            :          * not modify msg_iov after this point.
     209                 :            :          */
     210                 :      49504 :         .msg_iov = (struct iovec *) (uintptr_t) msg_iov,
     211                 :      49504 :         .msg_iovlen = msg_iovlen,
     212                 :      49504 :     };
     213                 :            : 
     214                 :      49504 :     char control_data[S2N_KTLS_CONTROL_BUFFER_SIZE] = { 0 };
     215         [ -  + ]:      49504 :     RESULT_GUARD(s2n_ktls_set_control_data(&msg, control_data, sizeof(control_data),
     216                 :      49504 :             S2N_TLS_SET_RECORD_TYPE, record_type));
     217                 :            : 
     218                 :      49504 :     ssize_t result = 0;
     219 [ +  + ][ -  + ]:      49504 :     S2N_IO_RETRY_EINTR(result, s2n_sendmsg_fn(io_context, &msg));
     220         [ +  + ]:      49504 :     RESULT_GUARD(s2n_io_check_write_result(result));
     221                 :            : 
     222                 :      49487 :     *blocked = S2N_NOT_BLOCKED;
     223                 :      49487 :     *bytes_written = result;
     224                 :      49487 :     return S2N_RESULT_OK;
     225                 :      49504 : }
     226                 :            : 
     227                 :            : S2N_RESULT s2n_ktls_recvmsg(void *io_context, uint8_t *record_type, uint8_t *buf,
     228                 :            :         size_t buf_len, s2n_blocked_status *blocked, size_t *bytes_read)
     229                 :        147 : {
     230 [ +  - ][ +  + ]:        147 :     RESULT_ENSURE_REF(record_type);
     231 [ +  - ][ +  + ]:        146 :     RESULT_ENSURE_REF(bytes_read);
     232 [ +  + ][ +  - ]:        145 :     RESULT_ENSURE_REF(blocked);
     233 [ +  + ][ +  - ]:        144 :     RESULT_ENSURE_REF(buf);
     234                 :            :     /* Ensure that buf_len is > 0 since trying to receive 0 bytes does not
     235                 :            :      * make sense and a return value of `0` from recvmsg is treated as EOF.
     236                 :            :      */
     237 [ +  + ][ +  - ]:        143 :     RESULT_ENSURE_GT(buf_len, 0);
     238                 :            : 
     239                 :        142 :     *blocked = S2N_BLOCKED_ON_READ;
     240                 :        142 :     *record_type = 0;
     241                 :        142 :     *bytes_read = 0;
     242                 :        142 :     struct iovec msg_iov = {
     243                 :        142 :         .iov_base = buf,
     244                 :        142 :         .iov_len = buf_len
     245                 :        142 :     };
     246                 :        142 :     struct msghdr msg = {
     247                 :        142 :         .msg_iov = &msg_iov,
     248                 :        142 :         .msg_iovlen = 1,
     249                 :        142 :     };
     250                 :            : 
     251                 :            :     /*
     252                 :            :      * https://man7.org/linux/man-pages/man3/cmsg.3.html
     253                 :            :      * To create ancillary data, first initialize the msg_controllen
     254                 :            :      * member of the msghdr with the length of the control message
     255                 :            :      * buffer.
     256                 :            :      */
     257                 :        142 :     char control_data[S2N_KTLS_CONTROL_BUFFER_SIZE] = { 0 };
     258                 :        142 :     msg.msg_controllen = sizeof(control_data);
     259                 :        142 :     msg.msg_control = control_data;
     260                 :            : 
     261                 :        142 :     ssize_t result = 0;
     262 [ +  + ][ -  + ]:        142 :     S2N_IO_RETRY_EINTR(result, s2n_recvmsg_fn(io_context, &msg));
     263         [ +  + ]:        142 :     RESULT_GUARD(s2n_io_check_read_result(result));
     264                 :            : 
     265         [ +  + ]:        122 :     RESULT_GUARD(s2n_ktls_get_control_data(&msg, S2N_TLS_GET_RECORD_TYPE, record_type));
     266                 :            : 
     267                 :        121 :     *blocked = S2N_NOT_BLOCKED;
     268                 :        121 :     *bytes_read = result;
     269                 :        121 :     return S2N_RESULT_OK;
     270                 :        122 : }
     271                 :            : 
     272                 :            : /* The iovec array `bufs` is constant and owned by the application.
     273                 :            :  *
     274                 :            :  * However, we need to apply the given offset to `bufs`. That may involve
     275                 :            :  * updating the iov_base and iov_len of entries in `bufs` to reflect the bytes
     276                 :            :  * already sent. Because `bufs` is constant, we need to instead copy `bufs` and
     277                 :            :  * modify the copy.
     278                 :            :  *
     279                 :            :  * Since one of the primary benefits of kTLS is that we avoid buffering application
     280                 :            :  * data and can pass application data as-is to the kernel, we try to limit the
     281                 :            :  * situations where we need to copy `bufs` and use stack memory where possible.
     282                 :            :  *
     283                 :            :  * Note: We are copying an array of iovecs here, NOT the scattered application
     284                 :            :  * data the iovecs reference. On Linux, the maximum data copied would be
     285                 :            :  * 1024 (IOV_MAX on Linux) * 16 (sizeof(struct iovec)) = ~16KB.
     286                 :            :  *
     287                 :            :  * To avoid any copies when using a large number of iovecs, applications should
     288                 :            :  * call s2n_sendv instead of s2n_sendv_with_offset.
     289                 :            :  */
     290                 :            : static S2N_RESULT s2n_ktls_update_bufs_with_offset(const struct iovec **bufs, size_t *count,
     291                 :            :         size_t offs, struct s2n_blob *mem)
     292                 :      32776 : {
     293 [ -  + ][ #  # ]:      32776 :     RESULT_ENSURE_REF(bufs);
     294 [ -  + ][ #  # ]:      32776 :     RESULT_ENSURE_REF(count);
     295 [ #  # ][ +  - ]:      32776 :     RESULT_ENSURE(*bufs != NULL || *count == 0, S2N_ERR_NULL);
                 [ #  # ]
     296 [ #  # ][ -  + ]:      32776 :     RESULT_ENSURE_REF(mem);
     297                 :            : 
     298                 :      32776 :     size_t skipped = 0;
     299         [ +  + ]:    1763849 :     while (offs > 0) {
     300                 :            :         /* If we need to skip more iovecs than actually exist,
     301                 :            :          * then the offset is too large and therefore invalid.
     302                 :            :          */
     303 [ -  + ][ #  # ]:    1763832 :         RESULT_ENSURE(skipped < *count, S2N_ERR_INVALID_ARGUMENT);
     304                 :            : 
     305                 :    1763832 :         size_t iov_len = (*bufs)[skipped].iov_len;
     306                 :            : 
     307                 :            :         /* This is the last iovec affected by the offset. */
     308         [ +  + ]:    1763832 :         if (offs < iov_len) {
     309                 :      32759 :             break;
     310                 :      32759 :         }
     311                 :            : 
     312                 :    1731073 :         offs -= iov_len;
     313                 :    1731073 :         skipped++;
     314                 :    1731073 :     }
     315                 :            : 
     316                 :      32776 :     *count = (*count) - skipped;
     317         [ +  + ]:      32776 :     if (*count == 0) {
     318                 :          2 :         return S2N_RESULT_OK;
     319                 :          2 :     }
     320                 :            : 
     321                 :      32774 :     *bufs = &(*bufs)[skipped];
     322         [ +  + ]:      32774 :     if (offs == 0) {
     323                 :         15 :         return S2N_RESULT_OK;
     324                 :         15 :     }
     325                 :            : 
     326                 :      32759 :     size_t size = (*count) * (sizeof(struct iovec));
     327                 :            :     /* If possible, use the existing stack memory in `mem` for the copy.
     328                 :            :      * Otherwise, we need to allocate sufficient new heap memory. */
     329         [ +  + ]:      32759 :     if (size > mem->size) {
     330         [ -  + ]:        222 :         RESULT_GUARD_POSIX(s2n_alloc(mem, size));
     331                 :        222 :     }
     332                 :            : 
     333                 :      32759 :     struct iovec *new_bufs = (struct iovec *) (void *) mem->data;
     334 [ #  # ][ -  + ]:      32759 :     RESULT_CHECKED_MEMCPY(new_bufs, *bufs, size);
                 [ +  - ]
     335                 :      32759 :     new_bufs[0].iov_base = (uint8_t *) new_bufs[0].iov_base + offs;
     336                 :      32759 :     new_bufs[0].iov_len = new_bufs[0].iov_len - offs;
     337                 :      32759 :     *bufs = new_bufs;
     338                 :            : 
     339                 :      32759 :     return S2N_RESULT_OK;
     340                 :      32759 : }
     341                 :            : 
     342                 :            : ssize_t s2n_ktls_sendv_with_offset(struct s2n_connection *conn, const struct iovec *bufs,
     343                 :            :         ssize_t count_in, ssize_t offs_in, s2n_blocked_status *blocked)
     344                 :      49224 : {
     345 [ +  + ][ +  - ]:      49224 :     POSIX_ENSURE_REF(conn);
     346 [ +  - ][ +  + ]:      49223 :     POSIX_ENSURE(count_in >= 0, S2N_ERR_INVALID_ARGUMENT);
     347                 :      49222 :     size_t count = count_in;
     348 [ +  + ][ +  - ]:      49222 :     POSIX_ENSURE(offs_in >= 0, S2N_ERR_INVALID_ARGUMENT);
     349                 :      49221 :     size_t offs = offs_in;
     350                 :            : 
     351                 :      49221 :     ssize_t total_bytes = 0;
     352         [ +  + ]:      49221 :     POSIX_GUARD_RESULT(s2n_sendv_with_offset_total_size(bufs, count_in, offs_in, &total_bytes));
     353         [ +  + ]:      49218 :     POSIX_GUARD_RESULT(s2n_ktls_key_update_send(conn, total_bytes));
     354                 :            : 
     355                 :            :     /* The order of new_bufs and new_bufs_mem matters. See https://github.com/aws/s2n-tls/issues/4354 */
     356                 :      49212 :     uint8_t new_bufs_mem[S2N_MAX_STACK_IOVECS_MEM] = { 0 };
     357                 :      49212 :     DEFER_CLEANUP(struct s2n_blob new_bufs = { 0 }, s2n_free_or_wipe);
     358         [ -  + ]:      49212 :     POSIX_GUARD(s2n_blob_init(&new_bufs, new_bufs_mem, sizeof(new_bufs_mem)));
     359         [ +  + ]:      49212 :     if (offs > 0) {
     360         [ -  + ]:      32776 :         POSIX_GUARD_RESULT(s2n_ktls_update_bufs_with_offset(&bufs, &count, offs, &new_bufs));
     361                 :      32776 :     }
     362                 :            : 
     363                 :      49212 :     size_t bytes_written = 0;
     364         [ +  + ]:      49212 :     POSIX_GUARD_RESULT(s2n_ktls_sendmsg(conn->send_io_context, TLS_APPLICATION_DATA,
     365                 :      49209 :             bufs, count, blocked, &bytes_written));
     366                 :            : 
     367         [ -  + ]:      49209 :     POSIX_GUARD_RESULT(s2n_ktls_set_estimated_sequence_number(conn, bytes_written));
     368                 :      49209 :     return bytes_written;
     369                 :      49209 : }
     370                 :            : 
     371                 :            : int s2n_ktls_send_cb(void *io_context, const uint8_t *buf, uint32_t len)
     372                 :         19 : {
     373 [ +  + ][ +  - ]:         19 :     POSIX_ENSURE_REF(io_context);
     374 [ +  + ][ +  - ]:         18 :     POSIX_ENSURE_REF(buf);
     375                 :            : 
     376                 :            :     /* For now, all control records are assumed to be alerts.
     377                 :            :      * We can set the record_type on the io_context in the future.
     378                 :            :      */
     379                 :         17 :     const uint8_t record_type = TLS_ALERT;
     380                 :            : 
     381                 :         17 :     const struct iovec iov = {
     382                 :         17 :         .iov_base = (void *) (uintptr_t) buf,
     383                 :         17 :         .iov_len = len,
     384                 :         17 :     };
     385                 :         17 :     s2n_blocked_status blocked = S2N_NOT_BLOCKED;
     386                 :         17 :     size_t bytes_written = 0;
     387                 :            : 
     388         [ +  + ]:         17 :     POSIX_GUARD_RESULT(s2n_ktls_sendmsg(io_context, record_type, &iov, 1,
     389                 :         11 :             &blocked, &bytes_written));
     390                 :            : 
     391 [ -  + ][ #  # ]:         11 :     POSIX_ENSURE_LTE(bytes_written, len);
     392                 :         11 :     return bytes_written;
     393                 :         11 : }
     394                 :            : 
     395                 :            : int s2n_ktls_record_writev(struct s2n_connection *conn, uint8_t content_type,
     396                 :            :         const struct iovec *in, int in_count, size_t offs, size_t to_write)
     397                 :         13 : {
     398 [ +  - ][ +  + ]:         13 :     POSIX_ENSURE_REF(conn);
     399 [ +  + ][ +  - ]:         12 :     POSIX_ENSURE(in_count > 0, S2N_ERR_INVALID_ARGUMENT);
     400                 :         11 :     size_t count = in_count;
     401 [ +  + ][ +  - ]:         11 :     POSIX_ENSURE_REF(in);
     402                 :            : 
     403                 :            :     /* Currently, ktls only supports sending alerts.
     404                 :            :      * To also support handshake messages, we would need a way to track record_type.
     405                 :            :      * We could add a field to the send io context.
     406                 :            :      */
     407 [ +  - ][ +  + ]:         10 :     POSIX_ENSURE(content_type == TLS_ALERT, S2N_ERR_UNIMPLEMENTED);
     408                 :            : 
     409                 :            :     /* When stuffers automatically resize, they allocate a potentially large
     410                 :            :      * chunk of memory to avoid repeated resizes.
     411                 :            :      * Since ktls only uses conn->out for control messages (alerts and eventually
     412                 :            :      * handshake messages), we expect infrequent small writes with conn->out
     413                 :            :      * freed in between. Since we're therefore more concerned with the size of
     414                 :            :      * the allocation than the frequency, use a more accurate size for each write.
     415                 :            :      */
     416         [ -  + ]:          9 :     POSIX_GUARD(s2n_stuffer_resize_if_empty(&conn->out, to_write));
     417                 :            : 
     418         [ -  + ]:          9 :     POSIX_GUARD(s2n_stuffer_writev_bytes(&conn->out, in, count, offs, to_write));
     419                 :          9 :     return to_write;
     420                 :          9 : }
     421                 :            : 
     422                 :            : int s2n_sendfile(struct s2n_connection *conn, int in_fd, off_t offset, size_t count,
     423                 :            :         size_t *bytes_written, s2n_blocked_status *blocked)
     424                 :       1488 : {
     425 [ +  - ][ +  + ]:       1488 :     POSIX_ENSURE_REF(blocked);
     426                 :       1487 :     *blocked = S2N_BLOCKED_ON_WRITE;
     427 [ +  + ][ +  - ]:       1487 :     POSIX_ENSURE_REF(bytes_written);
     428                 :       1486 :     *bytes_written = 0;
     429 [ +  - ][ +  + ]:       1486 :     POSIX_ENSURE_REF(conn);
     430 [ -  + ][ #  # ]:       1485 :     POSIX_ENSURE(conn->ktls_send_enabled, S2N_ERR_KTLS_UNSUPPORTED_CONN);
     431         [ +  + ]:       1485 :     POSIX_GUARD_RESULT(s2n_ktls_key_update_send(conn, count));
     432                 :            : 
     433                 :       1484 :     int out_fd = 0;
     434         [ -  + ]:       1484 :     POSIX_GUARD_RESULT(s2n_ktls_get_file_descriptor(conn, S2N_KTLS_MODE_SEND, &out_fd));
     435                 :            : 
     436                 :       1484 : #ifdef S2N_LINUX_SENDFILE
     437                 :            :     /* https://man7.org/linux/man-pages/man2/sendfile.2.html */
     438                 :       1484 :     ssize_t result = 0;
     439 [ +  + ][ -  + ]:       1484 :     S2N_IO_RETRY_EINTR(result, sendfile(out_fd, in_fd, &offset, count));
     440         [ +  + ]:       1484 :     POSIX_GUARD_RESULT(s2n_io_check_write_result(result));
     441                 :       1482 :     *bytes_written = result;
     442                 :            : #else
     443                 :            :     POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
     444                 :            : #endif
     445                 :            : 
     446         [ -  + ]:       1482 :     POSIX_GUARD_RESULT(s2n_ktls_set_estimated_sequence_number(conn, *bytes_written));
     447                 :       1482 :     *blocked = S2N_NOT_BLOCKED;
     448                 :       1482 :     return S2N_SUCCESS;
     449                 :       1482 : }
     450                 :            : 
     451                 :            : int s2n_ktls_read_full_record(struct s2n_connection *conn, uint8_t *record_type)
     452                 :        128 : {
     453 [ +  - ][ +  + ]:        128 :     POSIX_ENSURE_REF(conn);
     454 [ +  - ][ +  + ]:        127 :     POSIX_ENSURE_REF(record_type);
     455                 :            : 
     456                 :            :     /* If any unread data remains in conn->in, it must be application data that
     457                 :            :      * couldn't be returned due to the size of the application's provided buffer.
     458                 :            :      */
     459         [ +  + ]:        126 :     if (s2n_stuffer_data_available(&conn->in)) {
     460                 :          2 :         *record_type = TLS_APPLICATION_DATA;
     461                 :          2 :         return S2N_SUCCESS;
     462                 :          2 :     }
     463                 :            : 
     464         [ -  + ]:        124 :     POSIX_GUARD(s2n_stuffer_resize_if_empty(&conn->buffer_in, S2N_DEFAULT_FRAGMENT_LENGTH));
     465                 :            : 
     466                 :        124 :     struct s2n_stuffer record_stuffer = conn->buffer_in;
     467                 :        124 :     size_t len = s2n_stuffer_space_remaining(&record_stuffer);
     468                 :        124 :     uint8_t *buf = s2n_stuffer_raw_write(&record_stuffer, len);
     469 [ -  + ][ #  # ]:        124 :     POSIX_ENSURE_REF(buf);
     470                 :            : 
     471                 :        124 :     s2n_blocked_status blocked = S2N_NOT_BLOCKED;
     472                 :        124 :     size_t bytes_read = 0;
     473                 :            : 
     474                 :            :     /* Since recvmsg is responsible for decrypting the record in ktls,
     475                 :            :      * we apply blinding to the recvmsg call.
     476                 :            :      */
     477                 :        124 :     s2n_result result = s2n_ktls_recvmsg(conn->recv_io_context, record_type,
     478                 :        124 :             buf, len, &blocked, &bytes_read);
     479 [ +  + ][ +  - ]:        124 :     WITH_ERROR_BLINDING(conn, POSIX_GUARD_RESULT(result));
     480                 :            : 
     481         [ -  + ]:        119 :     POSIX_GUARD(s2n_stuffer_skip_write(&conn->buffer_in, bytes_read));
     482                 :            : 
     483                 :            :     /* We don't care about returning a full fragment because we don't need to decrypt.
     484                 :            :      * kTLS handled decryption already.
     485                 :            :      * So we can always set conn->in equal to the full buffer_in.
     486                 :            :      */
     487         [ -  + ]:        119 :     POSIX_GUARD_RESULT(s2n_recv_in_init(conn, bytes_read, bytes_read));
     488                 :        119 :     return S2N_SUCCESS;
     489                 :        119 : }

Generated by: LCOV version 1.14