|
| 1 | +From cf6f91f6121f4db167405db2f0de410a456f260c Fri May 31 11:14:33 2024 +0100 |
| 2 | +author Matt Caswell < [email protected]> Fri May 31 11:14:33 2024 +0100 |
| 3 | +committer Matt Caswell < [email protected]> Thu Jun 27 10:39:47 2024 +0100 |
| 4 | + |
| 5 | +Fix SSL_select_next_proto |
| 6 | + |
| 7 | +Ensure that the provided client list is non-NULL and starts with a valid |
| 8 | +entry. When called from the ALPN callback the client list should already |
| 9 | +have been validated by OpenSSL so this should not cause a problem. When |
| 10 | +called from the NPN callback the client list is locally configured and |
| 11 | +will not have already been validated. Therefore SSL_select_next_proto |
| 12 | +should not assume that it is correctly formatted. |
| 13 | + |
| 14 | +We implement stricter checking of the client protocol list. We also do the |
| 15 | +same for the server list while we are about it. |
| 16 | + |
| 17 | +CVE-2024-5535 |
| 18 | + |
| 19 | +Reviewed-by: Neil Horman < [email protected]> |
| 20 | +Reviewed-by: Tomas Mraz < [email protected]> |
| 21 | +(Merged from https://github.com/openssl/openssl/pull/24718) |
| 22 | + |
| 23 | +(cherry picked from commit 4ada436a1946cbb24db5ab4ca082b69c1bc10f37) |
| 24 | + |
| 25 | +diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c |
| 26 | +index cb4e006ea7..e628140dfa 100644 |
| 27 | +--- a/ssl/ssl_lib.c |
| 28 | ++++ b/ssl/ssl_lib.c |
| 29 | +@@ -2952,37 +2952,54 @@ int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, |
| 30 | + unsigned int server_len, |
| 31 | + const unsigned char *client, unsigned int client_len) |
| 32 | + { |
| 33 | +- unsigned int i, j; |
| 34 | +- const unsigned char *result; |
| 35 | +- int status = OPENSSL_NPN_UNSUPPORTED; |
| 36 | ++ PACKET cpkt, csubpkt, spkt, ssubpkt; |
| 37 | ++ |
| 38 | ++ if (!PACKET_buf_init(&cpkt, client, client_len) |
| 39 | ++ || !PACKET_get_length_prefixed_1(&cpkt, &csubpkt) |
| 40 | ++ || PACKET_remaining(&csubpkt) == 0) { |
| 41 | ++ *out = NULL; |
| 42 | ++ *outlen = 0; |
| 43 | ++ return OPENSSL_NPN_NO_OVERLAP; |
| 44 | ++ } |
| 45 | ++ |
| 46 | ++ /* |
| 47 | ++ * Set the default opportunistic protocol. Will be overwritten if we find |
| 48 | ++ * a match. |
| 49 | ++ */ |
| 50 | ++ *out = (unsigned char *)PACKET_data(&csubpkt); |
| 51 | ++ *outlen = (unsigned char)PACKET_remaining(&csubpkt); |
| 52 | + |
| 53 | + /* |
| 54 | + * For each protocol in server preference order, see if we support it. |
| 55 | + */ |
| 56 | +- for (i = 0; i < server_len;) { |
| 57 | +- for (j = 0; j < client_len;) { |
| 58 | +- if (server[i] == client[j] && |
| 59 | +- memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { |
| 60 | +- /* We found a match */ |
| 61 | +- result = &server[i]; |
| 62 | +- status = OPENSSL_NPN_NEGOTIATED; |
| 63 | +- goto found; |
| 64 | ++ if (PACKET_buf_init(&spkt, server, server_len)) { |
| 65 | ++ while (PACKET_get_length_prefixed_1(&spkt, &ssubpkt)) { |
| 66 | ++ if (PACKET_remaining(&ssubpkt) == 0) |
| 67 | ++ continue; /* Invalid - ignore it */ |
| 68 | ++ if (PACKET_buf_init(&cpkt, client, client_len)) { |
| 69 | ++ while (PACKET_get_length_prefixed_1(&cpkt, &csubpkt)) { |
| 70 | ++ if (PACKET_equal(&csubpkt, PACKET_data(&ssubpkt), |
| 71 | ++ PACKET_remaining(&ssubpkt))) { |
| 72 | ++ /* We found a match */ |
| 73 | ++ *out = (unsigned char *)PACKET_data(&ssubpkt); |
| 74 | ++ *outlen = (unsigned char)PACKET_remaining(&ssubpkt); |
| 75 | ++ return OPENSSL_NPN_NEGOTIATED; |
| 76 | ++ } |
| 77 | ++ } |
| 78 | ++ /* Ignore spurious trailing bytes in the client list */ |
| 79 | ++ } else { |
| 80 | ++ /* This should never happen */ |
| 81 | ++ return OPENSSL_NPN_NO_OVERLAP; |
| 82 | + } |
| 83 | +- j += client[j]; |
| 84 | +- j++; |
| 85 | + } |
| 86 | +- i += server[i]; |
| 87 | +- i++; |
| 88 | ++ /* Ignore spurious trailing bytes in the server list */ |
| 89 | + } |
| 90 | + |
| 91 | +- /* There's no overlap between our protocols and the server's list. */ |
| 92 | +- result = client; |
| 93 | +- status = OPENSSL_NPN_NO_OVERLAP; |
| 94 | +- |
| 95 | +- found: |
| 96 | +- *out = (unsigned char *)result + 1; |
| 97 | +- *outlen = result[0]; |
| 98 | +- return status; |
| 99 | ++ /* |
| 100 | ++ * There's no overlap between our protocols and the server's list. We use |
| 101 | ++ * the default opportunistic protocol selected earlier |
| 102 | ++ */ |
| 103 | ++ return OPENSSL_NPN_NO_OVERLAP; |
| 104 | + } |
| 105 | + |
| 106 | + #ifndef OPENSSL_NO_NEXTPROTONEG |
0 commit comments