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