1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include <fcntl.h>
#include <stdint.h>
#include <sys/wait.h>
#include <unistd.h>
#include "api/s2n.h"
#include "s2n_test.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_handshake.h"
#include "tls/s2n_tls13.h"
#include "utils/s2n_random.h"
#include "utils/s2n_safety.h"
static const float minimum_send_percent = 5.0;
#define MIN_PERCENT_COMPLETE(remaining, total) ((((total - remaining) / (total * 1.0)) * 100.0) > minimum_send_percent)
int mock_client(struct s2n_test_io_pair *io_pair, uint8_t *expected_data, uint32_t size)
{
uint8_t *buffer = malloc(size);
uint8_t *ptr = buffer;
struct s2n_connection *client_conn;
struct s2n_config *client_config;
s2n_blocked_status blocked;
int result = 0;
/* If something goes wrong, and the server never finishes sending,
* we'll want to have the child process die eventually, or certain
* CI/CD pipelines might never complete */
int should_block = 1;
/* Give the server a chance to listen */
sleep(1);
client_conn = s2n_connection_new(S2N_CLIENT);
client_config = s2n_config_new();
s2n_config_disable_x509_verification(client_config);
s2n_connection_set_config(client_conn, client_config);
POSIX_GUARD(s2n_config_set_cipher_preferences(client_config, "test_all"));
s2n_connection_set_io_pair(client_conn, io_pair);
result = s2n_negotiate(client_conn, &blocked);
if (result < 0) {
return 1;
}
/* Receive 10MB of data */
uint32_t remaining = size;
while (remaining) {
int r = s2n_recv(client_conn, ptr, remaining, &blocked);
if (r < 0) {
return 1;
}
remaining -= r;
ptr += r;
if (should_block && MIN_PERCENT_COMPLETE(remaining, size)) {
raise(SIGSTOP);
should_block = 0;
}
}
int shutdown_rc = -1;
do {
shutdown_rc = s2n_shutdown(client_conn, &blocked);
} while (shutdown_rc != 0);
for (size_t i = 0; i < size; i++) {
if (buffer[i] != expected_data[i]) {
return 1;
}
}
free(buffer);
s2n_connection_free(client_conn);
s2n_config_free(client_config);
s2n_cleanup();
return 0;
}
int mock_client_iov(struct s2n_test_io_pair *io_pair, struct iovec *iov, uint32_t iov_size)
{
struct s2n_connection *client_conn;
struct s2n_config *client_config;
s2n_blocked_status blocked;
int result = 0;
int total_size = 0, i;
int should_block = 1;
for (i = 0; i < iov_size; i++) {
total_size += iov[i].iov_len;
}
uint8_t *buffer = malloc(total_size + iov[0].iov_len);
int buffer_offs = 0;
/* Give the server a chance to listen */
sleep(1);
client_conn = s2n_connection_new(S2N_CLIENT);
client_config = s2n_config_new();
s2n_config_disable_x509_verification(client_config);
s2n_connection_set_config(client_conn, client_config);
POSIX_GUARD(s2n_config_set_cipher_preferences(client_config, "test_all"));
s2n_connection_set_io_pair(client_conn, io_pair);
result = s2n_negotiate(client_conn, &blocked);
if (result < 0) {
return 1;
}
uint32_t remaining = total_size;
while (remaining) {
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked);
if (r < 0) {
return 1;
}
remaining -= r;
buffer_offs += r;
if (should_block && MIN_PERCENT_COMPLETE(remaining, total_size)) {
raise(SIGSTOP);
should_block = 0;
}
}
remaining = iov[0].iov_len;
while (remaining) {
int r = s2n_recv(client_conn, &buffer[buffer_offs], remaining, &blocked);
if (r < 0) {
return 1;
}
remaining -= r;
buffer_offs += r;
}
int shutdown_rc = -1;
do {
shutdown_rc = s2n_shutdown(client_conn, &blocked);
} while (shutdown_rc != 0);
for (i = 0, buffer_offs = 0; i < iov_size; i++) {
if (memcmp(iov[i].iov_base, &buffer[buffer_offs], iov[i].iov_len)) {
return 1;
}
buffer_offs += iov[i].iov_len;
}
if (memcmp(iov[0].iov_base, &buffer[buffer_offs], iov[0].iov_len)) {
return 1;
}
free(buffer);
s2n_connection_free(client_conn);
s2n_config_free(client_config);
return 0;
}
S2N_RESULT cleanup_io_data(struct iovec **iov, int iov_size, struct s2n_blob *blob)
{
if (*iov) {
for (int i = 0; i < iov_size; i++) {
free((*iov)[i].iov_base);
}
free(*iov);
} else {
s2n_free(blob);
}
return S2N_RESULT_OK;
}
int test_send(int use_tls13, int use_iov, int prefer_throughput)
{
s2n_blocked_status blocked;
int status;
pid_t pid;
char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE];
char private_key_pem[S2N_MAX_TEST_PEM_SIZE];
char dhparams_pem[S2N_MAX_TEST_PEM_SIZE];
/* Get some random data to send/receive */
uint32_t data_size = 0;
struct s2n_blob blob = { 0 };
/* These numbers are chosen so that some of the payload is bigger
* than max TLS1.3 record size (2**14 + 1), which is needed to validate
* that we handle record sizing correctly.
* (see https://github.com/awslabs/s2n/pull/1780).
*
* Note that for each iov in the list, the payload size is doubled
* to ensure the implementation handles various lengths.
*
* With the current values, it will include
* * 8192 bytes
* * 16384 bytes
* * 32768 bytes
* * 65536 bytes
* * 131072 bytes
* * 262144 bytes
* * 524288 bytes */
int iov_payload_size = 8192, iov_size = 7;
struct iovec *iov = NULL;
if (!use_iov) {
data_size = 10000000;
EXPECT_SUCCESS(s2n_alloc(&blob, data_size));
EXPECT_OK(s2n_get_public_random_data(&blob));
} else {
iov = malloc(sizeof(*iov) * iov_size);
data_size = 0;
for (int i = 0; i < iov_size; i++, iov_payload_size *= 2) {
struct s2n_blob blob_local = { 0 };
iov[i].iov_base = blob_local.data = malloc(iov_payload_size);
iov[i].iov_len = blob_local.size = iov_payload_size;
EXPECT_OK(s2n_get_public_random_data(&blob));
data_size += iov_payload_size;
}
}
/* Create a pipe */
struct s2n_test_io_pair io_pair;
EXPECT_SUCCESS(s2n_io_pair_init(&io_pair));
/* Create a child process */
pid = fork();
if (pid == 0) {
/* This is the client process, close the server end of the pipe */
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
/* Run the client */
const int client_rc = !use_iov ? mock_client(&io_pair, blob.data, data_size) : mock_client_iov(&io_pair, iov, iov_size);
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
EXPECT_OK(cleanup_io_data(&iov, iov_size, &blob));
exit(client_rc);
}
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(), s2n_cert_chain_and_key_ptr_free);
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem));
if (use_tls13) {
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "test_all"));
} else {
POSIX_GUARD(s2n_config_set_cipher_preferences(config, "test_all_tls12"));
}
/* This is the server process, close the client end of the pipe */
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
if (prefer_throughput) {
EXPECT_SUCCESS(s2n_connection_prefer_throughput(conn));
} else {
EXPECT_SUCCESS(s2n_connection_prefer_low_latency(conn));
}
/* Set up the connection to read from the fd */
EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
EXPECT_SUCCESS(s2n_connection_use_corked_io(conn));
/* Negotiate the handshake. */
EXPECT_SUCCESS(s2n_negotiate(conn, &blocked));
/* Make sure we negotiated the expected version */
if (use_tls13) {
EXPECT_EQUAL(conn->actual_protocol_version, s2n_get_highest_fully_supported_tls_version());
} else {
EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
}
/* Make our pipes non-blocking */
s2n_fd_set_non_blocking(io_pair.server);
/* Try to all 10MB of data, should be enough to fill PIPEBUF, so
we'll get blocked at some point */
uint32_t remaining = data_size;
uint8_t *ptr = blob.data;
uint32_t iov_offs = 0;
while (remaining) {
int r = !use_iov ? s2n_send(conn, ptr, remaining, &blocked) :
s2n_sendv_with_offset(conn, iov, iov_size, iov_offs, &blocked);
/* We will send up to minimum_send_percent, after which the client will automatically block itself.
* This allows us to cover the case where s2n_send gets EAGAIN on the very first call
* which can happen on certain platforms. By making sure we've successfully sent something
* we can ensure write -> block -> client drain -> write ordering.*/
if (r < 0 && !MIN_PERCENT_COMPLETE(remaining, data_size)) {
continue;
}
if (r < 0 && blocked == S2N_BLOCKED_ON_WRITE) {
/* We reached a blocked state and made no forward progress last call */
break;
}
EXPECT_TRUE(r > 0);
remaining -= r;
if (!use_iov) {
ptr += r;
} else {
iov_offs += r;
}
}
/* Remaining should be between data_size and 0 */
EXPECT_TRUE(remaining < data_size);
EXPECT_TRUE(remaining > 0);
/* Wait for the child process to read some bytes and block itself*/
sleep(1);
/* Wake the child process by sending it SIGCONT */
EXPECT_SUCCESS(kill(pid, SIGCONT));
/* Make our sockets blocking again */
s2n_fd_set_blocking(io_pair.server);
/* Actually send the remaining data */
while (remaining) {
int r = !use_iov ? s2n_send(conn, ptr, remaining, &blocked) :
s2n_sendv_with_offset(conn, iov, iov_size, iov_offs, &blocked);
EXPECT_TRUE(r > 0);
remaining -= r;
if (!use_iov) {
ptr += r;
} else {
iov_offs += r;
}
}
if (use_iov) {
int r = s2n_sendv(conn, iov, 1, &blocked);
EXPECT_TRUE(r > 0);
}
EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
EXPECT_EQUAL(waitpid(-1, &status, 0), pid);
EXPECT_EQUAL(status, 0);
/* Clean up */
EXPECT_OK(cleanup_io_data(&iov, iov_size, &blob));
EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
return 0;
}
int main(int argc, char **argv)
{
/* Ignore SIGPIPE */
signal(SIGPIPE, SIG_IGN);
BEGIN_TEST();
for (int use_tls13 = 0; use_tls13 < 2; use_tls13++) {
for (int use_iovec = 0; use_iovec < 2; use_iovec++) {
for (int use_throughput = 0; use_throughput < 2; use_throughput++) {
test_send(use_tls13, use_iovec, use_throughput);
}
}
}
END_TEST();
return 0;
}
|