tls-server: Avoid allocating large buffer for cipher suites on stack

The `cipher_suites` field has a 16-bit length field, so up to 32k 2-byte
cipher suites could technically be proposed.  With `tls_cipher_suite_t`
typically being 4 bytes wide, the necessary allocation for the temporary
array can be up to 128 KiB.  Even though this should be fine on typical
systems, we avoid potentially overflowing the stack by using malloc()
instead of alloca().
This commit is contained in:
Tobias Brunner
2026-04-02 08:34:23 +02:00
parent a3c5fad1ff
commit b56b3d48b6
+4 -2
View File
@@ -344,7 +344,6 @@ static status_t process_client_hello(private_tls_server_t *this,
key_share_t peer = {0};
chunk_t extension_data = chunk_empty;
bio_reader_t *extensions, *extension;
tls_cipher_suite_t *suites;
tls_version_t original_version_max;
int count, i;
rng_t *rng;
@@ -534,9 +533,10 @@ static status_t process_client_hello(private_tls_server_t *this,
else
{
tls_cipher_suite_t original_suite = this->suite;
tls_cipher_suite_t *suites;
count = ciphers.len / sizeof(uint16_t);
suites = alloca(count * sizeof(tls_cipher_suite_t));
suites = malloc(count * sizeof(tls_cipher_suite_t));
DBG2(DBG_TLS, "received %d TLS cipher suites:", count);
for (i = 0; i < count; i++)
{
@@ -546,8 +546,10 @@ static status_t process_client_hello(private_tls_server_t *this,
if (!select_suite_and_key(this, suites, count))
{
this->alert->add(this->alert, TLS_FATAL, TLS_HANDSHAKE_FAILURE);
free(suites);
return NEED_MORE;
}
free(suites);
if (retrying(this) && original_suite != this->suite)
{
DBG1(DBG_TLS, "selected %N instead of %N during retry",