Skip to content

Commit 4ada436

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 #24718)
1 parent 93991bf commit 4ada436

1 file changed

Lines changed: 40 additions & 23 deletions

File tree

ssl/ssl_lib.c

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,37 +2953,54 @@ int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
29532953
unsigned int server_len,
29542954
const unsigned char *client, unsigned int client_len)
29552955
{
2956-
unsigned int i, j;
2957-
const unsigned char *result;
2958-
int status = OPENSSL_NPN_UNSUPPORTED;
2956+
PACKET cpkt, csubpkt, spkt, ssubpkt;
2957+
2958+
if (!PACKET_buf_init(&cpkt, client, client_len)
2959+
|| !PACKET_get_length_prefixed_1(&cpkt, &csubpkt)
2960+
|| PACKET_remaining(&csubpkt) == 0) {
2961+
*out = NULL;
2962+
*outlen = 0;
2963+
return OPENSSL_NPN_NO_OVERLAP;
2964+
}
2965+
2966+
/*
2967+
* Set the default opportunistic protocol. Will be overwritten if we find
2968+
* a match.
2969+
*/
2970+
*out = (unsigned char *)PACKET_data(&csubpkt);
2971+
*outlen = (unsigned char)PACKET_remaining(&csubpkt);
29592972

29602973
/*
29612974
* For each protocol in server preference order, see if we support it.
29622975
*/
2963-
for (i = 0; i < server_len;) {
2964-
for (j = 0; j < client_len;) {
2965-
if (server[i] == client[j] &&
2966-
memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
2967-
/* We found a match */
2968-
result = &server[i];
2969-
status = OPENSSL_NPN_NEGOTIATED;
2970-
goto found;
2976+
if (PACKET_buf_init(&spkt, server, server_len)) {
2977+
while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) {
2978+
if (PACKET_remaining(&ssubpkt) == 0)
2979+
continue; /* Invalid - ignore it */
2980+
if (PACKET_buf_init(&cpkt, client, client_len)) {
2981+
while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) {
2982+
if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt),
2983+
PACKET_remaining(&ssubpkt))) {
2984+
/* We found a match */
2985+
*out = (unsigned char *)PACKET_data(&ssubpkt);
2986+
*outlen = (unsigned char)PACKET_remaining(&ssubpkt);
2987+
return OPENSSL_NPN_NEGOTIATED;
2988+
}
2989+
}
2990+
/* Ignore spurious trailing bytes in the client list */
2991+
} else {
2992+
/* This should never happen */
2993+
return OPENSSL_NPN_NO_OVERLAP;
29712994
}
2972-
j += client[j];
2973-
j++;
29742995
}
2975-
i += server[i];
2976-
i++;
2996+
/* Ignore spurious trailing bytes in the server list */
29772997
}
29782998

2979-
/* There's no overlap between our protocols and the server's list. */
2980-
result = client;
2981-
status = OPENSSL_NPN_NO_OVERLAP;
2982-
2983-
found:
2984-
*out = (unsigned char *)result + 1;
2985-
*outlen = result[0];
2986-
return status;
2999+
/*
3000+
* There's no overlap between our protocols and the server's list. We use
3001+
* the default opportunistic protocol selected earlier
3002+
*/
3003+
return OPENSSL_NPN_NO_OVERLAP;
29873004
}
29883005

29893006
#ifndef OPENSSL_NO_NEXTPROTONEG

0 commit comments

Comments
 (0)