Skip to content

Commit 99fb785

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: Neil Horman <nhorman@openssl.org> Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from #24717)
1 parent 1967b73 commit 99fb785

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
@@ -3518,37 +3518,54 @@ int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
35183518
unsigned int server_len,
35193519
const unsigned char *client, unsigned int client_len)
35203520
{
3521-
unsigned int i, j;
3522-
const unsigned char *result;
3523-
int status = OPENSSL_NPN_UNSUPPORTED;
3521+
PACKET cpkt, csubpkt, spkt, ssubpkt;
3522+
3523+
if (!PACKET_buf_init(&cpkt, client, client_len)
3524+
|| !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
3525+
|| PACKET_remaining(&csubpkt) == 0) {
3526+
*out = NULL;
3527+
*outlen = 0;
3528+
return OPENSSL_NPN_NO_OVERLAP;
3529+
}
3530+
3531+
/*
3532+
* Set the default opportunistic protocol. Will be overwritten if we find
3533+
* a match.
3534+
*/
3535+
*out = (unsigned char *)PACKET_data(&csubpkt);
3536+
*outlen = (unsigned char)PACKET_remaining(&csubpkt);
35243537

35253538
/*
35263539
* For each protocol in server preference order, see if we support it.
35273540
*/
3528-
for (i = 0; i < server_len;) {
3529-
for (j = 0; j < client_len;) {
3530-
if (server[i] == client[j] &&
3531-
memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
3532-
/* We found a match */
3533-
result = &server[i];
3534-
status = OPENSSL_NPN_NEGOTIATED;
3535-
goto found;
3541+
if (PACKET_buf_init(&spkt, server, server_len)) {
3542+
while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
3543+
if (PACKET_remaining(&ssubpkt) == 0)
3544+
continue; /* Invalid - ignore it */
3545+
if (PACKET_buf_init(&cpkt, client, client_len)) {
3546+
while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
3547+
if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
3548+
PACKET_remaining(&ssubpkt))) {
3549+
/* We found a match */
3550+
*out = (unsigned char *)PACKET_data(&ssubpkt);
3551+
*outlen = (unsigned char)PACKET_remaining(&ssubpkt);
3552+
return OPENSSL_NPN_NEGOTIATED;
3553+
}
3554+
}
3555+
/* Ignore spurious trailing bytes in the client list */
3556+
} else {
3557+
/* This should never happen */
3558+
return OPENSSL_NPN_NO_OVERLAP;
35363559
}
3537-
j += client[j];
3538-
j++;
35393560
}
3540-
i += server[i];
3541-
i++;
3561+
/* Ignore spurious trailing bytes in the server list */
35423562
}
35433563

3544-
/* There's no overlap between our protocols and the server's list. */
3545-
result = client;
3546-
status = OPENSSL_NPN_NO_OVERLAP;
3547-
3548-
found:
3549-
*out = (unsigned char *)result + 1;
3550-
*outlen = result[0];
3551-
return status;
3564+
/*
3565+
* There's no overlap between our protocols and the server's list. We use
3566+
* the default opportunistic protocol selected earlier
3567+
*/
3568+
return OPENSSL_NPN_NO_OVERLAP;
35523569
}
35533570

35543571
#ifndef OPENSSL_NO_NEXTPROTONEG

0 commit comments

Comments
 (0)