From 9a455081014f6afdf107d27366bb1ac79a677e72 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Murcia Date: Sun, 2 Aug 2026 21:24:34 -0400 Subject: [PATCH] Fix buffer overreads on malformed inputs in sample programs Fixes #10803. Signed-off-by: Felipe Reyes Murcia --- ChangeLog.d/fix-sample-program-memory-errors.txt | 6 ++++++ programs/ssl/ssl_context_info.c | 9 +++++++-- programs/ssl/ssl_server2.c | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 ChangeLog.d/fix-sample-program-memory-errors.txt diff --git a/ChangeLog.d/fix-sample-program-memory-errors.txt b/ChangeLog.d/fix-sample-program-memory-errors.txt new file mode 100644 index 0000000000..0812b727e9 --- /dev/null +++ b/ChangeLog.d/fix-sample-program-memory-errors.txt @@ -0,0 +1,6 @@ +Bugfix + * Fix potential buffer overreads on malformed inputs in sample programs: + ssl_server2 could read past the end of a session ticket shorter than + 4 bytes when the dummy_ticket option is enabled, and ssl_context_info + could call strlen() on non-null-terminated ALPN data from the input + file, reading past the end of the decoded buffer. Fixes #10803. diff --git a/programs/ssl/ssl_context_info.c b/programs/ssl/ssl_context_info.c index 9d7fb99e09..c7e0790bd7 100644 --- a/programs/ssl/ssl_context_info.c +++ b/programs/ssl/ssl_context_info.c @@ -882,8 +882,13 @@ static void print_deserialized_ssl_context(const uint8_t *ssl, size_t len) printf("\tALPN negotiation : "); CHECK_SSL_END(alpn_len); if (alpn_len > 0) { - if (strlen((const char *) ssl) == alpn_len) { - printf("%s\n", ssl); + /* The ALPN protocol name in the serialized data is not + * null-terminated, so do not use strlen() on it: with a + * malformed input file it could read past the end of the + * decoded buffer. Read exactly alpn_len bytes instead, and + * treat an embedded null byte as incorrect data. */ + if (memchr(ssl, '\0', alpn_len) == NULL) { + printf("%.*s\n", (int) alpn_len, ssl); } else { printf("\n"); printf_err("\tALPN negotiation is incorrect\n"); diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index b5cdc817b8..87cd6c76ec 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -1385,6 +1385,14 @@ static int dummy_ticket_parse(void *p_ticket, mbedtls_ssl_session *session, int ret; ((void) p_ticket); + /* Tickets are written by dummy_ticket_write() and start with a 4-byte + * lifetime header. Reject shorter tickets (which a misbehaving client + * could send) to avoid an underflow of len - 4 below resulting in a + * read past the end of the ticket buffer. */ + if (len < 4) { + return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; + } + if ((ret = mbedtls_ssl_session_load(session, buf + 4, len - 4)) != 0) { return ret; }