Fix out-of-bounds write in mbedtls_ssl_cookie_write

mbedtls_ssl_cookie_write() only reserves COOKIE_HMAC_LEN bytes for the
MAC after the timestamp, but passed COOKIE_MD_OUTLEN as the output size
to psa_mac_sign_finish(). PSA fills the unused part of the declared
output buffer, so up to 4 bytes (20 with SHA-384) past the end of the
caller's buffer were overwritten when it had less than that to spare.

Pass the size that was actually reserved and add a regression test.

Signed-off-by: Naveed <naveed@bugqore.com>
This commit is contained in:
Naveed 2026-10-16 02:38:31 +05:30
parent 38c5d91611
commit 258bbbbdb0
4 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,6 @@
Bugfix
* Fix mbedtls_ssl_cookie_write() writing up to 20 bytes past the end of
the output buffer when less than 36 bytes (or 52 bytes when the cookie
uses SHA-384) were available. The library's own callers always leave
enough room, so only applications that call it directly with a smaller
buffer were affected.

View File

@ -159,7 +159,7 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
goto exit;
}
status = psa_mac_sign_finish(&operation, *p, COOKIE_MD_OUTLEN,
status = psa_mac_sign_finish(&operation, *p, COOKIE_HMAC_LEN,
&sign_mac_length);
if (status != PSA_SUCCESS) {
ret = PSA_TO_MBEDTLS_ERR(status);

View File

@ -3400,6 +3400,12 @@ cookie_parsing:"16fefd0000000000000000002f010000de000000000000011efefd7b72727272
Cookie parsing: one byte overread
cookie_parsing:"16fefd0000000000000000002F010000de000000000000011efefd7b7272727272727272727272727272727272727272727272727272727272727d0001":MBEDTLS_ERR_SSL_DECODE_ERROR
Cookie write: output buffer of exactly the cookie length
cookie_write_bounds:32:0
Cookie write: output buffer one byte too short
cookie_write_bounds:31:MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL
TLS 1.3 srv Certificate msg - wrong vector lengths
tls13_server_certificate_msg_invalid_vector_len

View File

@ -2,6 +2,7 @@
#include <ssl_misc.h>
#include <mbedtls/timing.h>
#include <mbedtls/debug.h>
#include <mbedtls/ssl_cookie.h>
#include <mbedtls/pk.h>
#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
#include <mbedtls/private/pk_private.h>
@ -4296,6 +4297,46 @@ exit:
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_SSL_COOKIE_C */
void cookie_write_bounds(int out_len, int exp_ret)
{
mbedtls_ssl_cookie_ctx ctx;
const unsigned char cli_id[] = { 0x7f, 0x00, 0x00, 0x01, 0x12, 0x34 };
const size_t guard_len = 64;
unsigned char *buf = NULL;
unsigned char *p;
size_t i;
mbedtls_ssl_cookie_init(&ctx);
PSA_INIT();
TEST_CALLOC(buf, out_len + guard_len);
memset(buf, 0x5a, out_len + guard_len);
TEST_EQUAL(mbedtls_ssl_cookie_setup(&ctx), 0);
p = buf;
TEST_EQUAL(mbedtls_ssl_cookie_write(&ctx, &p, buf + out_len,
cli_id, sizeof(cli_id)), exp_ret);
/* Nothing may be written past the end of the output buffer */
for (i = out_len; i < out_len + guard_len; i++) {
TEST_EQUAL(buf[i], 0x5a);
}
if (exp_ret == 0) {
TEST_EQUAL(p - buf, out_len);
TEST_EQUAL(mbedtls_ssl_cookie_check(&ctx, buf, p - buf,
cli_id, sizeof(cli_id)), 0);
}
exit:
mbedtls_free(buf);
mbedtls_ssl_cookie_free(&ctx);
PSA_DONE();
}
/* END_CASE */
/* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */
void timing_final_delay_accessor()
{