Debian Codegraph @

crt/s2n/tests/unit/s2n_release_non_empty_buffers_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 <stdint.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22
23 static const uint8_t buf_to_send[1023] = { 27 };
24
25 /**
26 * This test ensures that we don't allow releasing connection buffers if they contain part
27 * of the unprocessed record, avoiding connection corruption.
28 */
29 int main(int argc, char **argv)
30 {
31 s2n_blocked_status blocked;
32 uint8_t buf[sizeof(buf_to_send)];
33
34 BEGIN_TEST();
35 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
36
37 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(),
38 s2n_config_ptr_free);
39 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
40 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
41 s2n_cert_chain_and_key_ptr_free);
42 char *cert_chain_pem = NULL;
43 char *private_key_pem = NULL;
44 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
45 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
46 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
47 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
48 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
49 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
50
51 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(),
52 s2n_config_ptr_free);
53 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
54 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
55
56 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
57 s2n_connection_ptr_free);
58 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
59
60 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
61 s2n_connection_ptr_free);
62 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
63
64 /* Use a stuffer pair for the client-server I/O, but set up the server
65 * with a separate intermediate stuffer so we can control how much data
66 * it sees at a time (to simulate partial record delivery).
67 */
68 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
69 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
70
71 /* Client uses the stuffer pair normally */
72 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_pair.client_in, &io_pair.server_in, client_conn));
73
74 /* Server uses a separate "in" stuffer so we can manually control how
75 * much data is available for reading. Server still writes directly to
76 * client_in so the client can read responses.
77 */
78 DEFER_CLEANUP(struct s2n_stuffer server_in = { 0 }, s2n_stuffer_free);
79 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&server_in, 0));
80 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&server_in, &io_pair.client_in, server_conn));
81
82 /* Negotiate the handshake by shuttling data between io_pair.server_in
83 * and server_in manually, similar to how the original test shuttled
84 * data between a pipe and a stuffer.
85 */
86 {
87 bool server_done = false, client_done = false;
88
89 do {
90 if (!client_done) {
91 int ret = s2n_negotiate(client_conn, &blocked);
92 EXPECT_TRUE(ret == 0 || s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED);
93 if (ret == 0) {
94 client_done = true;
95 }
96 }
97
98 /* Move data from io_pair.server_in to the server's actual input stuffer */
99 uint32_t available = s2n_stuffer_data_available(&io_pair.server_in);
100 if (available > 0) {
101 EXPECT_SUCCESS(s2n_stuffer_copy(&io_pair.server_in, &server_in, available));
102 }
103
104 if (!server_done) {
105 int ret = s2n_negotiate(server_conn, &blocked);
106 EXPECT_TRUE(ret == 0 || s2n_error_get_type(s2n_errno) == S2N_ERR_T_BLOCKED);
107 if (ret == 0) {
108 server_done = true;
109 }
110 }
111 } while (!client_done || !server_done);
112 }
113
114 /* Client sends data. The encrypted record lands in io_pair.server_in. */
115 EXPECT_EQUAL(s2n_send(client_conn, buf_to_send, sizeof(buf_to_send), &blocked), sizeof(buf_to_send));
116
117 /* Copy only 100 bytes of the record into the server's input stuffer,
118 * simulating partial record delivery.
119 */
120 uint32_t total_available = s2n_stuffer_data_available(&io_pair.server_in);
121 EXPECT_TRUE(total_available > 100);
122 EXPECT_SUCCESS(s2n_stuffer_copy(&io_pair.server_in, &server_in, 100));
123
124 /* s2n_recv should fail as we received only part of the record */
125 EXPECT_FAILURE(s2n_recv(server_conn, buf, sizeof(buf), &blocked));
126 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_READ);
127
128 /* Now try to release the buffers and expect failure as buffers are not empty */
129 EXPECT_FAILURE(s2n_connection_release_buffers(server_conn));
130
131 /* Copy the rest of the record into the server's input stuffer */
132 uint32_t remaining = s2n_stuffer_data_available(&io_pair.server_in);
133 EXPECT_SUCCESS(s2n_stuffer_copy(&io_pair.server_in, &server_in, remaining));
134
135 /* s2n_recv should now succeed with the full record available */
136 ssize_t ret = s2n_recv(server_conn, buf, sizeof(buf), &blocked);
137 EXPECT_EQUAL(ret, sizeof(buf_to_send));
138 EXPECT_BYTEARRAY_EQUAL(buf, buf_to_send, ret);
139
140 /* Since full record was processed, we should be able to release buffers */
141 EXPECT_SUCCESS(s2n_connection_release_buffers(server_conn));
142
143 /* Reconnect the server to read directly from the stuffer pair for shutdown,
144 * since the intermediate stuffer was only needed for partial-record testing.
145 */
146 EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&io_pair.server_in, &io_pair.client_in, server_conn));
147
148 /* Shutdown */
149 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
150
151 /* Clean up */
152 free(cert_chain_pem);
153 free(private_key_pem);
154
155 END_TEST();
156
157 return 0;
158 }