Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_nonblocking_test.c

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