1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2026 Gleb Smirnoff <glebius@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/ioctl.h> 29 #include <sys/socket.h> 30 #include <sys/sysctl.h> 31 #include <sys/mman.h> 32 #include <netinet/in.h> 33 #include <fcntl.h> 34 #include <stdlib.h> 35 #include <poll.h> 36 #include <pthread.h> 37 38 #include <openssl/ssl.h> 39 #include <openssl/err.h> 40 #include <openssl/bio.h> 41 #include <openssl/x509v3.h> 42 43 #include <atf-c.h> 44 45 #define FSIZE (size_t)(2 * 1024 * 1024) 46 47 struct ctx { 48 EVP_PKEY *pkey; /* Self-signed key ... */ 49 X509 *cert; /* ... and certificate */ 50 SSL_CTX *ctx; /* client context */ 51 SSL *cln; /* client connection */ 52 SSL *srv; /* server connection */ 53 int cs; /* client socket */ 54 int ss; /* server socket */ 55 int fd; /* test file descriptor */ 56 void *mfd; /* mapped contents of the test file */ 57 uint16_t port; /* server listening port */ 58 pthread_t thr; /* server thread */ 59 off_t offset; /* SSL_sendfile offset */ 60 size_t size; /* SSL_sendfile size */ 61 bool nb; /* SSL_sendfile mode */ 62 ossl_ssize_t sbytes; /* SSL_sendfile returned sbytes */ 63 enum { 64 INIT, 65 READY, 66 RUNNING, 67 EXITING, 68 } state; 69 pthread_mutex_t mtx; 70 pthread_cond_t cv; 71 }; 72 73 static void *server_thread(void *arg); 74 75 static void 76 common_init(struct ctx *c) 77 { 78 char hostname[sizeof("localhost:65536")]; 79 char tempname[] = "/tmp/ssl_sendfile_test.XXXXXXXXXX"; 80 X509_NAME *name; 81 X509_EXTENSION *ext; 82 SSL *ssl; 83 bool enable; 84 size_t len = sizeof(enable); 85 86 if (sysctlbyname("kern.ipc.tls.enable", &enable, &len, NULL, 0) == -1) { 87 if (errno == ENOENT) 88 atf_tc_skip("kernel does not have options KERN_TLS"); 89 atf_libc_error(errno, "Failed to read kern.ipc.tls.enable"); 90 } 91 if (!enable) 92 atf_tc_skip("kern.ipc.tls.enable is off"); 93 94 c->state = INIT; 95 96 /* 97 * Generate self signed key & certificate. 98 */ 99 SSL_library_init(); 100 OpenSSL_add_all_algorithms(); 101 SSL_load_error_strings(); 102 c->pkey = EVP_RSA_gen(2048); 103 ATF_REQUIRE(c->pkey != NULL); 104 c->cert = X509_new(); 105 ATF_REQUIRE(c->cert != NULL); 106 ASN1_INTEGER_set(X509_get_serialNumber(c->cert), 1); 107 X509_set_version(c->cert, 2); 108 X509_gmtime_adj(X509_get_notBefore(c->cert), 0); 109 X509_gmtime_adj(X509_get_notAfter(c->cert), 60L*60*24*365); 110 X509_set_pubkey(c->cert, c->pkey); 111 name = X509_get_subject_name(c->cert); 112 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, 113 (unsigned char *)"localhost", -1, -1, 0); 114 X509_set_issuer_name(c->cert, name); 115 ext = X509V3_EXT_conf_nid(NULL, NULL, NID_basic_constraints, 116 "critical,CA:FALSE"); 117 X509_add_ext(c->cert, ext, -1); 118 X509_EXTENSION_free(ext); 119 ATF_REQUIRE(X509_sign(c->cert, c->pkey, EVP_sha256()) > 0); 120 121 /* 122 * Create random filled file with memory mapping. 123 */ 124 ATF_REQUIRE((c->fd = mkstemp(tempname)) > 0); 125 ATF_REQUIRE(unlink(tempname) == 0); 126 ATF_REQUIRE(ftruncate(c->fd, FSIZE) == 0); 127 ATF_REQUIRE((c->mfd = mmap(NULL, FSIZE, PROT_READ | PROT_WRITE, 128 MAP_SHARED, c->fd, 0)) != MAP_FAILED); 129 arc4random_buf(c->mfd, FSIZE); 130 131 ATF_REQUIRE(pthread_mutex_init(&c->mtx, NULL) == 0); 132 ATF_REQUIRE(pthread_cond_init(&c->cv, NULL) == 0); 133 134 /* 135 * Start server and wait for it to finish bind(2) + listen(2). 136 */ 137 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 138 ATF_REQUIRE(pthread_create(&c->thr, NULL, server_thread, c) == 0); 139 if (c->state != READY) 140 ATF_REQUIRE(pthread_cond_wait(&c->cv, &c->mtx) == 0); 141 ATF_REQUIRE(c->state == READY); 142 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 143 144 /* 145 * Connect client. 146 */ 147 c->ctx = SSL_CTX_new(TLS_client_method()); 148 ATF_REQUIRE(c->ctx != NULL); 149 ATF_REQUIRE(X509_STORE_add_cert(SSL_CTX_get_cert_store(c->ctx), 150 c->cert)); 151 ssl = c->cln = SSL_new(c->ctx); 152 ATF_REQUIRE(c->cln != NULL); 153 ATF_REQUIRE((c->cs = socket(AF_INET, SOCK_STREAM, 0)) > 0); 154 ATF_REQUIRE(connect(c->cs, (struct sockaddr *)&(struct sockaddr_in) 155 { .sin_family = AF_INET, .sin_len = sizeof(struct sockaddr_in), 156 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), .sin_port = c->port }, 157 sizeof(struct sockaddr_in)) == 0); 158 ATF_REQUIRE(SSL_set_fd(ssl, c->cs) == 1); 159 ATF_REQUIRE(snprintf(hostname, sizeof(hostname), "localhost:%u", 160 ntohs(c->port)) >= (int)sizeof("localhost:0")); 161 ATF_REQUIRE(SSL_set_tlsext_host_name(ssl, hostname) == 1); 162 SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL); 163 ATF_REQUIRE(SSL_connect(ssl) == 1); 164 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 165 ATF_REQUIRE(fcntl(c->cs, F_SETFL, O_NONBLOCK) != -1); 166 } 167 168 static void 169 common_cleanup(struct ctx *c) 170 { 171 172 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 173 c->state = EXITING; 174 ATF_REQUIRE(pthread_cond_signal(&c->cv) == 0); 175 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 176 ATF_REQUIRE(pthread_join(c->thr, NULL) == 0); 177 178 ATF_REQUIRE(pthread_mutex_destroy(&c->mtx) == 0); 179 ATF_REQUIRE(pthread_cond_destroy(&c->cv) == 0); 180 181 SSL_free(c->cln); 182 SSL_CTX_free(c->ctx); 183 X509_free(c->cert); 184 EVP_PKEY_free(c->pkey); 185 } 186 187 static void * 188 server_thread(void *arg) { 189 struct ctx *c = arg; 190 SSL_CTX *srv; 191 SSL *ssl; 192 struct sockaddr_in sin = { 193 .sin_family = AF_INET, 194 .sin_len = sizeof(sin), 195 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 196 }; 197 int s; 198 199 srv = SSL_CTX_new(TLS_server_method()); 200 ATF_REQUIRE(srv != NULL); 201 ATF_REQUIRE(SSL_CTX_set_options(srv, SSL_OP_ENABLE_KTLS) & 202 SSL_OP_ENABLE_KTLS); 203 SSL_CTX_use_PrivateKey(srv, c->pkey); 204 SSL_CTX_use_certificate(srv, c->cert); 205 ATF_REQUIRE((s = socket(AF_INET, SOCK_STREAM, 0)) > 0); 206 ATF_REQUIRE(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(socklen_t){1}, 207 sizeof(int)) == 0); 208 ATF_REQUIRE(bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0); 209 ATF_REQUIRE(getsockname(s, (struct sockaddr *)&sin, 210 &(socklen_t){ sizeof(sin) }) == 0); 211 ATF_REQUIRE(listen(s, -1) == 0); 212 213 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 214 c->port = sin.sin_port; 215 c->state = READY; 216 ATF_REQUIRE(pthread_cond_signal(&c->cv) == 0); 217 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 218 219 ATF_REQUIRE((c->ss = accept(s, NULL, NULL)) > 0); 220 ssl = c->srv = SSL_new(srv); 221 SSL_set_fd(ssl, c->ss); 222 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); 223 ATF_REQUIRE(SSL_accept(ssl) > 0); 224 225 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 226 while (c->state != EXITING) { 227 if (c->state == RUNNING) { 228 ATF_REQUIRE(fcntl(c->ss, F_SETFL, 229 c->nb ? O_NONBLOCK : 0) != -1); 230 c->sbytes = SSL_sendfile(ssl, c->fd, c->offset, 231 c->size, 0); 232 c->state = READY; 233 } 234 ATF_REQUIRE(c->state == READY); 235 ATF_REQUIRE(pthread_cond_signal(&c->cv) == 0); 236 ATF_REQUIRE(pthread_cond_wait(&c->cv, &c->mtx) == 0); 237 } 238 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 239 240 SSL_shutdown(ssl); 241 SSL_free(ssl); 242 close(c->ss); 243 SSL_CTX_free(srv); 244 close(s); 245 246 return (NULL); 247 } 248 249 static void 250 sendme_locked(struct ctx *c, off_t offset, size_t size, bool nb) 251 { 252 ATF_REQUIRE(c->state == READY); 253 c->state = RUNNING; 254 c->offset = offset; 255 c->size = size; 256 c->nb = nb; 257 ATF_REQUIRE(pthread_cond_signal(&c->cv) == 0); 258 } 259 260 static void 261 sendme_locked_wait(struct ctx *c, off_t offset, size_t size, bool nb) 262 { 263 sendme_locked(c, offset, size, nb); 264 while (c->state != READY) 265 ATF_REQUIRE(pthread_cond_wait(&c->cv, &c->mtx) == 0); 266 } 267 268 static void 269 sendme(struct ctx *c, off_t offset, size_t size, bool nb) 270 { 271 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 272 sendme_locked(c, offset, size, nb); 273 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 274 } 275 276 /* 277 * Block until non-blocking socket has at least a byte. 278 */ 279 static int 280 SSL_read_b(SSL *ssl, void *buf, int size) 281 { 282 int rv, fd; 283 284 ATF_REQUIRE((fd = SSL_get_fd(ssl)) > 0); 285 while ((rv = SSL_read(ssl, buf, size)) <= 0 && 286 SSL_get_error(ssl, rv) == SSL_ERROR_WANT_READ) 287 ATF_REQUIRE(poll(&(struct pollfd){ .fd = fd, .events = POLLIN }, 288 1, INFTIM) == 1); 289 290 return (rv); 291 } 292 293 static void 294 require_sbytes(struct ctx *c, ssize_t expect) 295 { 296 ATF_REQUIRE(pthread_mutex_lock(&c->mtx) == 0); 297 ATF_REQUIRE(c->sbytes == expect); 298 ATF_REQUIRE(pthread_mutex_unlock(&c->mtx) == 0); 299 } 300 301 ATF_TC_WITHOUT_HEAD(basic); 302 ATF_TC_BODY(basic, tc) 303 { 304 struct ctx c; 305 char buf[64]; 306 size_t nread; 307 int n; 308 309 common_init(&c); 310 311 sendme(&c, 0, 0, false); 312 nread = 0; 313 while (nread < FSIZE && (n = SSL_read_b(c.cln, buf, sizeof(buf))) > 0) { 314 ATF_REQUIRE(memcmp((char *)c.mfd + nread, buf, n) == 0); 315 nread += n; 316 } 317 ATF_REQUIRE(nread == FSIZE); 318 require_sbytes(&c, FSIZE); 319 320 common_cleanup(&c); 321 } 322 323 ATF_TC_WITHOUT_HEAD(random); 324 ATF_TC_BODY(random, tc) 325 { 326 struct ctx c; 327 #define RSIZE (256*1024) 328 329 common_init(&c); 330 331 for (u_int i = 0; i < 10; i++) { 332 char buf[RSIZE]; 333 off_t offset; 334 size_t size, n, nread, expect; 335 336 offset = arc4random() % FSIZE; 337 size = arc4random() % RSIZE; 338 sendme(&c, offset, size, false); 339 expect = offset + size < FSIZE ? size : FSIZE - offset; 340 nread = 0; 341 while (nread < expect && 342 (n = SSL_read_b(c.cln, buf, sizeof(buf))) > 0) { 343 ATF_REQUIRE(memcmp((char *)c.mfd + offset + nread, buf, 344 n) == 0); 345 nread += n; 346 } 347 ATF_REQUIRE(nread == expect); 348 require_sbytes(&c, (ssize_t)expect); 349 } 350 351 common_cleanup(&c); 352 } 353 354 /* Truncate the file while sendfile(2) is working on it. */ 355 ATF_TC_WITHOUT_HEAD(truncate); 356 ATF_TC_BODY(truncate, tc) 357 { 358 struct ctx c; 359 char buf[128 * 1024]; 360 size_t nread; 361 int n; 362 #define TRUNC (FSIZE - 1024) 363 364 common_init(&c); 365 366 ATF_REQUIRE(setsockopt(c.ss, SOL_SOCKET, SO_SNDBUF, &(int){FSIZE / 16}, 367 sizeof(int)) == 0); 368 ATF_REQUIRE(setsockopt(c.cs, SOL_SOCKET, SO_RCVBUF, &(int){FSIZE / 16}, 369 sizeof(int)) == 0); 370 371 sendme(&c, 0, 0, false); 372 /* Make sure sender is waiting on the socket buffer. */ 373 while (poll(&(struct pollfd){ .fd = c.ss, .events = POLLOUT }, 1, 1) 374 != 0) 375 ; 376 ATF_REQUIRE(ftruncate(c.fd, TRUNC) == 0); 377 nread = 0; 378 while (nread < TRUNC && (n = SSL_read_b(c.cln, buf, sizeof(buf))) > 0) { 379 ATF_REQUIRE(memcmp((char *)c.mfd + nread, buf, n) == 0); 380 nread += n; 381 } 382 ATF_REQUIRE(nread == TRUNC); 383 require_sbytes(&c, TRUNC); 384 385 common_cleanup(&c); 386 } 387 388 /* Grow the file while sendfile(2) is working on it. */ 389 ATF_TC_WITHOUT_HEAD(grow); 390 ATF_TC_BODY(grow, tc) 391 { 392 struct ctx c; 393 char buf[128 * 1024]; 394 size_t nread; 395 void *map; 396 int n; 397 #define GROW (FSIZE/2) 398 399 common_init(&c); 400 401 ATF_REQUIRE(setsockopt(c.ss, SOL_SOCKET, SO_SNDBUF, &(int){FSIZE / 16}, 402 sizeof(int)) == 0); 403 ATF_REQUIRE(setsockopt(c.cs, SOL_SOCKET, SO_RCVBUF, &(int){FSIZE / 16}, 404 sizeof(int)) == 0); 405 406 sendme(&c, 0, 0, false); 407 /* Make sure sender is waiting on the socket buffer. */ 408 while (poll(&(struct pollfd){ .fd = c.ss, .events = POLLOUT }, 1, 1) 409 != 0) 410 ; 411 /* Grow the file and create second map. */ 412 ATF_REQUIRE(ftruncate(c.fd, FSIZE + GROW) == 0); 413 ATF_REQUIRE((map = mmap(NULL, GROW, PROT_READ | PROT_WRITE, 414 MAP_SHARED, c.fd, FSIZE)) != MAP_FAILED); 415 arc4random_buf(map, GROW); 416 417 /* Read out original part. */ 418 nread = 0; 419 while (nread < FSIZE && (n = SSL_read_b(c.cln, buf, 420 FSIZE - nread > sizeof(buf) ? sizeof(buf) : FSIZE - nread)) > 0) { 421 ATF_REQUIRE(memcmp((char *)c.mfd + nread, buf, n) == 0); 422 nread += n; 423 } 424 ATF_REQUIRE(nread == FSIZE); 425 /* Read out grown part. */ 426 nread = 0; 427 while (nread < GROW && (n = SSL_read_b(c.cln, buf, sizeof(buf))) > 0) { 428 ATF_REQUIRE(memcmp((char *)map + nread, buf, n) == 0); 429 nread += n; 430 } 431 ATF_REQUIRE(nread == GROW); 432 require_sbytes(&c, FSIZE + GROW); 433 434 common_cleanup(&c); 435 } 436 437 ATF_TC_WITHOUT_HEAD(offset_beyond_eof); 438 ATF_TC_BODY(offset_beyond_eof, tc) 439 { 440 struct ctx c; 441 442 common_init(&c); 443 444 c.sbytes = -1; 445 sendme(&c, FSIZE + 1, 0, false); 446 ATF_REQUIRE(pthread_mutex_lock(&c.mtx) == 0); 447 while (c.state != READY) 448 ATF_REQUIRE(pthread_cond_wait(&c.cv, &c.mtx) == 0); 449 ATF_REQUIRE(c.sbytes == 0); 450 ATF_REQUIRE(pthread_mutex_unlock(&c.mtx) == 0); 451 452 common_cleanup(&c); 453 } 454 455 /* 456 * Prove that we can differentiate a short write due to EAGAIN from one due to 457 * end of file. 458 */ 459 ATF_TC_WITHOUT_HEAD(eagain_vs_eof); 460 ATF_TC_BODY(eagain_vs_eof, tc) 461 { 462 struct ctx c; 463 char buf[16 * 1024]; 464 ssize_t nread; 465 int n; 466 467 common_init(&c); 468 469 /* 470 * Exercise short write due to no buffer space on non-blocking 471 * socket. Internall sendfile(2) returns -1 and errno == EAGAIN. 472 */ 473 ATF_REQUIRE(pthread_mutex_lock(&c.mtx) == 0); 474 sendme_locked_wait(&c, 0, FSIZE, true); 475 ATF_REQUIRE(c.sbytes > 0); 476 ATF_REQUIRE(SSL_get_error(c.srv, c.sbytes) == 0); 477 #if 0 /* see https://github.com/openssl/openssl/issues/29742 */ 478 ATF_REQUIRE(BIO_should_retry(SSL_get_wbio(c.srv))); 479 #endif 480 481 /* 482 * Exercise second attempt on already full buffer. 483 */ 484 sendme_locked_wait(&c, 0, FSIZE, true); 485 ATF_REQUIRE(c.sbytes == -1); 486 ATF_REQUIRE(SSL_get_error(c.srv, c.sbytes) == SSL_ERROR_WANT_WRITE); 487 ATF_REQUIRE(BIO_should_retry(SSL_get_wbio(c.srv))); 488 489 /* Clear the buffer. */ 490 nread = 0; 491 while (nread < c.sbytes && 492 (n = SSL_read_b(c.cln, buf, sizeof(buf))) > 0) { 493 ATF_REQUIRE(memcmp((char *)c.mfd + nread, buf, n) == 0); 494 nread += n; 495 } 496 497 /* 498 * Exercise zero length write: offset == file size. 499 * 500 * SSL_ERROR_SYSCALL seems a strange error code, as the syscall did not 501 * fail, and errno is clear, because a request to send 0 bytes is 502 * legitimate one. This test just documents the existing behavior 503 * rather than asserts that this is a correct behavior. 504 */ 505 sendme_locked_wait(&c, FSIZE, 0, true); 506 ATF_REQUIRE(c.sbytes == 0); 507 ATF_REQUIRE(SSL_get_error(c.srv, c.sbytes) == SSL_ERROR_SYSCALL); 508 #if 0 /* see https://github.com/openssl/openssl/issues/29742 */ 509 ATF_REQUIRE(!BIO_should_retry(SSL_get_wbio(c.srv))); 510 #endif 511 512 /* 513 * Exercise short write due to end of file. 514 */ 515 sendme_locked_wait(&c, FSIZE - 100, 0, true); 516 ATF_REQUIRE(c.sbytes == 100); 517 ATF_REQUIRE(SSL_get_error(c.srv, c.sbytes) == 0); 518 #if 0 /* see https://github.com/openssl/openssl/issues/29742 */ 519 ATF_REQUIRE(!BIO_should_retry(SSL_get_wbio(c.srv))); 520 #endif 521 522 ATF_REQUIRE(pthread_mutex_unlock(&c.mtx) == 0); 523 524 common_cleanup(&c); 525 } 526 527 ATF_TP_ADD_TCS(tp) 528 { 529 ATF_TP_ADD_TC(tp, basic); 530 ATF_TP_ADD_TC(tp, random); 531 ATF_TP_ADD_TC(tp, truncate); 532 ATF_TP_ADD_TC(tp, grow); 533 ATF_TP_ADD_TC(tp, offset_beyond_eof); 534 ATF_TP_ADD_TC(tp, eagain_vs_eof); 535 536 return atf_no_error(); 537 } 538