|
| 1 | +Fix unconstrained session cache growth in TLSv1.3 |
| 2 | +In TLSv1.3 we create a new session object for each ticket that we send. |
| 3 | +We do this by duplicating the original session. If SSL_OP_NO_TICKET is in |
| 4 | +use then the new session will be added to the session cache. However, if |
| 5 | +early data is not in use (and therefore anti-replay protection is being |
| 6 | +used), then multiple threads could be resuming from the same session |
| 7 | +simultaneously. If this happens and a problem occurs on one of the threads, |
| 8 | +then the original session object could be marked as not_resumable. When we |
| 9 | +duplicate the session object this not_resumable status gets copied into the |
| 10 | +new session object. The new session object is then added to the session |
| 11 | +cache even though it is not_resumable. |
| 12 | + |
| 13 | +Subsequently, another bug means that the session_id_length is set to 0 for |
| 14 | +sessions that are marked as not_resumable - even though that session is |
| 15 | +still in the cache. Once this happens the session can never be removed from |
| 16 | +the cache. When that object gets to be the session cache tail object the |
| 17 | +cache never shrinks again and grows indefinitely. |
| 18 | + |
| 19 | +CVE-2024-2511 |
| 20 | + |
| 21 | +Reviewed-by: Neil Horman < [email protected]> |
| 22 | +Reviewed-by: Tomas Mraz < [email protected]> |
| 23 | +(Merged from #24044) |
| 24 | + |
| 25 | +(cherry picked from commit 7e4d731) |
| 26 | +--- |
| 27 | + |
| 28 | +diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h |
| 29 | +index 9af0c8995e..64eaca338a 100644 |
| 30 | +--- a/include/openssl/ssl.h |
| 31 | ++++ b/include/openssl/ssl.h |
| 32 | +@@ -1659,7 +1659,7 @@ __owur int SSL_SESSION_set1_id(SSL_SESSION *s, const unsigned char *sid, |
| 33 | + __owur int SSL_SESSION_is_resumable(const SSL_SESSION *s); |
| 34 | + |
| 35 | + __owur SSL_SESSION *SSL_SESSION_new(void); |
| 36 | +-__owur SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *src); |
| 37 | ++__owur SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src); |
| 38 | + const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, |
| 39 | + unsigned int *len); |
| 40 | + const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *s, |
| 41 | +diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c |
| 42 | +index 47adc3211c..c01ad8291c 100644 |
| 43 | +--- a/ssl/ssl_lib.c |
| 44 | ++++ b/ssl/ssl_lib.c |
| 45 | +@@ -3515,9 +3515,10 @@ void ssl_update_cache(SSL *s, int mode) |
| 46 | + |
| 47 | + /* |
| 48 | + * If the session_id_length is 0, we are not supposed to cache it, and it |
| 49 | +- * would be rather hard to do anyway :-) |
| 50 | ++ * would be rather hard to do anyway :-). Also if the session has already |
| 51 | ++ * been marked as not_resumable we should not cache it for later reuse. |
| 52 | + */ |
| 53 | +- if (s->session->session_id_length == 0) |
| 54 | ++ if (s->session->session_id_length == 0 || s->session->not_resumable) |
| 55 | + return; |
| 56 | + |
| 57 | + /* |
| 58 | +diff --git a/ssl/ssl_local.h b/ssl/ssl_local.h |
| 59 | +index 5c79215423..5e73fa4e22 100644 |
| 60 | +--- a/ssl/ssl_local.h |
| 61 | ++++ b/ssl/ssl_local.h |
| 62 | +@@ -2261,7 +2261,7 @@ __owur int ssl_get_new_session(SSL *s, int session); |
| 63 | + __owur SSL_SESSION *lookup_sess_in_cache(SSL *s, const unsigned char *sess_id, |
| 64 | + size_t sess_id_len); |
| 65 | + __owur int ssl_get_prev_session(SSL *s, CLIENTHELLO_MSG *hello); |
| 66 | +-__owur SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket); |
| 67 | ++__owur SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket); |
| 68 | + __owur int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b); |
| 69 | + DECLARE_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id); |
| 70 | + __owur int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap, |
| 71 | +diff --git a/ssl/ssl_sess.c b/ssl/ssl_sess.c |
| 72 | +index cda6b7cc5b..404599169d 100644 |
| 73 | +--- a/ssl/ssl_sess.c |
| 74 | ++++ b/ssl/ssl_sess.c |
| 75 | +@@ -94,16 +94,11 @@ SSL_SESSION *SSL_SESSION_new(void) |
| 76 | + return ss; |
| 77 | + } |
| 78 | + |
| 79 | +-SSL_SESSION *SSL_SESSION_dup(SSL_SESSION *src) |
| 80 | +-{ |
| 81 | +- return ssl_session_dup(src, 1); |
| 82 | +-} |
| 83 | +- |
| 84 | + /* |
| 85 | + * Create a new SSL_SESSION and duplicate the contents of |src| into it. If |
| 86 | + * ticket == 0 then no ticket information is duplicated, otherwise it is. |
| 87 | + */ |
| 88 | +-SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket) |
| 89 | ++static SSL_SESSION *ssl_session_dup_intern(const SSL_SESSION *src, int ticket) |
| 90 | + { |
| 91 | + SSL_SESSION *dest; |
| 92 | + |
| 93 | +@@ -223,6 +218,27 @@ SSL_SESSION *ssl_session_dup(SSL_SESSION *src, int ticket) |
| 94 | + return NULL; |
| 95 | + } |
| 96 | + |
| 97 | ++SSL_SESSION *SSL_SESSION_dup(const SSL_SESSION *src) |
| 98 | ++{ |
| 99 | ++ return ssl_session_dup_intern(src, 1); |
| 100 | ++} |
| 101 | ++ |
| 102 | ++/* |
| 103 | ++ * Used internally when duplicating a session which might be already shared. |
| 104 | ++ * We will have resumed the original session. Subsequently we might have marked |
| 105 | ++ * it as non-resumable (e.g. in another thread) - but this copy should be ok to |
| 106 | ++ * resume from. |
| 107 | ++ */ |
| 108 | ++SSL_SESSION *ssl_session_dup(const SSL_SESSION *src, int ticket) |
| 109 | ++{ |
| 110 | ++ SSL_SESSION *sess = ssl_session_dup_intern(src, ticket); |
| 111 | ++ |
| 112 | ++ if (sess != NULL) |
| 113 | ++ sess->not_resumable = 0; |
| 114 | ++ |
| 115 | ++ return sess; |
| 116 | ++} |
| 117 | ++ |
| 118 | + const unsigned char *SSL_SESSION_get_id(const SSL_SESSION *s, unsigned int *len) |
| 119 | + { |
| 120 | + if (len) |
| 121 | +diff --git a/ssl/statem/statem_srvr.c b/ssl/statem/statem_srvr.c |
| 122 | +index 43f77a5899..f55e11bde9 100644 |
| 123 | +--- a/ssl/statem/statem_srvr.c |
| 124 | ++++ b/ssl/statem/statem_srvr.c |
| 125 | +@@ -2403,9 +2403,8 @@ int tls_construct_server_hello(SSL *s, WPACKET *pkt) |
| 126 | + * so the following won't overwrite an ID that we're supposed |
| 127 | + * to send back. |
| 128 | + */ |
| 129 | +- if (s->session->not_resumable || |
| 130 | +- (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 131 | +- && !s->hit)) |
| 132 | ++ if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) |
| 133 | ++ && !s->hit) |
| 134 | + s->session->session_id_length = 0; |
| 135 | + |
| 136 | + if (usetls13) { |
0 commit comments