Skip to content

Commit e86ac43

Browse files
committed
Fix SSL_select_next_proto
Ensure that the provided client list is non-NULL and starts with a valid entry. When called from the ALPN callback the client list should already have been validated by OpenSSL so this should not cause a problem. When called from the NPN callback the client list is locally configured and will not have already been validated. Therefore SSL_select_next_proto should not assume that it is correctly formatted. We implement stricter checking of the client protocol list. We also do the same for the server list while we are about it. CVE-2024-5535 Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Neil Horman <nhorman@openssl.org> (Merged from #24716) (cherry picked from commit 2ebbe2d)
1 parent d489bc8 commit e86ac43

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

ssl/ssl_lib.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,37 +3530,54 @@ int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
35303530
unsigned int server_len,
35313531
const unsigned char *client, unsigned int client_len)
35323532
{
3533-
unsigned int i, j;
3534-
const unsigned char *result;
3535-
int status = OPENSSL_NPN_UNSUPPORTED;
3533+
PACKET cpkt, csubpkt, spkt, ssubpkt;
3534+
3535+
if (!PACKET_buf_init(&cpkt, client, client_len)
3536+
|| !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
3537+
|| PACKET_remaining(&csubpkt) == 0) {
3538+
*out = NULL;
3539+
*outlen = 0;
3540+
return OPENSSL_NPN_NO_OVERLAP;
3541+
}
3542+
3543+
/*
3544+
* Set the default opportunistic protocol. Will be overwritten if we find
3545+
* a match.
3546+
*/
3547+
*out = (unsigned char *)PACKET_data(&csubpkt);
3548+
*outlen = (unsigned char)PACKET_remaining(&csubpkt);
35363549

35373550
/*
35383551
* For each protocol in server preference order, see if we support it.
35393552
*/
3540-
for (i = 0; i < server_len;) {
3541-
for (j = 0; j < client_len;) {
3542-
if (server[i] == client[j] &&
3543-
memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
3544-
/* We found a match */
3545-
result = &server[i];
3546-
status = OPENSSL_NPN_NEGOTIATED;
3547-
goto found;
3553+
if (PACKET_buf_init(&spkt, server, server_len)) {
3554+
while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
3555+
if (PACKET_remaining(&ssubpkt) == 0)
3556+
continue; /* Invalid - ignore it */
3557+
if (PACKET_buf_init(&cpkt, client, client_len)) {
3558+
while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
3559+
if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
3560+
PACKET_remaining(&ssubpkt))) {
3561+
/* We found a match */
3562+
*out = (unsigned char *)PACKET_data(&ssubpkt);
3563+
*outlen = (unsigned char)PACKET_remaining(&ssubpkt);
3564+
return OPENSSL_NPN_NEGOTIATED;
3565+
}
3566+
}
3567+
/* Ignore spurious trailing bytes in the client list */
3568+
} else {
3569+
/* This should never happen */
3570+
return OPENSSL_NPN_NO_OVERLAP;
35483571
}
3549-
j += client[j];
3550-
j++;
35513572
}
3552-
i += server[i];
3553-
i++;
3573+
/* Ignore spurious trailing bytes in the server list */
35543574
}
35553575

3556-
/* There's no overlap between our protocols and the server's list. */
3557-
result = client;
3558-
status = OPENSSL_NPN_NO_OVERLAP;
3559-
3560-
found:
3561-
*out = (unsigned char *)result + 1;
3562-
*outlen = result[0];
3563-
return status;
3576+
/*
3577+
* There's no overlap between our protocols and the server's list. We use
3578+
* the default opportunistic protocol selected earlier
3579+
*/
3580+
return OPENSSL_NPN_NO_OVERLAP;
35643581
}
35653582

35663583
#ifndef OPENSSL_NO_NEXTPROTONEG

0 commit comments

Comments
 (0)